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

上文中详细描述了问题的表现情况,由于这个特性严重影响到目前为公司设计的一套前台统一认证方案,因此不得不特别关注。好在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.

看代码:
[code] String id = oldId;

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

SessionImpl session = create(id, now, true);[/code]


我们关注! 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));
再看
[code]
/**
* 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));
}[/code]
并且可以看到默认值为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,问题解决
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值