`

resin的session id reuse特性(2)--分析问题

    博客分类:
  • web
Web 
阅读更多
    上文中详细描述了问题的表现情况,由于这个特性严重影响到目前为公司设计的一套前台统一认证方案,因此不得不特别关注。好在resin的源代码是公开的,直接从resin的官网将resin的源代码拿下来,看resin到底是如何处理的。

首先找到com.caucho.server.http.HttpRequest,发现是extends AbstractHttpRequest
在AbstractHttpRequest中找到方法
  public HttpSession getSession(boolean create)

        发现调用
_session = createSession(create, hasOldSession);
    这里有一段注释解释了重用jsessonid的行为:
        Must accept old ids because different applications in the same server must share the same cookie
        But, if the session group doesn't match, then create a new session.

  在createSession方法中找到
  manager.createSession(id, now, this, _isSessionIdFromCookie);


  打开com.caucho.server.session.SessionManager的createSession()方法,
  发现有注释: @param oldId the id passed to the request.  Reuse if possible.

  看代码:
  String id = oldId;

    if (id == null || id.length() < 4 ||
        ! isInSessionGroup(id) || ! reuseSessionId(fromCookie)) {
      id = createSessionId(request, true);
    }

    SessionImpl session = create(id, now, true);



        我们关注! reuseSessionId(fromCookie)这句,打开看函数
   /**
   * True if the server should reuse the current session id if the
   * session doesn't exist.
   */
  public boolean reuseSessionId(boolean fromCookie)
  {
    int reuseSessionId = _reuseSessionId;
  
    return reuseSessionId == TRUE || fromCookie && reuseSessionId == COOKIE;
  }

  注:非常不喜欢resin的这种代码风格,一般我宁可写成下面的这种,看代码时容易理解
  return (reuseSessionId == TRUE) || (fromCookie && (reuseSessionId == COOKIE));
        再看
/**
   * True if the server should reuse the current session id if the
   * session doesn't exist.
   */
  public void setReuseSessionId(String reuse)
    throws ConfigException
  {
    if (reuse == null)
      _reuseSessionId = COOKIE;
    else if (reuse.equalsIgnoreCase("true") ||
         reuse.equalsIgnoreCase("yes") ||
         reuse.equalsIgnoreCase("cookie"))
      _reuseSessionId = COOKIE;
    else if (reuse.equalsIgnoreCase("false") || reuse.equalsIgnoreCase("no"))
      _reuseSessionId = FALSE;
    else if (reuse.equalsIgnoreCase("all"))
      _reuseSessionId = TRUE;
    else
      throw new ConfigException(L.l("'{0}' is an invalid value for reuse-session-id.  'true' or 'false' are the allowed values.",
                    reuse));
  }

      并且可以看到默认值为COOKIE
private int _reuseSessionId = COOKIE;
  
    翻一下resin的文档,可以发现在resin.conf的<session-config>有reuse-session-id这个配置项,resin文档的说明如下:
  
    "reuse-session-id defaults to true so that Resin can share the session id amongst different web-apps."
  
    默认情况下reuse-session-id设置为true,setReuseSessionId("true")会使得_reuseSessionId=COOKIE,而reuseSessionId()方法中的表达式可以简化:
    (reuseSessionId == TRUE) || (fromCookie && (reuseSessionId == COOKIE));
    -->  (COOKIE == TRUE) || (fromCookie && (COOKIE == COOKIE))
    --> (false) || (fromCookie && true)
    --> fromCookie
    因此默认情况下jsessionid用cookie传递就可以做到重用,否则就要生成新的jsessionid.
  
    按照这个思路,只要将reuse-session-id配置项设置为"all",就可以做到即使使用url rewrite也可以重用jsessionid.
    ok,问题解决
分享到:
评论
1 楼 liguangyi 2008-10-20  
呵呵,解决了我的一个大问题!

相关推荐

Global site tag (gtag.js) - Google Analytics