SpringBoot 2 HTTP转HTTPS

Posted 0x4D6E

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 2 HTTP转HTTPS相关的知识,希望对你有一定的参考价值。

@Bean
public TomcatServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            SecurityConstraint constraint = new SecurityConstraint();
            constraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            constraint.addCollection(collection);
            context.addConstraint(constraint);
        }
    };
    tomcat.addAdditionalTomcatConnectors(httpConnector());
    return tomcat;
}

@Bean
public Connector httpConnector() {
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setScheme("http");
    //Connector监听的http的端口号
    connector.setPort(httpPort);
    connector.setSecure(false);
    //监听到http的端口号后转向到的https的端口号
    connector.setRedirectPort(httpsPort);
    return connector;
}

 

以上是关于SpringBoot 2 HTTP转HTTPS的主要内容,如果未能解决你的问题,请参考以下文章

springboot 如何配置http跳转https(301)?

SpringBoot配置HTTPS,并实现HTTP访问自动转HTTPS访问

SpringBoot配置HTTPS,并实现HTTP访问自动转HTTPS访问

Springboot以Tomcat为容器实现http重定向到https的两种方式

Springboot以Tomcat为容器实现http重定向到https的两种方式

设置http跳转https的几种方法