春ブーツ - HTTPリダイレクトへのHTTPSアクセスソリューション

ソリューション 

方法1:使用プラス起動クラスは、クラスの@SpringBootApplicationノートです 

/**
 * http重定向到https
 * @return
 */
@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(8080);
    connector.setSecure(false);
    //监听到http的端口号后转向到的https的端口号,也就是项目配置的port
    connector.setRedirectPort(8089);
    return connector;
}

方法2:新しい構成クラスを作成し、プラスステートメント@Configurationコメント

@Configuration
public class TomcatConfig {
    @Bean
    TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(){
            @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);
            }
        };
        factory.addAdditionalTomcatConnectors(createTomcatConnector());
        return factory;
    }

    private Connector createTomcatConnector() {
        Connector connector = new
                Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(5001);
        connector.setSecure(false);
        connector.setRedirectPort(443);
        return connector;
    }
}

参考記事

https://blog.csdn.net/baidu_37302589/article/details/100692957

リリース1401元の記事 ウォンの賞賛249 ビュー360 000 +

おすすめ

転載: blog.csdn.net/weixin_43272781/article/details/104412715