获取证书

  • 这里我用阿里云做例子
  1. 打开阿里云,在这里免费获取证书就可以了
    image.png
  2. 下载对应的证书,这里我下载是tomcat,其实更建议使用nginx做认证(等我以后研究).

接下来我们来设置springboot项目

  1. 首先修改配置
    image.png
  2. 配置tomcat
@Bean
public TomcatServletWebServerFactory servletContainer() { //springboot2 新变化

        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {

            @Override
            protected void postProcessContext(Context context) {

                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
        return tomcat;
    }

    private Connector initiateHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(port);//http端口
        connector.setSecure(true);//这个坑,网上普遍为false重定向容易出错,我这里设置为true
        connector.setRedirectPort(httpsPort);
        return connector;
    }
  • 这样,就已经支撑了https访问了
上一篇 下一篇