我目前正在使用 Spring 和 Shiro 开发应用程序。我正在部署到 Tomcat 7,在生产环境中我使用 nginx 作为反向代理。除了在通过 nginx 代理访问应用程序时将 jsessionid
添加到每个 URL 之外,一切都运行顺利(很好)。
当我使用以下 nginx 配置时:
server {
server_name example.com www.example.com;
listen 80;
location /myapp {
proxy_pass http://localhost:8080;
}
}
我通过 www.example.com/myapp 访问该应用程序,一切正常 - URL 中没有 jsessionid
当我使用以下配置时:
server {
server_name sub.example.com www.sub.example.com
listen 80;
location / {
proxy_pass http://localhost:8080/myapp/;
}
我通过 www.sub.example.com 访问应用程序,然后我看到 jsessionid
添加到每个 URL(即使在成功登录后)。
我发现了类似的线程,建议将以下内容添加到 web.xml 中:
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
有效 - 好吧,jsessionid
已删除,但我无法进行身份验证,这让我认为 nginx 中存在 cookie 配置问题,有什么建议吗?
EDIT//: 找到解决方案,只需要在nginx配置中添加以下内容:
proxy_cookie_path /myapp/ /;
最佳答案
特别是对于 Shiro,我在我们的应用程序中使用以下内容解决了这个问题 - 您需要添加
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, ShiroHttpServletRequest.COOKIE_SESSION_ID_SOURCE);
在客户端创建 JSESSIONID cookie 的请求中。基本上告诉 shiro 使用 cookie 源而不是 urlrewriting 来获取 sessionids
以下不适用于 Shiro 的 DefaultWebSessionManager。它只适用于 ServletContainerSessionManager
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
关于spring - Shiro,Spring 应用程序将 jsessionid 附加到每个 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26508492/