【问题标题】:Shiro, Spring application appending jsessionid to each URLShiro,Spring 应用程序将 jsessionid 附加到每个 URL
【发布时间】:2014-12-17 23:08:37
【问题描述】:

我目前正在使用 Spring 和 Shiro 开发应用程序。我正在部署到 Tomcat 7,在生产中我使用 nginx 作为反向代理。除了jsessionid 在通过 nginx 代理访问应用程序时添加到每个 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 访问该应用程序,然后我看到每个 URL 都添加了jsessionid(即使在成功登录后也是如此)。

我发现了类似的帖子,建议在 web.xml 中添加以下内容:

<session-config>
  <tracking-mode>COOKIE</tracking-mode>
</session-config>

这行得通 - 好吧,jsessionid 已删除但我无法进行身份验证,这让我认为 nginx 中存在 cookie 配置问题,有什么建议吗?

EDIT//:找到了解决办法,只需要在nginx config中添加以下内容:

 proxy_cookie_path /myapp/ /;

【问题讨论】:

  • 您确认这确实是 cookie 问题吗?在生产中直接访问tomcat时可以进行身份​​验证吗?还要检查网络浏览器中 cookie 的值(使用 chrome 控制台或其他东西),它是否被设置并且值不会改变?
  • 直接访问tomcat可以认证没问题。即使使用第一个 nginx 配置访问也可以。

标签: spring nginx proxy shiro jsessionid


【解决方案1】:

对于 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>

【讨论】:

  • 这应该被标记为答案。完全正确!
【解决方案2】:

在 proxy_pass 之后添加以下内容可能会起作用:

    proxy_redirect http://localhost:8080/myapp/ /;

proxy_pass 和proxy_redirect 通常相互补充。见http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect

【讨论】:

    猜你喜欢
    • 2011-02-17
    • 2012-07-28
    • 1970-01-01
    • 2019-05-28
    • 2018-03-25
    • 1970-01-01
    • 2014-11-16
    • 2011-06-08
    • 2014-01-17
    相关资源
    最近更新 更多