Shiro的校驗Session是否過時處理的過程

首先開啓定時掃描活躍的session進行校驗web

<!-- shiro會話管理 -->
    <!-- 即用戶登陸後就是一次會話,在沒有退出以前,它的全部信息都在會話中;會話能夠是普通 JavaSE 環境的,也能夠是如 Web 環境的 -->
    <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
        <property name="cacheManager" ref="redisCacheManager"/>
        <property name="sessionDAO" ref="redisSessionDAO"/>
        <property name="sessionIdCookie" ref="simpleCookie"/>
        <!-- 全局的會話信息時間,,單位爲毫秒  -->
        <property name="globalSessionTimeout" value="60000"/>
        <!-- 檢測掃描信息時間間隔,單位爲毫秒-->
        <property name="sessionValidationInterval" value="60000"/>
        <!-- 是否開啓掃描 -->
        <property name="sessionValidationSchedulerEnabled" value="true"/>
        <!-- 去掉URL中的JSESSIONID -->
        <property name="sessionIdUrlRewritingEnabled" value="false"/>
    </bean>

而後看源代碼redis

AbstractValidatingSessionManager類中的validateSessions()apache

public void validateSessions() {
        if (log.isInfoEnabled()) {
            log.info("Validating all active sessions...");
        }

        int invalidCount = 0;

        Collection<Session> activeSessions = getActiveSessions();

        if (activeSessions != null && !activeSessions.isEmpty()) {
            for (Session s : activeSessions) {
                try {
                    //simulate a lookup key to satisfy the method signature.
                    //this could probably stand to be cleaned up in future versions:
                    SessionKey key = new DefaultSessionKey(s.getId());
                    validate(s, key);
                } catch (InvalidSessionException e) {
                    if (log.isDebugEnabled()) {
                        boolean expired = (e instanceof ExpiredSessionException);
                        String msg = "Invalidated session with id [" + s.getId() + "]" +
                                (expired ? " (expired)" : " (stopped)");
                        log.debug(msg);
                    }
                    invalidCount++;
                }
            }
        }

        if (log.isInfoEnabled()) {
            String msg = "Finished session validation.";
            if (invalidCount > 0) {
                msg += "  [" + invalidCount + "] sessions were stopped.";
            } else {
                msg += "  No sessions were stopped.";
            }
            log.info(msg);
        }
    }

作校驗的方法是validate(s, key);session

protected void validate(Session session, SessionKey key) throws InvalidSessionException {
        try {
            doValidate(session);
        } catch (ExpiredSessionException ese) {
            onExpiration(session, ese, key);
            throw ese;
        } catch (InvalidSessionException ise) {
            onInvalidation(session, ise, key);
            throw ise;
        }
    }

validate(session, key)說明:
AbstractValidatingSessionManager.validate(Session session, SessionKey key)方法中,若是是session有效期過時了,這會調用onExpiration(Session s, ExpiredSessionException ese, SessionKey key)方法,該方法中onExpiration(s)調用ShiroCache類,刪除shiro_redis_session:shiro-activeSessionCache:的session信息;afterExpired(s)調用RedisSessionDAO類,刪除shiro_redis_session:的session信息this

相關文章
相關標籤/搜索