如何使Spring Security应用程序在代理后面运行?

lex*_*exx 8 java spring spring-security resteasy

我们有一个基于Spring 1.6.3的Java 1.6上的应用程序,它使用Spring Security 3.0.5并使用Spring Web和RestEasy 2.1.0实现REST API.我需要将此应用程序(服务器)置于代理之后,该代理将HTTPS请求流量从REST API客户端应用程序转换为HTTP流量.此更改为登录请求创建"跨域"方案:客户端发送HTTPS登录请求,服务器使用HTTP的重定向URL进行应答.目前它回复:

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

我需要的是:

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

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

在forum.spring.io上的线程

我已经使用contextRelative ="true"设置RedirectStrategy bean并从LoginSuccessHandler扩展类中的AbstractAuthenticationTargetUrlRequestHandler覆盖redirectStrategy setter,我看到HttpServletResponse对象的redirectStrategy属性按预期设置为true.仍然没有解决问题.

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

”http://192.168.0.10:8090/otherLogin”

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

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

最好的祝福.

Vla*_*fer 7

发送重定向时,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);
}
Run Code Online (Sandbox Code Playgroud)

sendRedirect方法必须以下列方式运行:

该方法可以接受相对URL;Servlet容器必须在将响应发送到客户端之前将相对URL转换为绝对URL。

这意味着,无论配置或上下文设置是什么,您都将始终获得绝对URL。

您有多种选择:

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