【问题标题】:Spring Security: requires-channel="https" behind SSL acceleratorSpring Security:SSL 加速器后面的 requires-channel="https"
【发布时间】:2011-10-07 15:05:18
【问题描述】:

我们正在使用 F5 BIG-IP 设备来终止 SSL 连接,并通过纯 HTTP 连接到具有启用 spring 的应用程序的应用程序服务器。我们还配置 F5 以发送带有 http 或 https 作为值的 X-Forwarded-Proto 标头。

现在我们想通过配置拦截 url 来强制执行 HTTPS:

<security:intercept-url pattern="/login.action" requires-channel="https" />

但这只有在 servlet 容器中的协议方案是 HTTPS 时才有效,因此我们需要解释 HTTP 标头。

知道怎么做吗?

谢谢 西蒙

【问题讨论】:

    标签: ssl spring-security accelerator


    【解决方案1】:

    子类 SecureChannelProcessor 和 InsecureChannelProcessor 覆盖 decide()。您需要复制并粘贴一些代码,例如 Secure:

        @Override
        public void decide(FilterInvocation invocation, Collection<ConfigAttribute> config) throws IOException, ServletException {
          Assert.isTrue((invocation != null) && (config != null), 
                           "Nulls cannot be provided");
    
          for (ConfigAttribute attribute : config) {
              if (supports(attribute)) {
                  if (invocation.getHttpRequest().
                          getHeader("X-Forwarded-Proto").equals("http")) {
                      entryPoint.commence(invocation.getRequest(),
                          invocation.getResponse());
                  }
              }
          }
        }
    

    然后使用 BeanPostProcessor 在 ChannelDecisionManagerImpl bean 上设置这些 ChannelProcessor。

    【讨论】:

      【解决方案2】:

      我知道这个问题/答案已有 4 年历史,但它可以帮助我找到问题的解决方案。但在现代 Spring Boot 应用程序中,修复更容易。只需在您的application.yaml 中添加以下条目:

      server.tomcat.protocol_header: x-forwarded-proto

      更多信息在这里:http://docs.spring.io/spring-boot/docs/current/reference/html/howto-security.html#howto-enable-https

      【讨论】:

      • 我试过了,但它似乎只有在您使用嵌入式 Tomcat 实例而不将应用程序作为 WAR 部署到外部 Tomcat 实例上时才有效。
      • 遵循参考文档会导致请求被重定向到/login,仅通过extends WebSecurityConfigurerAdapter 的存在而没有其他。
      【解决方案3】:

      现在更简单了:

      server.use-forward-headers: true
      

      默认为 Cloud Foundry 和 Heroku 启用,但不适用于 AWS 等其他产品。

      文档(第 73.7 节):https://docs.spring.io/spring-boot/docs/1.5.x/reference/html/howto-embedded-servlet-containers.html

      【讨论】:

      • 这是当今最好的解决方案。
      • 链接已失效。我什至看不出承认标头与强制 SSL 有什么关系。
      猜你喜欢
      • 2012-08-03
      • 2014-07-24
      • 2015-10-19
      • 1970-01-01
      • 2011-09-18
      • 2013-10-15
      • 2018-12-11
      • 2018-10-01
      • 1970-01-01
      相关资源
      最近更新 更多