首页 > 解决方案 > Spring Boot SSL 不会为所有人重定向 HTTP 到 HTTPS

问题描述

我已经使用 SSL 证书为我的网站设置了 HTTPS,一切似乎都运行良好,如果我输入 www.realmlands.com 它会将我重定向到 https:// 并且如果我删除 https:// 上的“s”并且点击输入它也会重定向到 https:// 但问题在于其他人,当他们去 www.realmlands.com 时它说不安全并且它不会让他们登录,因为它不是 https:// 但它应该重定向他们自动地。

我已经尝试过这种似乎大部分时间都可以使用的配置。

    @Bean
    public ServletWebServerFactory servletContainer() {
        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(redirectConnector());
        return tomcat;
    }

    private Connector redirectConnector() {
        Connector connector = new Connector(
                TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setScheme("https");
        connector.setPort(8080);
        connector.setSecure(true);
        connector.setRedirectPort(443);
        return connector;
    }

它应该将每个人重定向到 https:// 但有些人认为不安全并被允许使用 http://

标签: javaspringspring-bootspring-security

解决方案


推荐阅读