Rename SessionIdGenerationStrategy to SessionIdGenerator

Closes gh-2391
This commit is contained in:
Marcus Da Coregio
2023-08-03 13:44:42 -03:00
parent b2615c2010
commit 2d4233fcfd
38 changed files with 269 additions and 286 deletions

View File

@@ -31,8 +31,8 @@ import org.springframework.session.MapSession;
import org.springframework.session.ReactiveSessionRepository;
import org.springframework.session.SaveMode;
import org.springframework.session.Session;
import org.springframework.session.SessionIdGenerationStrategy;
import org.springframework.session.UuidSessionIdGenerationStrategy;
import org.springframework.session.SessionIdGenerator;
import org.springframework.session.UuidSessionIdGenerator;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -63,7 +63,7 @@ public class ReactiveRedisSessionRepository
private SaveMode saveMode = SaveMode.ON_SET_ATTRIBUTE;
private SessionIdGenerationStrategy sessionIdGenerationStrategy = UuidSessionIdGenerationStrategy.getInstance();
private SessionIdGenerator sessionIdGenerator = UuidSessionIdGenerator.getInstance();
/**
* Create a new {@link ReactiveRedisSessionRepository} instance.
@@ -124,7 +124,7 @@ public class ReactiveRedisSessionRepository
@Override
public Mono<RedisSession> createSession() {
return Mono.defer(() -> {
MapSession cached = new MapSession(this.sessionIdGenerationStrategy);
MapSession cached = new MapSession(this.sessionIdGenerator);
cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
RedisSession session = new RedisSession(cached, true);
return Mono.just(session);
@@ -172,13 +172,13 @@ public class ReactiveRedisSessionRepository
}
/**
* Set the {@link SessionIdGenerationStrategy} to use to generate session ids.
* @param sessionIdGenerationStrategy the {@link SessionIdGenerationStrategy} to use
* Set the {@link SessionIdGenerator} to use to generate session ids.
* @param sessionIdGenerator the {@link SessionIdGenerator} to use
* @since 3.2
*/
public void setSessionIdGenerationStrategy(SessionIdGenerationStrategy sessionIdGenerationStrategy) {
Assert.notNull(sessionIdGenerationStrategy, "sessionIdGenerationStrategy cannot be null");
this.sessionIdGenerationStrategy = sessionIdGenerationStrategy;
public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) {
Assert.notNull(sessionIdGenerator, "sessionIdGenerator cannot be null");
this.sessionIdGenerator = sessionIdGenerator;
}
/**
@@ -220,7 +220,7 @@ public class ReactiveRedisSessionRepository
@Override
public String changeSessionId() {
String newSessionId = ReactiveRedisSessionRepository.this.sessionIdGenerationStrategy.generate();
String newSessionId = ReactiveRedisSessionRepository.this.sessionIdGenerator.generate();
this.cached.setId(newSessionId);
return newSessionId;
}

View File

@@ -52,8 +52,8 @@ import org.springframework.session.MapSession;
import org.springframework.session.PrincipalNameIndexResolver;
import org.springframework.session.SaveMode;
import org.springframework.session.Session;
import org.springframework.session.SessionIdGenerationStrategy;
import org.springframework.session.UuidSessionIdGenerationStrategy;
import org.springframework.session.SessionIdGenerator;
import org.springframework.session.UuidSessionIdGenerator;
import org.springframework.session.events.SessionCreatedEvent;
import org.springframework.session.events.SessionDeletedEvent;
import org.springframework.session.events.SessionDestroyedEvent;
@@ -324,7 +324,7 @@ public class RedisIndexedSessionRepository
private ThreadPoolTaskScheduler taskScheduler;
private SessionIdGenerationStrategy sessionIdGenerationStrategy = UuidSessionIdGenerationStrategy.getInstance();
private SessionIdGenerator sessionIdGenerator = UuidSessionIdGenerator.getInstance();
/**
* Creates a new instance. For an example, refer to the class level javadoc.
@@ -551,7 +551,7 @@ public class RedisIndexedSessionRepository
@Override
public RedisSession createSession() {
MapSession cached = new MapSession(this.sessionIdGenerationStrategy);
MapSession cached = new MapSession(this.sessionIdGenerator);
cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
RedisSession session = new RedisSession(cached, true);
session.flushImmediateIfNecessary();
@@ -721,13 +721,13 @@ public class RedisIndexedSessionRepository
}
/**
* Set the {@link SessionIdGenerationStrategy} to use to generate session ids.
* @param sessionIdGenerationStrategy the {@link SessionIdGenerationStrategy} to use
* Set the {@link SessionIdGenerator} to use to generate session ids.
* @param sessionIdGenerator the {@link SessionIdGenerator} to use
* @since 3.2
*/
public void setSessionIdGenerationStrategy(SessionIdGenerationStrategy sessionIdGenerationStrategy) {
Assert.notNull(sessionIdGenerationStrategy, "sessionIdGenerationStrategy cannot be null");
this.sessionIdGenerationStrategy = sessionIdGenerationStrategy;
public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) {
Assert.notNull(sessionIdGenerator, "sessionIdGenerator cannot be null");
this.sessionIdGenerator = sessionIdGenerator;
}
/**
@@ -794,7 +794,7 @@ public class RedisIndexedSessionRepository
@Override
public String changeSessionId() {
String newSessionId = RedisIndexedSessionRepository.this.sessionIdGenerationStrategy.generate();
String newSessionId = RedisIndexedSessionRepository.this.sessionIdGenerator.generate();
this.cached.setId(newSessionId);
return newSessionId;
}

View File

@@ -27,9 +27,9 @@ import org.springframework.session.FlushMode;
import org.springframework.session.MapSession;
import org.springframework.session.SaveMode;
import org.springframework.session.Session;
import org.springframework.session.SessionIdGenerationStrategy;
import org.springframework.session.SessionIdGenerator;
import org.springframework.session.SessionRepository;
import org.springframework.session.UuidSessionIdGenerationStrategy;
import org.springframework.session.UuidSessionIdGenerator;
import org.springframework.util.Assert;
/**
@@ -58,7 +58,7 @@ public class RedisSessionRepository implements SessionRepository<RedisSessionRep
private SaveMode saveMode = SaveMode.ON_SET_ATTRIBUTE;
private SessionIdGenerationStrategy sessionIdGenerationStrategy = UuidSessionIdGenerationStrategy.getInstance();
private SessionIdGenerator sessionIdGenerator = UuidSessionIdGenerator.getInstance();
/**
* Create a new {@link RedisSessionRepository} instance.
@@ -110,7 +110,7 @@ public class RedisSessionRepository implements SessionRepository<RedisSessionRep
@Override
public RedisSession createSession() {
MapSession cached = new MapSession(this.sessionIdGenerationStrategy);
MapSession cached = new MapSession(this.sessionIdGenerator);
cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
RedisSession session = new RedisSession(cached, true);
session.flushIfRequired();
@@ -167,13 +167,13 @@ public class RedisSessionRepository implements SessionRepository<RedisSessionRep
}
/**
* Set the {@link SessionIdGenerationStrategy} to use to generate session ids.
* @param sessionIdGenerationStrategy the {@link SessionIdGenerationStrategy} to use
* Set the {@link SessionIdGenerator} to use to generate session ids.
* @param sessionIdGenerator the {@link SessionIdGenerator} to use
* @since 3.2
*/
public void setSessionIdGenerationStrategy(SessionIdGenerationStrategy sessionIdGenerationStrategy) {
Assert.notNull(sessionIdGenerationStrategy, "sessionIdGenerationStrategy cannot be null");
this.sessionIdGenerationStrategy = sessionIdGenerationStrategy;
public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) {
Assert.notNull(sessionIdGenerator, "sessionIdGenerator cannot be null");
this.sessionIdGenerator = sessionIdGenerator;
}
/**
@@ -212,7 +212,7 @@ public class RedisSessionRepository implements SessionRepository<RedisSessionRep
@Override
public String changeSessionId() {
String newSessionId = RedisSessionRepository.this.sessionIdGenerationStrategy.generate();
String newSessionId = RedisSessionRepository.this.sessionIdGenerator.generate();
this.cached.setId(newSessionId);
return newSessionId;
}

View File

@@ -28,8 +28,8 @@ import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.session.SessionIdGenerationStrategy;
import org.springframework.session.UuidSessionIdGenerationStrategy;
import org.springframework.session.SessionIdGenerator;
import org.springframework.session.UuidSessionIdGenerator;
import org.springframework.session.data.redis.RedisSessionRepository;
import org.springframework.session.web.http.SessionRepositoryFilter;
import org.springframework.util.StringUtils;
@@ -52,7 +52,7 @@ public class RedisHttpSessionConfiguration extends AbstractRedisHttpSessionConfi
private StringValueResolver embeddedValueResolver;
private SessionIdGenerationStrategy sessionIdGenerationStrategy = UuidSessionIdGenerationStrategy.getInstance();
private SessionIdGenerator sessionIdGenerator = UuidSessionIdGenerator.getInstance();
@Bean
@Override
@@ -65,7 +65,7 @@ public class RedisHttpSessionConfiguration extends AbstractRedisHttpSessionConfi
}
sessionRepository.setFlushMode(getFlushMode());
sessionRepository.setSaveMode(getSaveMode());
sessionRepository.setSessionIdGenerationStrategy(this.sessionIdGenerationStrategy);
sessionRepository.setSessionIdGenerator(this.sessionIdGenerator);
getSessionRepositoryCustomizers()
.forEach((sessionRepositoryCustomizer) -> sessionRepositoryCustomizer.customize(sessionRepository));
return sessionRepository;
@@ -94,8 +94,8 @@ public class RedisHttpSessionConfiguration extends AbstractRedisHttpSessionConfi
}
@Autowired(required = false)
public void setSessionIdGenerationStrategy(SessionIdGenerationStrategy sessionIdGenerationStrategy) {
this.sessionIdGenerationStrategy = sessionIdGenerationStrategy;
public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) {
this.sessionIdGenerator = sessionIdGenerator;
}
}

View File

@@ -44,8 +44,8 @@ import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.session.IndexResolver;
import org.springframework.session.Session;
import org.springframework.session.SessionIdGenerationStrategy;
import org.springframework.session.UuidSessionIdGenerationStrategy;
import org.springframework.session.SessionIdGenerator;
import org.springframework.session.UuidSessionIdGenerator;
import org.springframework.session.data.redis.RedisIndexedSessionRepository;
import org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
@@ -82,7 +82,7 @@ public class RedisIndexedHttpSessionConfiguration
private StringValueResolver embeddedValueResolver;
private SessionIdGenerationStrategy sessionIdGenerationStrategy = UuidSessionIdGenerationStrategy.getInstance();
private SessionIdGenerator sessionIdGenerator = UuidSessionIdGenerator.getInstance();
@Bean
@Override
@@ -105,7 +105,7 @@ public class RedisIndexedHttpSessionConfiguration
sessionRepository.setCleanupCron(this.cleanupCron);
int database = resolveDatabase();
sessionRepository.setDatabase(database);
sessionRepository.setSessionIdGenerationStrategy(this.sessionIdGenerationStrategy);
sessionRepository.setSessionIdGenerator(this.sessionIdGenerator);
getSessionRepositoryCustomizers()
.forEach((sessionRepositoryCustomizer) -> sessionRepositoryCustomizer.customize(sessionRepository));
return sessionRepository;
@@ -210,8 +210,8 @@ public class RedisIndexedHttpSessionConfiguration
}
@Autowired(required = false)
public void setSessionIdGenerationStrategy(SessionIdGenerationStrategy sessionIdGenerationStrategy) {
this.sessionIdGenerationStrategy = sessionIdGenerationStrategy;
public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) {
this.sessionIdGenerator = sessionIdGenerator;
}
/**

View File

@@ -39,8 +39,8 @@ import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.MapSession;
import org.springframework.session.SaveMode;
import org.springframework.session.SessionIdGenerationStrategy;
import org.springframework.session.UuidSessionIdGenerationStrategy;
import org.springframework.session.SessionIdGenerator;
import org.springframework.session.UuidSessionIdGenerator;
import org.springframework.session.config.ReactiveSessionRepositoryCustomizer;
import org.springframework.session.config.annotation.web.server.SpringWebSessionConfiguration;
import org.springframework.session.data.redis.ReactiveRedisSessionRepository;
@@ -79,7 +79,7 @@ public class RedisWebSessionConfiguration implements BeanClassLoaderAware, Embed
private StringValueResolver embeddedValueResolver;
private SessionIdGenerationStrategy sessionIdGenerationStrategy = UuidSessionIdGenerationStrategy.getInstance();
private SessionIdGenerator sessionIdGenerator = UuidSessionIdGenerator.getInstance();
@Bean
public ReactiveRedisSessionRepository sessionRepository() {
@@ -90,7 +90,7 @@ public class RedisWebSessionConfiguration implements BeanClassLoaderAware, Embed
sessionRepository.setRedisKeyNamespace(this.redisNamespace);
}
sessionRepository.setSaveMode(this.saveMode);
sessionRepository.setSessionIdGenerationStrategy(this.sessionIdGenerationStrategy);
sessionRepository.setSessionIdGenerator(this.sessionIdGenerator);
this.sessionRepositoryCustomizers
.forEach((sessionRepositoryCustomizer) -> sessionRepositoryCustomizer.customize(sessionRepository));
return sessionRepository;
@@ -174,8 +174,8 @@ public class RedisWebSessionConfiguration implements BeanClassLoaderAware, Embed
}
@Autowired(required = false)
public void setSessionIdGenerationStrategy(SessionIdGenerationStrategy sessionIdGenerationStrategy) {
this.sessionIdGenerationStrategy = sessionIdGenerationStrategy;
public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) {
this.sessionIdGenerator = sessionIdGenerator;
}
}

View File

@@ -440,7 +440,7 @@ class ReactiveRedisSessionRepositoryTests {
@Test
void createSessionWhenSessionIdGenerationStrategyThenUses() {
this.repository.setSessionIdGenerationStrategy(() -> "test");
this.repository.setSessionIdGenerator(() -> "test");
this.repository.createSession().as(StepVerifier::create).assertNext((redisSession) -> {
assertThat(redisSession.getId()).isEqualTo("test");
@@ -450,14 +450,14 @@ class ReactiveRedisSessionRepositoryTests {
@Test
void setSessionIdGenerationStrategyWhenNullThenThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.repository.setSessionIdGenerationStrategy(null))
.withMessage("sessionIdGenerationStrategy cannot be null");
assertThatIllegalArgumentException().isThrownBy(() -> this.repository.setSessionIdGenerator(null))
.withMessage("sessionIdGenerator cannot be null");
}
@Test
@SuppressWarnings("unchecked")
void findByIdWhenChangeSessionIdThenUsesSessionIdGenerationStrategy() {
this.repository.setSessionIdGenerationStrategy(() -> "changed-session-id");
this.repository.setSessionIdGenerator(() -> "changed-session-id");
given(this.redisOperations.opsForHash()).willReturn(this.hashOperations);
String attribute1 = "attribute1";
String attribute2 = "attribute2";

View File

@@ -912,7 +912,7 @@ class RedisIndexedSessionRepositoryTests {
@Test
void createSessionWhenSessionIdGenerationStrategyThenUses() {
this.redisRepository.setSessionIdGenerationStrategy(() -> "test");
this.redisRepository.setSessionIdGenerator(() -> "test");
RedisSession session = this.redisRepository.createSession();
assertThat(session.getId()).isEqualTo("test");
assertThat(session.changeSessionId()).isEqualTo("test");
@@ -920,13 +920,13 @@ class RedisIndexedSessionRepositoryTests {
@Test
void setSessionIdGenerationStrategyWhenNullThenThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.redisRepository.setSessionIdGenerationStrategy(null))
.withMessage("sessionIdGenerationStrategy cannot be null");
assertThatIllegalArgumentException().isThrownBy(() -> this.redisRepository.setSessionIdGenerator(null))
.withMessage("sessionIdGenerator cannot be null");
}
@Test
void findByIdWhenChangeSessionIdThenUsesSessionIdGenerationStrategy() {
this.redisRepository.setSessionIdGenerationStrategy(() -> "test");
this.redisRepository.setSessionIdGenerator(() -> "test");
String attribute1 = "attribute1";
String attribute2 = "attribute2";
MapSession expected = new MapSession("original");

View File

@@ -375,7 +375,7 @@ class RedisSessionRepositoryTests {
@Test
void createSessionWhenSessionIdGenerationStrategyThenUses() {
this.sessionRepository.setSessionIdGenerationStrategy(() -> "test");
this.sessionRepository.setSessionIdGenerator(() -> "test");
RedisSessionRepository.RedisSession session = this.sessionRepository.createSession();
assertThat(session.getId()).isEqualTo("test");
assertThat(session.changeSessionId()).isEqualTo("test");
@@ -383,14 +383,13 @@ class RedisSessionRepositoryTests {
@Test
void setSessionIdGenerationStrategyWhenNullThenThrowsException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.sessionRepository.setSessionIdGenerationStrategy(null))
.withMessage("sessionIdGenerationStrategy cannot be null");
assertThatIllegalArgumentException().isThrownBy(() -> this.sessionRepository.setSessionIdGenerator(null))
.withMessage("sessionIdGenerator cannot be null");
}
@Test
void findByIdWhenChangeSessionIdThenUsesSessionIdGenerationStrategy() {
this.sessionRepository.setSessionIdGenerationStrategy(() -> "test");
this.sessionRepository.setSessionIdGenerator(() -> "test");
Instant now = Instant.now().truncatedTo(ChronoUnit.MILLIS);
given(this.sessionHashOperations.entries(eq(TEST_SESSION_KEY)))
.willReturn(mapOf(RedisSessionMapper.CREATION_TIME_KEY, Instant.EPOCH.toEpochMilli(),

View File

@@ -39,8 +39,8 @@ import org.springframework.data.redis.core.RedisOperations;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.session.FlushMode;
import org.springframework.session.SaveMode;
import org.springframework.session.SessionIdGenerationStrategy;
import org.springframework.session.UuidSessionIdGenerationStrategy;
import org.springframework.session.SessionIdGenerator;
import org.springframework.session.UuidSessionIdGenerator;
import org.springframework.session.config.SessionRepositoryCustomizer;
import org.springframework.session.data.redis.RedisSessionRepository;
import org.springframework.session.data.redis.config.annotation.SpringSessionRedisConnectionFactory;
@@ -212,16 +212,14 @@ class RedisHttpsSessionConfigurationTests {
void registerWhenSessionIdGenerationStrategyBeanThenUses() {
registerAndRefresh(RedisConfig.class, SessionIdGenerationStrategyConfiguration.class);
RedisSessionRepository sessionRepository = this.context.getBean(RedisSessionRepository.class);
assertThat(sessionRepository).extracting("sessionIdGenerationStrategy")
.isInstanceOf(TestSessionIdGenerationStrategy.class);
assertThat(sessionRepository).extracting("sessionIdGenerator").isInstanceOf(TestSessionIdGenerator.class);
}
@Test
void registerWhenNoSessionIdGenerationStrategyBeanThenDefault() {
registerAndRefresh(RedisConfig.class, DefaultConfiguration.class);
RedisSessionRepository sessionRepository = this.context.getBean(RedisSessionRepository.class);
assertThat(sessionRepository).extracting("sessionIdGenerationStrategy")
.isInstanceOf(UuidSessionIdGenerationStrategy.class);
assertThat(sessionRepository).extracting("sessionIdGenerator").isInstanceOf(UuidSessionIdGenerator.class);
}
private void registerAndRefresh(Class<?>... annotatedClasses) {
@@ -404,8 +402,8 @@ class RedisHttpsSessionConfigurationTests {
static class SessionIdGenerationStrategyConfiguration {
@Bean
SessionIdGenerationStrategy sessionIdGenerationStrategy() {
return new TestSessionIdGenerationStrategy();
SessionIdGenerator sessionIdGenerationStrategy() {
return new TestSessionIdGenerator();
}
}
@@ -416,7 +414,7 @@ class RedisHttpsSessionConfigurationTests {
}
static class TestSessionIdGenerationStrategy implements SessionIdGenerationStrategy {
static class TestSessionIdGenerator implements SessionIdGenerator {
@Override
public String generate() {

View File

@@ -43,8 +43,8 @@ import org.springframework.session.FlushMode;
import org.springframework.session.IndexResolver;
import org.springframework.session.SaveMode;
import org.springframework.session.Session;
import org.springframework.session.SessionIdGenerationStrategy;
import org.springframework.session.UuidSessionIdGenerationStrategy;
import org.springframework.session.SessionIdGenerator;
import org.springframework.session.UuidSessionIdGenerator;
import org.springframework.session.config.SessionRepositoryCustomizer;
import org.springframework.session.data.redis.RedisIndexedSessionRepository;
import org.springframework.session.data.redis.config.annotation.SpringSessionRedisConnectionFactory;
@@ -246,16 +246,14 @@ class RedisIndexedHttpSessionConfigurationTests {
void registerWhenSessionIdGenerationStrategyBeanThenUses() {
registerAndRefresh(RedisConfig.class, SessionIdGenerationStrategyConfiguration.class);
RedisIndexedSessionRepository sessionRepository = this.context.getBean(RedisIndexedSessionRepository.class);
assertThat(sessionRepository).extracting("sessionIdGenerationStrategy")
.isInstanceOf(TestSessionIdGenerationStrategy.class);
assertThat(sessionRepository).extracting("sessionIdGenerator").isInstanceOf(TestSessionIdGenerator.class);
}
@Test
void registerWhenNoSessionIdGenerationStrategyBeanThenDefault() {
registerAndRefresh(RedisConfig.class, DefaultConfiguration.class);
RedisIndexedSessionRepository sessionRepository = this.context.getBean(RedisIndexedSessionRepository.class);
assertThat(sessionRepository).extracting("sessionIdGenerationStrategy")
.isInstanceOf(UuidSessionIdGenerationStrategy.class);
assertThat(sessionRepository).extracting("sessionIdGenerator").isInstanceOf(UuidSessionIdGenerator.class);
}
private void registerAndRefresh(Class<?>... annotatedClasses) {
@@ -467,8 +465,8 @@ class RedisIndexedHttpSessionConfigurationTests {
static class SessionIdGenerationStrategyConfiguration {
@Bean
SessionIdGenerationStrategy sessionIdGenerationStrategy() {
return new TestSessionIdGenerationStrategy();
SessionIdGenerator sessionIdGenerationStrategy() {
return new TestSessionIdGenerator();
}
}
@@ -479,7 +477,7 @@ class RedisIndexedHttpSessionConfigurationTests {
}
static class TestSessionIdGenerationStrategy implements SessionIdGenerationStrategy {
static class TestSessionIdGenerator implements SessionIdGenerator {
@Override
public String generate() {