在使用 SpringBoot 安全性的应用程序上,Safari 不会自动从 http 端口重定向到 https 端口

问题描述

我的 SpringBoot 应用程序已配置为在 https 端口上运行。它工作正常。现在我想自动将流量从 HTTP 端口重定向到 HTTPS 端口。该代码也可以正常工作 - 但仅限于 Firefox。为什么 Safari 不自动重定向

在 Safari 上点击 HTTP URL:PORT,在服务器控制台上给出奇怪的日志 - 方法名称中的非法字符等。

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
    
    
    /**
     * This part is option & used only for this particular use case. 
     * 
     * We want the http port 8080 redirect automatically to the https port 8443. 
     * 
     */
    
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext (Context context) {
            SecurityConstraint securityConstriant=new SecurityConstraint();
            securityConstriant.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection=new SecurityCollection();
            collection.addPattern("/*");
            securityConstriant.addCollection(collection);
            context.addConstraint(securityConstriant);
        }
        };
        tomcat.addAdditionalTomcatConnectors(redirectConnector());
        return tomcat;
        
    }


    private Connector redirectConnector() {
        Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setRedirectPort(8443);
        return connector;
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)