Spring Security和AJP代理

8

我使用Spring Security和Apache代理来进行网站应用程序的开发。当使用标准的mod_proxy时一切正常,但是在切换到AJP代理后,出现了Spring安全重定向的问题。

Apache配置:

<VirtualHost *:80>
  ServerName domain.com

  ProxyPass / ajp://localhost:8009/Context/
  ProxyPassReverse / ajp://localhost:8009/Context/
</VirtualHost>

当我访问http://domain.com/login时,会看到一个登录表单。
当我提交表单后,我会跳转到http://domain.com/auth并得到认证。
然后Spring Security应该重定向到http://domain.com/index,但它实际上重定向到http://domain.com/Context/index
如何去掉这个上下文路径?为什么Spring Security在每个地方都添加它?
Spring Security网站上有一个类似的问题,但没有人回答: http://forum.springsource.org/showthread.php?95141-Why-is-spring-security-including-the-context-path P.S. 奇怪的是,Google找不到更多与此问题相关的内容。难道只有我在使用Spring Security + AJP吗?也许这是一种错误的模式?
解决方案:
<VirtualHost *:80>
  ServerName domain.com

  RewriteEngine on
  RewriteRule ^/Context/(.*)$ /$1 [R=301]

  ProxyPass / ajp://localhost:8009/Context/
  ProxyPassReverse / ajp://localhost:8009/Context/
</VirtualHost>
1个回答

4

Spring Security是Web应用程序上下文感知的,这意味着它的重定向将始终基于当前Web应用程序上下文。这是有意设计的,因为您的应用服务器可能正在运行几个不同的Web应用程序,它们不应该互相干扰。

您是否仅在服务器上运行此应用程序并有可能将其部署为Tomcat的ROOT应用程序(例如,将其放入webapps/ROOT/)?这将消除您的上下文前缀并解决您的问题。

另一种选择可能是在应用程序服务器将重定向URL传递给客户端之前重新编写它,例如使用org.tuckey的伟大的URLRewriteFilter(类似于mod_rewrite,但适用于Java EE Web应用程序)中的outbound-rule。当然,在您的web.xml中需要注意正确的过滤器顺序,因为Spring Security也使用过滤器进行其逻辑处理。


谢谢!最终我使用了Apache mod_rewrite。请查看我的问题更新。 - Andrey Minogin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接