8

我们使用 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 标头。

知道怎么做吗?

谢谢西蒙

4

3 回答 3

8

子类SecureChannelProcessorInsecureChannelProcessor覆盖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 。

于 2011-07-18T13:48:18.963 回答
1

我知道这个问题/答案已有 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

于 2017-03-17T12:59:05.757 回答
0

现在更简单了:

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

于 2017-12-28T08:20:30.867 回答