SpringBoot项目配置阿里云的SSL证书,简单有效可行

1.首先申请SSL证书,在搜索框申请免费的SSL证书,大概十五分钟会通过

SpringBoot项目配置阿里云的SSL证书,简单有效可行_第1张图片

 2.第二步,下载证书:

SpringBoot项目配置阿里云的SSL证书,简单有效可行_第2张图片

3.因为Springboot打包成jar包运行内置了Tomcat,所以我下载的是Tomcat,导入到项目的Resources下

SpringBoot项目配置阿里云的SSL证书,简单有效可行_第3张图片

 不用在意c3p0, 下载的证书就是3049031_.....和pfx-password的这个

4.倒数第二部,在application.yml配置信息如下:因为Https协议默认就是443端口

SpringBoot项目配置阿里云的SSL证书,简单有效可行_第4张图片

5.最后一步:在项目里加上这个配置类:一定要加!!!

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConnectorConfig {

    @Bean
    public ServletWebServerFactory servletWebServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection securityCollection = new SecurityCollection();
                securityCollection.addPattern("/*");
                securityConstraint.addCollection(securityCollection);
                context.addConstraint(securityConstraint);
            }
        };
        factory.addAdditionalTomcatConnectors(redirectConnector());
        return factory;
    }

    private Connector redirectConnector() {
        Connector connector = new Connector(Http11NioProtocol.class.getName());
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(443);
        return connector;
    }
}

配置完毕后别着急运行,这个要是在本地运行会报错端口占用,而且SSL也是需要域名的,所以请到了代码完成后准备发布的时候再修改成这样再打成jar包,在服务器上运行就没有问题了

你可能感兴趣的:(SpringBoot项目配置阿里云的SSL证书,简单有效可行)