【问题标题】:spring boot automatic redirect http to httpsspring boot 自动重定向 http 到 https
【发布时间】:2019-05-15 11:06:27
【问题描述】:

美好的一天,我在 SpringBoot 2 上构建了带有微服务和网关 (zuul) 的应用程序。它全部使用 SSL。 我需要从:http:\\localhost(当前什么都不显示)到 https:\\localhost(显示一些文本)的自动重定向,所以用户不需要打扰。

再次:http:\\localhost 必须显示与 https:\\localhost 相同的文本(我需要重定向)

我试过了,什么都没做。

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requiresChannel().anyRequest().requiresSecure();
    }
}

尝试了另一种方法,但是 SpringBoot 无法识别 TomcatEmbeddedServletContainerFactory

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {

        @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(createHttpConnector());
    return tomcat;
}

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

这个也不起作用(似乎没有改变任何东西)

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private Environment environment;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        // other security configuration missing

        http.portMapper()
                .http(80) // http port defined in yml config file
                .mapsTo(443); // https port defined in yml config file

        // we only need https on /auth
        http.requiresChannel()
                .antMatchers("/auth/**").requiresSecure()
                .anyRequest().requiresInsecure();
    }
}

这个也不起作用,错误是无法实例化[org.springframework.web.servlet.HandlerMapping]:工厂方法'resourceHandlerMapping'抛出异常;嵌套异常是 java.lang.IllegalStateException: No ServletContext set

@Bean
public TomcatServletWebServerFactory httpsRedirectConfig() {
    return 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);
        }
    };
}

甚至这个 java.lang.IllegalStateException: No ServletContext set 错误

  @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("http");
        connector.setPort(80);
        connector.setSecure(false);
        connector.setRedirectPort(443);
        return connector;
    }

有什么建议吗?

【问题讨论】:

    标签: http spring-boot redirect https


    【解决方案1】:

    知道了。 感谢 EstebanGarciaAlonso 和他的回答 ugrade spring boot 2.0.0.RC2 exception No ServletContext set

    "调试后,问题是mvc配置类 EnableWebMvcConfiguration 加载过早,servlet 尚未加载。”

    我在这上面花了几个小时。我设法找到了一个原因 正在发生。我的配置被分成几个文件,我正在创建一个 Security Config(之前创建的)中的 MVC 相关 bean 强制在其时间之前使用 MVC 配置。

    解决方案是从安全配置中移动 @Bean 实例 到 MVC 配置。我希望它可以帮助其他人!

    我在 main 方法之前将以下代码移至 Application.java,一切都像奇迹一样工作

       @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("http");
            connector.setPort(80);
            connector.setSecure(false);
            connector.setRedirectPort(443);
            return connector;
        }
    

    【讨论】:

    • 此解决方案所需的导入或依赖项是什么?当我粘贴代码时(我是 Java 菜鸟),到处都是红色曲线,当我将鼠标悬停在 TomcatServletWebServerFactory 上时,我的 IDE 建议的快速修复是“创建类”而不是导入。
    猜你喜欢
    • 2021-03-31
    • 2014-12-26
    • 2021-12-12
    • 2016-11-06
    • 2019-09-21
    • 2014-12-13
    • 2014-05-02
    • 2019-02-12
    • 1970-01-01
    相关资源
    最近更新 更多