shiroセッションの原理分析
1、どこからセッションを取得するか要求するたびにセッションを取得しようとする
取得プロセス:DefaultWebSessionManager-->getReferencedSessionId-->getSessionIdCookieValue->simpleCookie(JESSIONID)
投げ出し問題:なぜクッキーからsessionidを取得できるのか、3点目の初期化を見てください
セッションが空でない場合は、CachingSessionDAOからセッションを直接取得します.
この原理を熟知すれば、クッキーに依存することなく、DefaultWebSessionManagerを継承してgetSessionIdロジックをカスタマイズして無状態のセッションを実現することができます.--一般的な企業内部システムでない限り、生産環境にも使用する必要があります.
2.セッションの作成の最初のステップ:AbstractShiroFilterパッケージservletRequestをShiroHttpServertRequestにする
ステップ2:requestが最初に呼び出されました.getsession()はsessionを作成します.セッションウェアハウスに保存(通常はCachingSessionDAOをカスタマイズしてredisに保存)ShiroHttpServeretRequestに
3.セッションの作成後に行う初期化操作:AbstractNativeSessionManager、DefaultWebSessionManager
注意:isSessionIdCookieEnabled()スイッチは、sessionidをクッキーに保存するかどうかを決定します.
ソース:
取得プロセス:DefaultWebSessionManager-->getReferencedSessionId-->getSessionIdCookieValue->simpleCookie(JESSIONID)
投げ出し問題:なぜクッキーからsessionidを取得できるのか、3点目の初期化を見てください
セッションが空でない場合は、CachingSessionDAOからセッションを直接取得します.
この原理を熟知すれば、クッキーに依存することなく、DefaultWebSessionManagerを継承してgetSessionIdロジックをカスタマイズして無状態のセッションを実現することができます.--一般的な企業内部システムでない限り、生産環境にも使用する必要があります.
2.セッションの作成の最初のステップ:AbstractShiroFilterパッケージservletRequestをShiroHttpServertRequestにする
protected ServletRequest prepareServletRequest(ServletRequest request, ServletResponse response, FilterChain chain) {
ServletRequest toUse = request;
if (request instanceof HttpServletRequest) {
HttpServletRequest http = (HttpServletRequest)request;
toUse = this.wrapServletRequest(http);
}
return toUse;
}
ステップ2:requestが最初に呼び出されました.getsession()はsessionを作成します.セッションウェアハウスに保存(通常はCachingSessionDAOをカスタマイズしてredisに保存)ShiroHttpServeretRequestに
public HttpSession getSession() {
return this.getSession(true);
}
3.セッションの作成後に行う初期化操作:AbstractNativeSessionManager、DefaultWebSessionManager
注意:isSessionIdCookieEnabled()スイッチは、sessionidをクッキーに保存するかどうかを決定します.
ソース:
public Session start(SessionContext context) {
Session session = this.createSession(context);
this.applyGlobalSessionTimeout(session);
this.onStart(session, context);
this.notifyStart(session);
return this.createExposedSession(session, context);
}
protected void onStart(Session session, SessionContext context) {
super.onStart(session, context);
if (!WebUtils.isHttp(context)) {
log.debug("SessionContext argument is not HTTP compatible or does not have an HTTP request/response pair. No session ID cookie will be set.");
} else {
HttpServletRequest request = WebUtils.getHttpRequest(context);
HttpServletResponse response = WebUtils.getHttpResponse(context);
if (this.isSessionIdCookieEnabled()) {
Serializable sessionId = session.getId();
this.storeSessionId(sessionId, request, response);
} else {
log.debug("Session ID cookie is disabled. No cookie has been set for new session with id {}", session.getId());
}
request.removeAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE);
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_IS_NEW, Boolean.TRUE);
}
}