Commit b7e7bcf7 authored by Stephane Nicoll's avatar Stephane Nicoll

Make `spring.session.store-type` mandatory

Previously, Spring Session would be auto-configured by the mere presence
of Spring Session in the classpath. This was fragile as determining a
store type according to the environment could easily change when the
classpath of the project changes.

This commit makes the store-type property mandatory. If it is not set,
Spring Session is no longer auto-configured.

Closes gh-5838
parent d8071734
...@@ -36,7 +36,7 @@ class SessionCondition extends SpringBootCondition { ...@@ -36,7 +36,7 @@ class SessionCondition extends SpringBootCondition {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
context.getEnvironment(), "spring.session."); context.getEnvironment(), "spring.session.");
if (!resolver.containsProperty("store-type")) { if (!resolver.containsProperty("store-type")) {
return ConditionOutcome.match("Automatic session store type"); return ConditionOutcome.noMatch("Session store type not set");
} }
StoreType sessionStoreType = SessionStoreMappings StoreType sessionStoreType = SessionStoreMappings
.getType(((AnnotationMetadata) metadata).getClassName()); .getType(((AnnotationMetadata) metadata).getClassName());
......
...@@ -32,7 +32,7 @@ import org.springframework.session.data.redis.RedisFlushMode; ...@@ -32,7 +32,7 @@ import org.springframework.session.data.redis.RedisFlushMode;
public class SessionProperties { public class SessionProperties {
/** /**
* Session store type, auto-detected according to the environment by default. * Session store type.
*/ */
private StoreType storeType; private StoreType storeType;
......
...@@ -30,11 +30,6 @@ public enum StoreType { ...@@ -30,11 +30,6 @@ public enum StoreType {
*/ */
REDIS, REDIS,
/**
* Hazelcast backed sessions.
*/
HAZELCAST,
/** /**
* Mongo backed sessions. * Mongo backed sessions.
*/ */
...@@ -45,6 +40,11 @@ public enum StoreType { ...@@ -45,6 +40,11 @@ public enum StoreType {
*/ */
JDBC, JDBC,
/**
* Hazelcast backed sessions.
*/
HAZELCAST,
/** /**
* Simple in-memory map of sessions. * Simple in-memory map of sessions.
*/ */
......
...@@ -53,6 +53,18 @@ import static org.mockito.Mockito.verify; ...@@ -53,6 +53,18 @@ import static org.mockito.Mockito.verify;
*/ */
public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTests { public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTests {
@Test
public void autoConfigurationDisabledIfStoreTypeNotSet() {
load();
assertThat(this.context.getBeansOfType(SessionRepository.class)).hasSize(0);
}
@Test
public void autoConfigurationDisabledIfStoreTypeSetToNone() {
load("spring.session.store-type=none");
assertThat(this.context.getBeansOfType(SessionRepository.class)).hasSize(0);
}
@Test @Test
public void backOffIfSessionRepositoryIsPresent() { public void backOffIfSessionRepositoryIsPresent() {
load(Collections.<Class<?>>singletonList(SessionRepositoryConfiguration.class), load(Collections.<Class<?>>singletonList(SessionRepositoryConfiguration.class),
...@@ -86,12 +98,6 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat ...@@ -86,12 +98,6 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat
assertThat(getSessionTimeout(repository)).isNull(); assertThat(getSessionTimeout(repository)).isNull();
} }
@Test
public void hashMapSessionStoreIsDefault() {
load();
validateSessionRepository(MapSessionRepository.class);
}
@Test @Test
public void jdbcSessionStore() { public void jdbcSessionStore() {
load(Arrays.asList(EmbeddedDataSourceConfiguration.class, load(Arrays.asList(EmbeddedDataSourceConfiguration.class,
...@@ -153,6 +159,7 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat ...@@ -153,6 +159,7 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat
.isEqualTo("foobar"); .isEqualTo("foobar");
} }
@Configuration @Configuration
static class SessionRepositoryConfiguration { static class SessionRepositoryConfiguration {
......
...@@ -362,7 +362,7 @@ content into your application; rather pick only the properties that you need. ...@@ -362,7 +362,7 @@ content into your application; rather pick only the properties that you need.
spring.session.mongo.collection-name=sessions # Collection name used to store sessions. spring.session.mongo.collection-name=sessions # Collection name used to store sessions.
spring.session.redis.flush-mode= # Flush mode for the Redis sessions. spring.session.redis.flush-mode= # Flush mode for the Redis sessions.
spring.session.redis.namespace= # Namespace for keys used to store sessions. spring.session.redis.namespace= # Namespace for keys used to store sessions.
spring.session.store-type= # Session store type, auto-detected according to the environment by default. spring.session.store-type= # Session store type.
# SPRING SOCIAL ({sc-spring-boot-autoconfigure}/social/SocialWebAutoConfiguration.{sc-ext}[SocialWebAutoConfiguration]) # SPRING SOCIAL ({sc-spring-boot-autoconfigure}/social/SocialWebAutoConfiguration.{sc-ext}[SocialWebAutoConfiguration])
spring.social.auto-connection-views=false # Enable the connection status view for supported providers. spring.social.auto-connection-views=false # Enable the connection status view for supported providers.
......
...@@ -4392,9 +4392,7 @@ class for more details. ...@@ -4392,9 +4392,7 @@ class for more details.
[[boot-features-session]] [[boot-features-session]]
== Spring Session == Spring Session
Spring Boot provides Spring Session auto-configuration for a wide range of stores. If Spring Boot provides Spring Session auto-configuration for a wide range of stores:
Spring Session is available and you haven't defined a bean of type `SessionRepository`,
Spring Boot tries to detect the following session stores (in this order):
* JDBC * JDBC
* MongoDB * MongoDB
...@@ -4402,9 +4400,18 @@ Spring Boot tries to detect the following session stores (in this order): ...@@ -4402,9 +4400,18 @@ Spring Boot tries to detect the following session stores (in this order):
* Hazelcast * Hazelcast
* HashMap * HashMap
It is also possible to _force_ the store to use via the `spring.session.store-type` If Spring Session is available, you only need to chose the
property. Each store have specific additional settings. For instance it is possible {sc-spring-boot-autoconfigure}/session/StoreType.{sc-ext}[`StoreType`] that you wish to
to customize the name of the table for the jdbc store: use to store the sessions. For instance to use Redis as backend store, you'd configure
your application as follows:
[source,properties,indent=0]
----
spring.session.store-type=redis
----
Each store has specific additional settings. For instance it is possible to customize
the name of the table for the jdbc store:
[source,properties,indent=0] [source,properties,indent=0]
---- ----
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment