tomcat - Spring Security 和 AJP 代理

标签 tomcat spring-security mod-proxy ajp

我将 Spring Security 和 Apache 代理用于 Web 应用程序。使用标准 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

附言 谷歌没有找到与此问题更多相关的内容,这似乎很奇怪。我是唯一一个使用 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>

最佳答案

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

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

另一种选择可能是在将重定向 URL 传递给客户端之前重写应用服务器上的重定向 URL,例如。 G。与 an outbound-rule from org.tuckey's great URLRewriteFilter (类似于 mod_rewrite,但适用于 Java EE 网络应用程序)。当然,您必须在 web.xml 中注意正确的过滤器排序,因为 Spring Security 也为其逻辑使用过滤器。

关于tomcat - Spring Security 和 AJP 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6063339/

相关文章:

tomcat - 如何让 Apache Tomcat 从 'www.myapp.com' 而不是 'www.myapp.com/myapp' 为我的网页提供服务?

java - 创建名称为 'inMemoryUserDetailsManager' 的 bean 时出错

java - 如何确保响应中存在某些 http header

java - Controller (Web 请求/antMatcher)安全性与方法(服务)级安全性

node.js - Express 3、Apache 代理和基本路径

Apache ProxyPass HTTPS 和带有 SNI 的远程服务器

javascript - 请求的资源上不存在 'Access-Control-Allow-Origin' header 。因此不允许访问 Origin 'http://127.0.0.1:1111'

java - 可以从 gmail 发送电子邮件,但不能从 yahoo 发送电子邮件

java - 为单个 Elastic Beanstalk TOMCAT 实例配置 SSL 证书

redirect - 使用 Apache 2.x 和 Tomcat 6.x 将子域映射到 Servlet 上下文