diff --git a/spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisOperationsSessionRepositoryITests.java b/spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisOperationsSessionRepositoryITests.java index a12b0952..68839065 100644 --- a/spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisOperationsSessionRepositoryITests.java +++ b/spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisOperationsSessionRepositoryITests.java @@ -89,7 +89,7 @@ public class RedisOperationsSessionRepositoryITests extends AbstractRedisITests public void saves() throws InterruptedException { String username = "saves-" + System.currentTimeMillis(); - String usernameSessionKey = "spring:session:RedisOperationsSessionRepositoryITests:index:" + String usernameSessionKey = "RedisOperationsSessionRepositoryITests:index:" + INDEX_NAME + ":" + username; RedisSession toSave = this.repository.createSession(); @@ -188,7 +188,7 @@ public class RedisOperationsSessionRepositoryITests extends AbstractRedisITests this.repository.save(toSave); - String body = "spring:session:RedisOperationsSessionRepositoryITests:sessions:expires:" + String body = "RedisOperationsSessionRepositoryITests:sessions:expires:" + toSave.getId(); String channel = ":expired"; DefaultMessage message = new DefaultMessage(channel.getBytes("UTF-8"), @@ -356,7 +356,7 @@ public class RedisOperationsSessionRepositoryITests extends AbstractRedisITests this.repository.save(toSave); - String body = "spring:session:RedisOperationsSessionRepositoryITests:sessions:expires:" + String body = "RedisOperationsSessionRepositoryITests:sessions:expires:" + toSave.getId(); String channel = ":expired"; DefaultMessage message = new DefaultMessage(channel.getBytes("UTF-8"), diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisOperationsSessionRepository.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisOperationsSessionRepository.java index a90c22dc..a33bdc7b 100644 --- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisOperationsSessionRepository.java +++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisOperationsSessionRepository.java @@ -93,7 +93,7 @@ public class ReactiveRedisOperationsSessionRepository implements public void setRedisKeyNamespace(String namespace) { Assert.hasText(namespace, "namespace cannot be null or empty"); - this.keyPrefix = DEFAULT_SPRING_SESSION_REDIS_PREFIX + namespace.trim() + ":"; + this.keyPrefix = namespace.trim() + ":"; } /** diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisOperationsSessionRepository.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisOperationsSessionRepository.java index f4e5e155..cc8b970e 100644 --- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisOperationsSessionRepository.java +++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisOperationsSessionRepository.java @@ -588,7 +588,8 @@ public class RedisOperationsSessionRepository implements } public void setRedisKeyNamespace(String namespace) { - this.keyPrefix = DEFAULT_SPRING_SESSION_REDIS_PREFIX + namespace + ":"; + Assert.hasText(namespace, "namespace cannot be null or empty"); + this.keyPrefix = namespace.trim() + ":"; } /** diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisOperationsSessionRepositoryTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisOperationsSessionRepositoryTests.java index b34692cc..1ad4e89f 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisOperationsSessionRepositoryTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisOperationsSessionRepositoryTests.java @@ -85,9 +85,7 @@ public class ReactiveRedisOperationsSessionRepositoryTests { public void customRedisKeyNamespace() { this.repository.setRedisKeyNamespace("test"); - assertThat(ReflectionTestUtils.getField(this.repository, "keyPrefix")).isEqualTo( - ReactiveRedisOperationsSessionRepository.DEFAULT_SPRING_SESSION_REDIS_PREFIX - + "test:"); + assertThat(ReflectionTestUtils.getField(this.repository, "keyPrefix")).isEqualTo("test:"); } @Test diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisOperationsSessionRepositoryTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisOperationsSessionRepositoryTests.java index 403a5cbe..e21c8176 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisOperationsSessionRepositoryTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisOperationsSessionRepositoryTests.java @@ -16,6 +16,7 @@ package org.springframework.session.data.redis; +import java.text.MessageFormat; import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; @@ -709,8 +710,62 @@ public class RedisOperationsSessionRepositoryTests { this.redisRepository.setRedisFlushMode(null); } + + @Test + public void testDefaultRedisNamespace() { + RedisSession session = this.redisRepository.new RedisSession(new MapSession()); + session.setMaxInactiveInterval(Duration.ZERO); + given(this.redisOperations.boundHashOps(anyString())) + .willReturn(this.boundHashOperations); + given(this.redisOperations.boundSetOps(anyString())) + .willReturn(this.boundSetOperations); + + this.redisRepository.save(session); + + String id = session.getId(); + verify(this.redisOperations, atLeastOnce()) + .delete(getKey("expires:" + id)); + verify(this.redisOperations, never()).boundValueOps(getKey("expires:" + id)); + } + + @Test + public void testRedisNamespaceChange() { + String namespace = "foo:bar"; + this.redisRepository.setRedisKeyNamespace(namespace); + RedisSession session = this.redisRepository.new RedisSession(new MapSession()); + session.setMaxInactiveInterval(Duration.ZERO); + given(this.redisOperations.boundHashOps(anyString())) + .willReturn(this.boundHashOperations); + given(this.redisOperations.boundSetOps(anyString())) + .willReturn(this.boundSetOperations); + + this.redisRepository.save(session); + + String id = session.getId(); + verify(this.redisOperations, atLeastOnce()) + .delete(getKeyWithinNamespace(namespace, "expires:" + id)); + verify(this.redisOperations, never()).boundValueOps(getKeyWithinNamespace(namespace, "expires:" + id)); + } + + @Test(expected = IllegalArgumentException.class) + public void testLaunchExceptionOnNullNamespace() { + this.redisRepository.setRedisKeyNamespace(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testLaunchExceptionOnEmptyNamespace() { + this.redisRepository.setRedisKeyNamespace(""); + } + private String getKey(String id) { - return "spring:session:sessions:" + id; + return getKeyWithinNamespace("spring:session", id); + } + + private String getKeyWithinNamespace(String prefix, String id) { + if (prefix.endsWith(":")) { + prefix = prefix.substring(0, prefix.length()); + } + return MessageFormat.format("{0}:sessions:{1}", prefix, id); } private Map map(Object... objects) { diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfigurationTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfigurationTests.java index c4116a61..9b06ecb2 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfigurationTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfigurationTests.java @@ -82,7 +82,7 @@ public class RedisWebSessionConfigurationTests { .getBean(ReactiveRedisOperationsSessionRepository.class); assertThat(repository).isNotNull(); assertThat(ReflectionTestUtils.getField(repository, "keyPrefix")) - .isEqualTo("spring:session:" + REDIS_NAMESPACE + ":"); + .isEqualTo(REDIS_NAMESPACE + ":"); } @Test