spring-session icon indicating copy to clipboard operation
spring-session copied to clipboard

maxInactiveInterval < 0 but redis session still expired

Open hdsuperman opened this issue 9 months ago • 0 comments

Source:

https://github.com/spring-projects/spring-session/blob/2353d8b3cec4f0ed104c45f10d01acf278fe1dad/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java#L930-L962

Bug

The following code will always execute, Regardless of whether maxInactiveInterval is less than 0

RedisIndexedSessionRepository.this.sessionRedisOperations.boundHashOps(getSessionKey(getId()))
				.expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);

This code in createShadowKey will always be skipped:

if (sessionExpireInSeconds < 0) {
	...
	RedisIndexedSessionRepository.this.sessionRedisOperations.boundHashOps(getSessionKey(getId()))
		.persist();
}

How to fix

long sessionExpireInSeconds = getMaxInactiveInterval().getSeconds();

createShadowKey(sessionExpireInSeconds);

if (sessionExpireInSeconds > 0) {
    long fiveMinutesAfterExpires = sessionExpireInSeconds + TimeUnit.MINUTES.toSeconds(5);
    RedisIndexedSessionRepository.this.sessionRedisOperations.boundHashOps(getSessionKey(getId()))
	.expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);
}

RedisIndexedSessionRepository.this.expirationStore.save(this);
this.delta = new HashMap<>(this.delta.size());

hdsuperman avatar Mar 25 '25 14:03 hdsuperman