diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java index 06c4f6f143..6c47e9c3bd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java @@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.web.ServerProperties; 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.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; @@ -53,16 +54,23 @@ import org.springframework.session.data.redis.config.annotation.web.http.RedisIn class RedisSessionConfiguration { @Configuration(proxyBeanMethods = false) - @ConditionalOnProperty(prefix = "spring.session.redis", name = "repository", havingValue = "default", + @ConditionalOnProperty(prefix = "spring.session.redis", name = "repository-type", havingValue = "default", matchIfMissing = true) static class DefaultRedisSessionConfiguration { @Configuration(proxyBeanMethods = false) - public static class SpringBootRedisHttpSessionConfiguration extends RedisHttpSessionConfiguration { + static class SpringBootRedisHttpSessionConfiguration extends RedisHttpSessionConfiguration { @Autowired - public void customize(SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties, + void customize(SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties, 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 .determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()); if (timeout != null) { @@ -78,7 +86,7 @@ class RedisSessionConfiguration { } @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 { @Bean @@ -91,11 +99,12 @@ class RedisSessionConfiguration { } @Configuration(proxyBeanMethods = false) - public static class SpringBootRedisIndexedHttpSessionConfiguration - extends RedisIndexedHttpSessionConfiguration { + static class SpringBootRedisIndexedHttpSessionConfiguration extends RedisIndexedHttpSessionConfiguration { + + private static final String DEFAULT_CLEANUP_CRON = "0 * * * * *"; @Autowired - public void customize(SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties, + void customize(SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties, ServerProperties serverProperties) { Duration timeout = sessionProperties .determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()); @@ -105,7 +114,8 @@ class RedisSessionConfiguration { setRedisNamespace(redisSessionProperties.getNamespace()); setFlushMode(redisSessionProperties.getFlushMode()); setSaveMode(redisSessionProperties.getSaveMode()); - setCleanupCron(redisSessionProperties.getCleanupCron()); + String cleanupCron = redisSessionProperties.getCleanupCron(); + setCleanupCron((cleanupCron != null) ? cleanupCron : DEFAULT_CLEANUP_CRON); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionProperties.java index 69d66a0bb1..8b2042567a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionProperties.java @@ -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"); * 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") public class RedisSessionProperties { - private static final String DEFAULT_CLEANUP_CRON = "0 * * * * *"; - /** * Namespace for keys used to store sessions. */ @@ -55,9 +53,15 @@ public class RedisSessionProperties { 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() { return this.namespace; @@ -99,6 +103,14 @@ public class RedisSessionProperties { this.configureAction = configureAction; } + public RepositoryType getRepositoryType() { + return this.repositoryType; + } + + public void setRepositoryType(RepositoryType repositoryType) { + this.repositoryType = repositoryType; + } + /** * 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 + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index b8f94f492d..2debd65b48 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -2749,6 +2749,10 @@ "name": "spring.session.jdbc.save-mode", "defaultValue": "on-set-attribute" }, + { + "name": "spring.session.redis.cleanup-cron", + "defaultValue": "0 * * * * *" + }, { "name": "spring.session.redis.configure-action", "defaultValue": "notify-keyspace-events" @@ -2758,18 +2762,8 @@ "defaultValue": "on-save" }, { - "name": "spring.session.redis.repository", - "description": "Redis session repository implementation to use.", - "values": [ - { - "value": "default", - "description": "Use default Redis session repository." - }, - { - "value": "indexed", - "description": "Use indexed Redis session repository." - } - ] + "name": "spring.session.redis.repository-type", + "defaultValue": "default" }, { "name": "spring.session.redis.save-mode", diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationRedisTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationRedisTests.java index d0fecd4253..f27fec99e5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationRedisTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationRedisTests.java @@ -27,6 +27,7 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.session.RedisSessionConfiguration.IndexedRedisSessionConfiguration.SpringBootRedisIndexedHttpSessionConfiguration; 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.assertj.AssertableWebApplicationContext; import org.springframework.boot.test.context.runner.ContextConsumer; @@ -75,6 +76,17 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio 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 void redisTakesPrecedenceMultipleImplementations() { this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class)) @@ -108,7 +120,7 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio @Test 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()) .withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class)) .run(validateSpringSessionUsesIndexedRedis("spring:session:", FlushMode.ON_SAVE, @@ -118,8 +130,9 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio @Test void indexedRedisSessionStoreWithCustomizations() { this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class)) - .withPropertyValues("spring.session.redis.repository=indexed", "spring.session.redis.namespace=foo", - "spring.session.redis.flush-mode=immediate", "spring.session.redis.save-mode=on-get-attribute", + .withPropertyValues("spring.session.redis.repository-type=indexed", + "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.data.redis.port=" + redis.getFirstMappedPort()) .run(validateSpringSessionUsesIndexedRedis("foo:", FlushMode.IMMEDIATE, SaveMode.ON_GET_ATTRIBUTE, @@ -129,7 +142,7 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio @Test void indexedRedisSessionWithConfigureActionNone() { 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.data.redis.port=" + redis.getFirstMappedPort()) .run(validateStrategy(ConfigureRedisAction.NO_OP.getClass())); @@ -138,7 +151,7 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio @Test void indexedRedisSessionWithDefaultConfigureActionNone() { 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.port=" + redis.getFirstMappedPort()) .run(validateStrategy(ConfigureNotifyKeyspaceEventsAction.class, @@ -149,7 +162,7 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio void indexedRedisSessionWithCustomConfigureRedisActionBean() { this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.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.port=" + redis.getFirstMappedPort()) .run(validateStrategy(MaxEntriesRedisAction.class, entry("set-max-intset-entries", "1024"))); diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-redis/src/main/resources/application.properties b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-redis/src/main/resources/application.properties index 8c8faded23..0cecb91d27 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-redis/src/main/resources/application.properties +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-redis/src/main/resources/application.properties @@ -1,4 +1,4 @@ management.endpoints.web.exposure.include=* spring.security.user.name=user spring.security.user.password=password -spring.session.redis.repository=indexed +spring.session.redis.repository-type=indexed