ssl - Bluemix 在 Spring Boot 应用程序上强制使用 HTTPS

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

我有一个 Spring Boot 应用程序,它作为 CF 应用程序推送到 Bluemix 上。 它可以有效地与 http 协议(protocol)一起工作。但是,如果我尝试强制使用 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。我寻找类似 1 的解决方案和 2但没有任何运气。

然后我按照 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);
        }
    };
}

我在方法中遗漏了什么?非常感谢您提供给我的任何提示/建议

最佳答案

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

Tomcat is not detecting the x-forwarded headers as being a trusted proxy. Try setting server.tomcat.internal-proxies=.* and logging.level.org.apache.catalina.valves=DEBUG

关于ssl - Bluemix 在 Spring Boot 应用程序上强制使用 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39004986/

相关文章:

ios - SRTP 问题 : PJSIP Error initializing media channel: Not Acceptable Here [status=170488]

java - 我可以为客户端和服务器 Java SSL 使用相同的自签名证书吗

tomcat - HAProxy 负载均衡与 tomcat SSL

Spring security with @RestController - JSONish CustomAuthenticationProvider 响应

java - 将 json 插入 postgreSQL

Spring Boot 2.0.0.M2 和 Spring Data Elasticsearch 配置

magento - Nginx 在 https 域上提供非安全资源

java - 通过 java 代码配置 spring security 的自定义 403 错误页面

java - Spring Security中PreAuthorize注解中使用permitAll()的目的

java - 是否可以使用不同的端口实例化同一个 spring-boot 应用程序两次?