【问题标题】:How to make Spring Security application to run behind a proxy?如何使 Spring Security 应用程序在代理后面运行?
【发布时间】:2014-12-19 01:07:21
【问题描述】:

我们有一个基于 Java 1.6 和 Spring 3.0.3 的应用程序,它使用 Spring Security 3.0.5,并使用带有 RestEasy 2.1.0 的 Spring Web 实现 REST API。 我需要将此应用程序(服务器)放在代理后面,该代理会将来自 REST API 客户端应用程序的 HTTPS 请求流量转换为 HTTP 流量。此更改为登录请求创建了“跨域”场景:客户端发送 HTTPS 登录请求,服务器使用 HTTP 的重定向 URL 进行响应。目前它的回应是:

”http://192.168.0.10:8090/index.html;jsessionid=64FD79...86D”,

我需要的是:

”/index.html;jsessionid=64FD79...86D”

我们提供了使服务器响应“相对”URL 而不是“绝对”URL 的解决方案。所以我尝试在这里实现与描述情况类似的东西:

thread on forum.spring.io

我已经使用 contextRelative="true" 设置了 RedirectStrategy bean,并在我的 LoginSuccessHandler 扩展类中覆盖了来自 AbstractAuthenticationTargetUrlRequestHandler 的 redirectStrategy 设置器,我看到 HttpServletResponse 对象的 redirectStrategy 属性按预期设置为 true。还是没有解决问题。

当使用 encodeRedirectURL("otherLogin") 更改 HttpServletResponse 对象的 redirectURLCC 属性时,也会设置类似

”http://192.168.0.10:8090/otherLogin”

这不是我需要的。我需要删除 URL 的整个协议+ipaddress 部分。响应对象的 URL 属性不可更改,因为它由 Filter 和 FilterChain 接口实现包装。

请提出任何想法。我想这种事情应该在 web.xml 或 auth-AplicationContext.xml 文件中解决,而不是在代码中。

最好的问候。

【问题讨论】:

    标签: java spring spring-security resteasy


    【解决方案1】:

    Spring Security 在发送重定向时使用以下逻辑:

    public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
        String redirectUrl = calculateRedirectUrl(request.getContextPath(), url);
        redirectUrl = response.encodeRedirectURL(redirectUrl);
    
        if (logger.isDebugEnabled()) {
            logger.debug("Redirecting to '" + redirectUrl + "'");
        }
    
        response.sendRedirect(redirectUrl);
    }
    

    sendRedirect 方法需要以下列方式运行:

    此方法可以接受相对 URL; servlet 容器必须 在发送之前将相对 URL 转换为绝对 URL 回复客户。

    这意味着无论配置或上下文设置如何,默认情况下您将始终获得绝对 URL。

    您有多种选择:

    • 配置您的容器或应用程序服务器以了解公共 URL(例如,使用 AJP 代替 HTTP 反向代理,或使用某些应用程序服务器支持的公共 URL 传递 HTTP 标头),例如documentation for Tomcat
    • 配置您的 HTTP 反向代理以执行正确的重写,例如见ProxyPassReverseApache mod_proxy documentation
    • 实现自定义org.springframework.security.web.RedirectStrategy,您将在其中手动设置Location 响应标头和HTTP 302 状态代码,这应该允许您根据需要发送上下文相关重定向

    【讨论】:

      【解决方案2】:

      不完全是相对的,但可能您可能希望查看使用org.springframework.web.servlet.view.UrlBasedViewResolver 的第四个选项,以使用您的外部主机名重写重定向的 URL,如以下答案所述:Override the default redirect URL in a SpringMVC application

      【讨论】:

        【解决方案3】:

        这应该可以通过以下方式在 Apache 上实现:

        ProxyPreserveHost Off
        ProxyPass / http://192.168.0.10:8090
        ProxyPassReverse / http://192.168.0.10:8090
        

        并在使用该端口的代理主机上再添加一个反向代理。 例如,假设您的服务器名称是 proxy.example.com,第四行将是:

        ProxyPassReverse / http://proxy.exemple.com:8090
        

        看看这个答案:Sending redirect in Tomcat web application behind a Apache 2 proxy (mod_proxy)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-06-28
          • 1970-01-01
          • 1970-01-01
          • 2021-08-30
          • 1970-01-01
          • 2012-07-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多