Skip to content

Commit baaaa8f

Browse files
committed
Treat a SerializationException retrieving a session at though there were no session.
This makes zero-downtime / seamless upgrades possible in more cases instead of requiring manual deletion of cached sessions when e.g. upgrading spring-security across versions that change SpringSecurityCoreVersion.SERIAL_VERSION_ID.
1 parent 58813e9 commit baaaa8f

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2021 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737
import org.springframework.data.redis.core.RedisOperations;
3838
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
3939
import org.springframework.data.redis.serializer.RedisSerializer;
40+
import org.springframework.data.redis.serializer.SerializationException;
4041
import org.springframework.data.redis.util.ByteUtils;
4142
import org.springframework.session.DelegatingIndexResolver;
4243
import org.springframework.session.FindByIndexNameSessionRepository;
@@ -454,7 +455,15 @@ public Map<String, RedisSession> findByIndexNameAndIndexValue(String indexName,
454455
* @return the Redis session
455456
*/
456457
private RedisSession getSession(String id, boolean allowExpired) {
457-
Map<Object, Object> entries = getSessionBoundHashOperations(id).entries();
458+
Map<Object, Object> entries;
459+
try {
460+
entries = getSessionBoundHashOperations(id).entries();
461+
}
462+
catch (SerializationException ex) {
463+
// An error deserializing is equivalent to not having any information at all.
464+
logger.warn("exception getting session " + id, ex);
465+
return null;
466+
}
458467
if (entries.isEmpty()) {
459468
return null;
460469
}

spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@
4343
import org.springframework.data.redis.core.RedisOperations;
4444
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
4545
import org.springframework.data.redis.serializer.RedisSerializer;
46+
import org.springframework.data.redis.serializer.SerializationException;
4647
import org.springframework.session.FindByIndexNameSessionRepository;
4748
import org.springframework.session.FlushMode;
4849
import org.springframework.session.MapSession;
@@ -394,6 +395,15 @@ void getSessionExpired() {
394395
assertThat(this.redisRepository.findById(expiredId)).isNull();
395396
}
396397

398+
@Test
399+
void getSessionIncompatible() {
400+
String incompatibleId = "incompatible";
401+
402+
given(this.redisOperations.boundHashOps(getKey(incompatibleId))).willReturn(this.boundHashOperations);
403+
given(this.boundHashOperations.entries()).willThrow(new SerializationException("arbitrary exception"));
404+
assertThat(this.redisRepository.findById(incompatibleId)).isNull();
405+
}
406+
397407
@Test
398408
@SuppressWarnings("unchecked")
399409
void findByPrincipalNameExpired() {

0 commit comments

Comments
 (0)