【原創】大叔問題定位分享(35)spring中session失效時間


spring項目中將sessionid對應的cookie過期時間設置很長,但是實際session還是在半個小時后失效,跟了一下代碼,spring中session實現接口為

org.springframework.session.SessionRepository

public interface SessionRepository<S extends Session> {
    S createSession();

    void save(S var1);

    S findById(String var1);

    void deleteById(String var1);
}

這個接口有兩個實現類:

MapSessionRepository
RedisOperationsSessionRepository

單機環境使用前者,分布式環境使用后者,來看后者代碼:

org.springframework.session.data.redis.RedisOperationsSessionRepository

    public RedisOperationsSessionRepository.RedisSession createSession() {
        RedisOperationsSessionRepository.RedisSession redisSession = new RedisOperationsSessionRepository.RedisSession();
        if(this.defaultMaxInactiveInterval != null) {
            redisSession.setMaxInactiveInterval(Duration.ofSeconds((long)this.defaultMaxInactiveInterval.intValue()));
        }

        return redisSession;
    }

org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession

        RedisSession() {
            this(new MapSession());
            this.delta.put("creationTime", Long.valueOf(this.getCreationTime().toEpochMilli()));
            this.delta.put("maxInactiveInterval", Integer.valueOf((int)this.getMaxInactiveInterval().getSeconds()));
            this.delta.put("lastAccessedTime", Long.valueOf(this.getLastAccessedTime().toEpochMilli()));
            this.isNew = true;
            this.flushImmediateIfNecessary();
        }

        public boolean isExpired() {
            return this.cached.isExpired();
        }

org.springframework.session.MapSession

    public boolean isExpired() {
        return this.isExpired(Instant.now());
    }

    boolean isExpired(Instant now) {
        return this.maxInactiveInterval.isNegative()?false:now.minus(this.maxInactiveInterval).compareTo(this.lastAccessedTime) >= 0;
    }

 

可見是在創建session的時候設置兩個時間,

lastAccessedTime
maxInactiveInterval

如果 當前時間 - maxInactiveInterval > lastAccessedTime 就會認為session過期,設置的方法:

@EnableRedisHttpSession(maxInactiveIntervalInSeconds=2000) 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM