【问题标题】:Bluemix force HTTPS on Spring Boot applicationBluemix 在 Spring Boot 应用程序上强制使用 HTTPS
【发布时间】:2016-12-24 14:30:36
【问题描述】:

我有一个 Spring Boot 应用程序,它作为 CF 应用程序推送到 Bluemix 上。 它与 http 协议一起有效地工作。但是,如果我尝试强制使用 https,则会收到 502 错误。

我有:

@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

}

我有一个包含这些条目的 application.properties 文件:

server.ssl.key-store = classpath:**.jks
server.ssl.key-store-password = *******
server.ssl.key-password = ******

server.tomcat.remote_ip_header=x-forwarded-for
server.tomcat.protocol_header=x-forwarded-proto

我知道 Bluemix 执行 SSL 终止;事实上,它正确设置了 x-forwarded-proto 和 x-forwarded-for。我寻找像 12 这样的解决方案,但没有任何运气。

然后我按照this article 中的建议尝试了以下解决方案,但收到了一个重定向循环:

@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory(){
    return 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 没有将 x-forwarded 标头检测为受信任的代理。尝试设置 server.tomcat.internal-proxies=.* 和 logging.level.org.apache.catalina.valves=DEBUG
  • 谢谢!它确实有助于找到解决方案!为了完整起见,因为 Bluemix 已经执行 SSL 验证,所以我删除了 server.ssl.* 部分广告,添加了以下内容:server.tomcat.internal-proxies=.* server.use-forward-headers=true 现在它就像一个魅力,它还执行 HTTP 到 HTTPS 的重定向。再次感谢您的帮助

标签: ssl spring-security spring-boot ibm-cloud cloud-foundry


【解决方案1】:

为了社区的利益,很高兴看到 Rob 的评论被接受为答案。 Rob,如果您希望看到该答案被接受,请随意添加您自己的答案。

Tomcat 未将 x-forwarded 标头检测为可信代理。尝试设置 server.tomcat.internal-proxies=.* 和 logging.level.org.apache.catalina.valves=DEBUG

【讨论】:

    猜你喜欢
    • 2017-02-05
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 2014-09-27
    • 2019-04-16
    • 2020-06-22
    相关资源
    最近更新 更多