Polish "Add property to configure Spring Session Redis repository type"
See gh-32205
This commit is contained in:
parent
3093380e35
commit
2977373505
|
@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
|
||||||
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.data.redis.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
@ -53,16 +54,23 @@ import org.springframework.session.data.redis.config.annotation.web.http.RedisIn
|
||||||
class RedisSessionConfiguration {
|
class RedisSessionConfiguration {
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnProperty(prefix = "spring.session.redis", name = "repository", havingValue = "default",
|
@ConditionalOnProperty(prefix = "spring.session.redis", name = "repository-type", havingValue = "default",
|
||||||
matchIfMissing = true)
|
matchIfMissing = true)
|
||||||
static class DefaultRedisSessionConfiguration {
|
static class DefaultRedisSessionConfiguration {
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
public static class SpringBootRedisHttpSessionConfiguration extends RedisHttpSessionConfiguration {
|
static class SpringBootRedisHttpSessionConfiguration extends RedisHttpSessionConfiguration {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void customize(SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties,
|
void customize(SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties,
|
||||||
ServerProperties serverProperties) {
|
ServerProperties serverProperties) {
|
||||||
|
String cleanupCron = redisSessionProperties.getCleanupCron();
|
||||||
|
if (cleanupCron != null) {
|
||||||
|
throw new InvalidConfigurationPropertyValueException("spring.session.redis.cleanup-cron",
|
||||||
|
cleanupCron,
|
||||||
|
"Cron-based cleanup is only supported when spring.session.redis.repository-type is set to "
|
||||||
|
+ "indexed.");
|
||||||
|
}
|
||||||
Duration timeout = sessionProperties
|
Duration timeout = sessionProperties
|
||||||
.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout());
|
.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout());
|
||||||
if (timeout != null) {
|
if (timeout != null) {
|
||||||
|
@ -78,7 +86,7 @@ class RedisSessionConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnProperty(prefix = "spring.session.redis", name = "repository", havingValue = "indexed")
|
@ConditionalOnProperty(prefix = "spring.session.redis", name = "repository-type", havingValue = "indexed")
|
||||||
static class IndexedRedisSessionConfiguration {
|
static class IndexedRedisSessionConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -91,11 +99,12 @@ class RedisSessionConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
public static class SpringBootRedisIndexedHttpSessionConfiguration
|
static class SpringBootRedisIndexedHttpSessionConfiguration extends RedisIndexedHttpSessionConfiguration {
|
||||||
extends RedisIndexedHttpSessionConfiguration {
|
|
||||||
|
private static final String DEFAULT_CLEANUP_CRON = "0 * * * * *";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void customize(SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties,
|
void customize(SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties,
|
||||||
ServerProperties serverProperties) {
|
ServerProperties serverProperties) {
|
||||||
Duration timeout = sessionProperties
|
Duration timeout = sessionProperties
|
||||||
.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout());
|
.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout());
|
||||||
|
@ -105,7 +114,8 @@ class RedisSessionConfiguration {
|
||||||
setRedisNamespace(redisSessionProperties.getNamespace());
|
setRedisNamespace(redisSessionProperties.getNamespace());
|
||||||
setFlushMode(redisSessionProperties.getFlushMode());
|
setFlushMode(redisSessionProperties.getFlushMode());
|
||||||
setSaveMode(redisSessionProperties.getSaveMode());
|
setSaveMode(redisSessionProperties.getSaveMode());
|
||||||
setCleanupCron(redisSessionProperties.getCleanupCron());
|
String cleanupCron = redisSessionProperties.getCleanupCron();
|
||||||
|
setCleanupCron((cleanupCron != null) ? cleanupCron : DEFAULT_CLEANUP_CRON);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -29,8 +29,6 @@ import org.springframework.session.SaveMode;
|
||||||
@ConfigurationProperties(prefix = "spring.session.redis")
|
@ConfigurationProperties(prefix = "spring.session.redis")
|
||||||
public class RedisSessionProperties {
|
public class RedisSessionProperties {
|
||||||
|
|
||||||
private static final String DEFAULT_CLEANUP_CRON = "0 * * * * *";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Namespace for keys used to store sessions.
|
* Namespace for keys used to store sessions.
|
||||||
*/
|
*/
|
||||||
|
@ -55,9 +53,15 @@ public class RedisSessionProperties {
|
||||||
private ConfigureAction configureAction = ConfigureAction.NOTIFY_KEYSPACE_EVENTS;
|
private ConfigureAction configureAction = ConfigureAction.NOTIFY_KEYSPACE_EVENTS;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cron expression for expired session cleanup job.
|
* Cron expression for expired session cleanup job. Only supported when
|
||||||
|
* repository-type is set to indexed.
|
||||||
*/
|
*/
|
||||||
private String cleanupCron = DEFAULT_CLEANUP_CRON;
|
private String cleanupCron;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type of Redis session repository to configure.
|
||||||
|
*/
|
||||||
|
private RepositoryType repositoryType = RepositoryType.DEFAULT;
|
||||||
|
|
||||||
public String getNamespace() {
|
public String getNamespace() {
|
||||||
return this.namespace;
|
return this.namespace;
|
||||||
|
@ -99,6 +103,14 @@ public class RedisSessionProperties {
|
||||||
this.configureAction = configureAction;
|
this.configureAction = configureAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RepositoryType getRepositoryType() {
|
||||||
|
return this.repositoryType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRepositoryType(RepositoryType repositoryType) {
|
||||||
|
this.repositoryType = repositoryType;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Strategies for configuring and validating Redis.
|
* Strategies for configuring and validating Redis.
|
||||||
*/
|
*/
|
||||||
|
@ -117,4 +129,21 @@ public class RedisSessionProperties {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type of Redis session repository to auto-configure.
|
||||||
|
*/
|
||||||
|
public enum RepositoryType {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-configure a RedisSessionRepository.
|
||||||
|
*/
|
||||||
|
DEFAULT,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-configure a RedisIndexedSessionRepository.
|
||||||
|
*/
|
||||||
|
INDEXED
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2749,6 +2749,10 @@
|
||||||
"name": "spring.session.jdbc.save-mode",
|
"name": "spring.session.jdbc.save-mode",
|
||||||
"defaultValue": "on-set-attribute"
|
"defaultValue": "on-set-attribute"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.session.redis.cleanup-cron",
|
||||||
|
"defaultValue": "0 * * * * *"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "spring.session.redis.configure-action",
|
"name": "spring.session.redis.configure-action",
|
||||||
"defaultValue": "notify-keyspace-events"
|
"defaultValue": "notify-keyspace-events"
|
||||||
|
@ -2758,18 +2762,8 @@
|
||||||
"defaultValue": "on-save"
|
"defaultValue": "on-save"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "spring.session.redis.repository",
|
"name": "spring.session.redis.repository-type",
|
||||||
"description": "Redis session repository implementation to use.",
|
"defaultValue": "default"
|
||||||
"values": [
|
|
||||||
{
|
|
||||||
"value": "default",
|
|
||||||
"description": "Use default Redis session repository."
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"value": "indexed",
|
|
||||||
"description": "Use indexed Redis session repository."
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "spring.session.redis.save-mode",
|
"name": "spring.session.redis.save-mode",
|
||||||
|
|
|
@ -27,6 +27,7 @@ import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.session.RedisSessionConfiguration.IndexedRedisSessionConfiguration.SpringBootRedisIndexedHttpSessionConfiguration;
|
import org.springframework.boot.autoconfigure.session.RedisSessionConfiguration.IndexedRedisSessionConfiguration.SpringBootRedisIndexedHttpSessionConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||||
|
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
|
||||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||||
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
|
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
|
||||||
import org.springframework.boot.test.context.runner.ContextConsumer;
|
import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||||
|
@ -75,6 +76,17 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio
|
||||||
SaveMode.ON_SET_ATTRIBUTE));
|
SaveMode.ON_SET_ATTRIBUTE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void invalidConfigurationPropertyValueWhenDefaultConfigIsUsedWithCustomCronCleanup() {
|
||||||
|
this.contextRunner.withPropertyValues("spring.data.redis.host=" + redis.getHost(),
|
||||||
|
"spring.data.redis.port=" + redis.getFirstMappedPort(), "spring.session.redis.cleanup-cron=0 0 * * * *")
|
||||||
|
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class)).run((context) -> {
|
||||||
|
assertThat(context).hasFailed();
|
||||||
|
assertThat(context.getStartupFailure())
|
||||||
|
.hasRootCauseExactlyInstanceOf(InvalidConfigurationPropertyValueException.class);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void redisTakesPrecedenceMultipleImplementations() {
|
void redisTakesPrecedenceMultipleImplementations() {
|
||||||
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
|
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
|
||||||
|
@ -108,7 +120,7 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void indexedRedisSessionDefaultConfig() {
|
void indexedRedisSessionDefaultConfig() {
|
||||||
this.contextRunner.withPropertyValues("spring.session.redis.repository=indexed",
|
this.contextRunner.withPropertyValues("spring.session.redis.repository-type=indexed",
|
||||||
"spring.data.redis.host=" + redis.getHost(), "spring.data.redis.port=" + redis.getFirstMappedPort())
|
"spring.data.redis.host=" + redis.getHost(), "spring.data.redis.port=" + redis.getFirstMappedPort())
|
||||||
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
|
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
|
||||||
.run(validateSpringSessionUsesIndexedRedis("spring:session:", FlushMode.ON_SAVE,
|
.run(validateSpringSessionUsesIndexedRedis("spring:session:", FlushMode.ON_SAVE,
|
||||||
|
@ -118,8 +130,9 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio
|
||||||
@Test
|
@Test
|
||||||
void indexedRedisSessionStoreWithCustomizations() {
|
void indexedRedisSessionStoreWithCustomizations() {
|
||||||
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
|
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
|
||||||
.withPropertyValues("spring.session.redis.repository=indexed", "spring.session.redis.namespace=foo",
|
.withPropertyValues("spring.session.redis.repository-type=indexed",
|
||||||
"spring.session.redis.flush-mode=immediate", "spring.session.redis.save-mode=on-get-attribute",
|
"spring.session.redis.namespace=foo", "spring.session.redis.flush-mode=immediate",
|
||||||
|
"spring.session.redis.save-mode=on-get-attribute",
|
||||||
"spring.session.redis.cleanup-cron=0 0 12 * * *", "spring.data.redis.host=" + redis.getHost(),
|
"spring.session.redis.cleanup-cron=0 0 12 * * *", "spring.data.redis.host=" + redis.getHost(),
|
||||||
"spring.data.redis.port=" + redis.getFirstMappedPort())
|
"spring.data.redis.port=" + redis.getFirstMappedPort())
|
||||||
.run(validateSpringSessionUsesIndexedRedis("foo:", FlushMode.IMMEDIATE, SaveMode.ON_GET_ATTRIBUTE,
|
.run(validateSpringSessionUsesIndexedRedis("foo:", FlushMode.IMMEDIATE, SaveMode.ON_GET_ATTRIBUTE,
|
||||||
|
@ -129,7 +142,7 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio
|
||||||
@Test
|
@Test
|
||||||
void indexedRedisSessionWithConfigureActionNone() {
|
void indexedRedisSessionWithConfigureActionNone() {
|
||||||
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
|
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
|
||||||
.withPropertyValues("spring.session.redis.repository=indexed",
|
.withPropertyValues("spring.session.redis.repository-type=indexed",
|
||||||
"spring.session.redis.configure-action=none", "spring.data.redis.host=" + redis.getHost(),
|
"spring.session.redis.configure-action=none", "spring.data.redis.host=" + redis.getHost(),
|
||||||
"spring.data.redis.port=" + redis.getFirstMappedPort())
|
"spring.data.redis.port=" + redis.getFirstMappedPort())
|
||||||
.run(validateStrategy(ConfigureRedisAction.NO_OP.getClass()));
|
.run(validateStrategy(ConfigureRedisAction.NO_OP.getClass()));
|
||||||
|
@ -138,7 +151,7 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio
|
||||||
@Test
|
@Test
|
||||||
void indexedRedisSessionWithDefaultConfigureActionNone() {
|
void indexedRedisSessionWithDefaultConfigureActionNone() {
|
||||||
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
|
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
|
||||||
.withPropertyValues("spring.session.redis.repository=indexed",
|
.withPropertyValues("spring.session.redis.repository-type=indexed",
|
||||||
"spring.data.redis.host=" + redis.getHost(),
|
"spring.data.redis.host=" + redis.getHost(),
|
||||||
"spring.data.redis.port=" + redis.getFirstMappedPort())
|
"spring.data.redis.port=" + redis.getFirstMappedPort())
|
||||||
.run(validateStrategy(ConfigureNotifyKeyspaceEventsAction.class,
|
.run(validateStrategy(ConfigureNotifyKeyspaceEventsAction.class,
|
||||||
|
@ -149,7 +162,7 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio
|
||||||
void indexedRedisSessionWithCustomConfigureRedisActionBean() {
|
void indexedRedisSessionWithCustomConfigureRedisActionBean() {
|
||||||
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
|
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
|
||||||
.withUserConfiguration(MaxEntriesRedisAction.class)
|
.withUserConfiguration(MaxEntriesRedisAction.class)
|
||||||
.withPropertyValues("spring.session.redis.repository=indexed",
|
.withPropertyValues("spring.session.redis.repository-type=indexed",
|
||||||
"spring.data.redis.host=" + redis.getHost(),
|
"spring.data.redis.host=" + redis.getHost(),
|
||||||
"spring.data.redis.port=" + redis.getFirstMappedPort())
|
"spring.data.redis.port=" + redis.getFirstMappedPort())
|
||||||
.run(validateStrategy(MaxEntriesRedisAction.class, entry("set-max-intset-entries", "1024")));
|
.run(validateStrategy(MaxEntriesRedisAction.class, entry("set-max-intset-entries", "1024")));
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
management.endpoints.web.exposure.include=*
|
management.endpoints.web.exposure.include=*
|
||||||
spring.security.user.name=user
|
spring.security.user.name=user
|
||||||
spring.security.user.password=password
|
spring.security.user.password=password
|
||||||
spring.session.redis.repository=indexed
|
spring.session.redis.repository-type=indexed
|
||||||
|
|
Loading…
Reference in New Issue