Merge branch '3.1.x'

Closes gh-36697
pull/36767/head
Andy Wilkinson 1 year ago
commit 48813a0e5b

@ -27,6 +27,8 @@ import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.session.SessionRepository; import org.springframework.session.SessionRepository;
import org.springframework.session.config.SessionRepositoryCustomizer; import org.springframework.session.config.SessionRepositoryCustomizer;
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository; import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository;
@ -49,6 +51,7 @@ import org.springframework.session.hazelcast.config.annotation.web.http.Hazelcas
class HazelcastSessionConfiguration { class HazelcastSessionConfiguration {
@Bean @Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> springBootSessionRepositoryCustomizer( SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> springBootSessionRepositoryCustomizer(
SessionProperties sessionProperties, HazelcastSessionProperties hazelcastSessionProperties, SessionProperties sessionProperties, HazelcastSessionProperties hazelcastSessionProperties,
ServerProperties serverProperties) { ServerProperties serverProperties) {

@ -25,6 +25,8 @@ import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.session.SessionRepository; import org.springframework.session.SessionRepository;
import org.springframework.session.config.SessionRepositoryCustomizer; import org.springframework.session.config.SessionRepositoryCustomizer;
@ -47,6 +49,7 @@ import org.springframework.session.data.mongo.config.annotation.web.http.MongoHt
class MongoSessionConfiguration { class MongoSessionConfiguration {
@Bean @Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
SessionRepositoryCustomizer<MongoIndexedSessionRepository> springBootSessionRepositoryCustomizer( SessionRepositoryCustomizer<MongoIndexedSessionRepository> springBootSessionRepositoryCustomizer(
SessionProperties sessionProperties, MongoSessionProperties mongoSessionProperties, SessionProperties sessionProperties, MongoSessionProperties mongoSessionProperties,
ServerProperties serverProperties) { ServerProperties serverProperties) {

@ -27,6 +27,8 @@ import org.springframework.boot.context.properties.source.InvalidConfigurationPr
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.session.SessionRepository; import org.springframework.session.SessionRepository;
@ -61,6 +63,7 @@ class RedisSessionConfiguration {
static class DefaultRedisSessionConfiguration { static class DefaultRedisSessionConfiguration {
@Bean @Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
SessionRepositoryCustomizer<RedisSessionRepository> springBootSessionRepositoryCustomizer( SessionRepositoryCustomizer<RedisSessionRepository> springBootSessionRepositoryCustomizer(
SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties, SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties,
ServerProperties serverProperties) { ServerProperties serverProperties) {
@ -98,6 +101,7 @@ class RedisSessionConfiguration {
} }
@Bean @Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
SessionRepositoryCustomizer<RedisIndexedSessionRepository> springBootSessionRepositoryCustomizer( SessionRepositoryCustomizer<RedisIndexedSessionRepository> springBootSessionRepositoryCustomizer(
SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties, SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties,
ServerProperties serverProperties) { ServerProperties serverProperties) {

@ -31,6 +31,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.session.FlushMode; import org.springframework.session.FlushMode;
import org.springframework.session.SaveMode; import org.springframework.session.SaveMode;
import org.springframework.session.config.SessionRepositoryCustomizer;
import org.springframework.session.data.mongo.MongoIndexedSessionRepository; import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
import org.springframework.session.data.redis.RedisIndexedSessionRepository; import org.springframework.session.data.redis.RedisIndexedSessionRepository;
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository; import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository;
@ -112,6 +113,17 @@ class SessionAutoConfigurationHazelcastTests extends AbstractSessionAutoConfigur
}); });
} }
@Test
void whenTheUserDefinesTheirOwnSessionRepositoryCustomizerThenDefaultConfigurationIsOverwritten() {
this.contextRunner.withUserConfiguration(CustomizerConfiguration.class)
.withPropertyValues("spring.session.hazelcast.save-mode=on-get-attribute")
.run((context) -> {
HazelcastIndexedSessionRepository repository = validateSessionRepository(context,
HazelcastIndexedSessionRepository.class);
assertThat(repository).hasFieldOrPropertyWithValue("saveMode", SaveMode.ALWAYS);
});
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class HazelcastConfiguration { static class HazelcastConfiguration {
@ -127,4 +139,14 @@ class SessionAutoConfigurationHazelcastTests extends AbstractSessionAutoConfigur
} }
@Configuration(proxyBeanMethods = false)
static class CustomizerConfiguration {
@Bean
SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> sessionRepositoryCustomizer() {
return (repository) -> repository.setSaveMode(SaveMode.ALWAYS);
}
}
} }

@ -32,6 +32,9 @@ import org.springframework.boot.test.context.assertj.AssertableWebApplicationCon
import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames; import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.config.SessionRepositoryCustomizer;
import org.springframework.session.data.mongo.MongoIndexedSessionRepository; import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
import org.springframework.session.data.redis.RedisIndexedSessionRepository; import org.springframework.session.data.redis.RedisIndexedSessionRepository;
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository; import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository;
@ -75,6 +78,13 @@ class SessionAutoConfigurationMongoTests extends AbstractSessionAutoConfiguratio
.run(validateSpringSessionUsesMongo("foo")); .run(validateSpringSessionUsesMongo("foo"));
} }
@Test
void whenTheUserDefinesTheirOwnSessionRepositoryCustomizerThenDefaultConfigurationIsOverwritten() {
this.contextRunner.withUserConfiguration(CustomizerConfiguration.class)
.withPropertyValues("spring.session.mongodb.collection-name=foo")
.run(validateSpringSessionUsesMongo("customized"));
}
private ContextConsumer<AssertableWebApplicationContext> validateSpringSessionUsesMongo(String collectionName) { private ContextConsumer<AssertableWebApplicationContext> validateSpringSessionUsesMongo(String collectionName) {
return validateSpringSessionUsesMongo(collectionName, return validateSpringSessionUsesMongo(collectionName,
new ServerProperties().getServlet().getSession().getTimeout()); new ServerProperties().getServlet().getSession().getTimeout());
@ -90,4 +100,14 @@ class SessionAutoConfigurationMongoTests extends AbstractSessionAutoConfiguratio
}; };
} }
@Configuration(proxyBeanMethods = false)
static class CustomizerConfiguration {
@Bean
SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizer() {
return (repository) -> repository.setCollectionName("customized");
}
}
} }

@ -32,10 +32,13 @@ import org.springframework.boot.test.context.assertj.AssertableWebApplicationCon
import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.testsupport.testcontainers.RedisContainer; import org.springframework.boot.testsupport.testcontainers.RedisContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.session.FlushMode; import org.springframework.session.FlushMode;
import org.springframework.session.SaveMode; import org.springframework.session.SaveMode;
import org.springframework.session.config.SessionRepositoryCustomizer;
import org.springframework.session.data.mongo.MongoIndexedSessionRepository; import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
import org.springframework.session.data.redis.RedisIndexedSessionRepository; import org.springframework.session.data.redis.RedisIndexedSessionRepository;
import org.springframework.session.data.redis.RedisSessionRepository; import org.springframework.session.data.redis.RedisSessionRepository;
@ -165,6 +168,32 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio
} }
@Test
void whenTheUserDefinesTheirOwnSessionRepositoryCustomizerThenDefaultConfigurationIsOverwritten() {
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
.withUserConfiguration(CustomizerConfiguration.class)
.withPropertyValues("spring.session.redis.flush-mode=immediate",
"spring.data.redis.host=" + redis.getHost(), "spring.data.redis.port=" + redis.getFirstMappedPort())
.run((context) -> {
RedisSessionRepository repository = validateSessionRepository(context, RedisSessionRepository.class);
assertThat(repository).hasFieldOrPropertyWithValue("flushMode", FlushMode.ON_SAVE);
});
}
@Test
void whenIndexedAndTheUserDefinesTheirOwnSessionRepositoryCustomizerThenDefaultConfigurationIsOverwritten() {
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
.withUserConfiguration(IndexedCustomizerConfiguration.class)
.withPropertyValues("spring.session.redis.repository-type=indexed",
"spring.session.redis.flush-mode=immediate", "spring.data.redis.host=" + redis.getHost(),
"spring.data.redis.port=" + redis.getFirstMappedPort())
.run((context) -> {
RedisIndexedSessionRepository repository = validateSessionRepository(context,
RedisIndexedSessionRepository.class);
assertThat(repository).hasFieldOrPropertyWithValue("flushMode", FlushMode.ON_SAVE);
});
}
private ContextConsumer<AssertableWebApplicationContext> validateSpringSessionUsesDefaultRedis(String keyNamespace, private ContextConsumer<AssertableWebApplicationContext> validateSpringSessionUsesDefaultRedis(String keyNamespace,
FlushMode flushMode, SaveMode saveMode) { FlushMode flushMode, SaveMode saveMode) {
return (context) -> { return (context) -> {
@ -213,4 +242,24 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio
} }
@Configuration(proxyBeanMethods = false)
static class CustomizerConfiguration {
@Bean
SessionRepositoryCustomizer<RedisSessionRepository> sessionRepositoryCustomizer() {
return (repository) -> repository.setFlushMode(FlushMode.ON_SAVE);
}
}
@Configuration(proxyBeanMethods = false)
static class IndexedCustomizerConfiguration {
@Bean
SessionRepositoryCustomizer<RedisIndexedSessionRepository> sessionRepositoryCustomizer() {
return (repository) -> repository.setFlushMode(FlushMode.ON_SAVE);
}
}
} }

Loading…
Cancel
Save