springboot 同时支持http和https请求

SpringBoot不支持同时在配置中启动http和https,所以需要把http请求重定向到https请求

//配置方式

@Configuration
public class TomcatHttp2HttpsConfig {

    public TomcatServletWebServerFactory tomcatServletWebServerFactory(){
        //org.apache.coyote.http11.Http11NioProtocol
        Connector connector = new Connector();
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(8081);//重定向https端口地址
        TomcatServletWebServerFactory webServerFactory = 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);
            }
        };
        webServerFactory.addAdditionalTomcatConnectors(connector);
        return webServerFactory;
    }
}

你可能感兴趣的:(SpringBoot,java,Tomcat相关)