@override
public httpsessionwrapper getsession(boolean create) {
// 从当前请求线程中获取 session
httpsessionwrapper currentsession = getcurrentsession();
// 如果有直接返回
if (currentsession != null) {
return currentsession;
}
// 从请求中获取 session,这里面会涉及到从缓存中拿session的过程
s requestedsession = getrequestedsession();
if (requestedsession != null) {
// 无效的会话id(不支持的会话存储库)请求属性名称。
// 这里看下当前的sessionid是否有效
if (getattribute(invalid_session_id_attr) == null) {
// 设置当前session的最后访问时间,用于延迟session的有效期
requestedsession.setlastaccessedtime(instant.now());
// 将requestedsessionidvalid置为true
this.requestedsessionidvalid = true;
// 包装session
currentsession = new httpsessionwrapper(requestedsession, getservletcontext());
// 不是新的session,如果是新的session则需要改变sessionid
currentsession.setnew(false);
// 将session设置到当前请求上下文
setcurrentsession(currentsession);
// 返回session
return currentsession;
}
}
else {
// 这里处理的是无效的sessionid的情况,但是当前请求线程 session有效
if (session_logger.isdebugenabled()) {
session_logger.debug(
"no session found by id: caching result for getsession(false) for this httpservletrequest.");
}
// 将invalidsessionid置为true
setattribute(invalid_session_id_attr, "true");
}
// 是否需要创建新的session
if (!create) {
return null;
}
if (session_logger.isdebugenabled()) {
session_logger.debug(
"a new session was created. to help you troubleshoot where the session was created we provided a stacktrace (this is not an error). you can prevent this from appearing by disabling debug logging for "
+ session_logger_name,
new runtimeexception(
"for debugging purposes only (not an error)"));
}
// 创建新的session
s session = sessionrepositoryfilter.this.sessionrepository.createsession();
// 设置最后访问时间,也就是指定了当前session的有效期限
session.setlastaccessedtime(instant.now());
// 包装下当前session
currentsession = new httpsessionwrapper(session, getservletcontext());
//设置到当前请求线程
setcurrentsession(currentsession);
return currentsession;
}