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
This commit is contained in:
Stephane Nicoll 2016-05-09 08:07:25 +02:00
parent d80717349a
commit b7e7bcf717
6 changed files with 34 additions and 20 deletions

View File

@ -36,7 +36,7 @@ class SessionCondition extends SpringBootCondition {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
context.getEnvironment(), "spring.session.");
if (!resolver.containsProperty("store-type")) {
return ConditionOutcome.match("Automatic session store type");
return ConditionOutcome.noMatch("Session store type not set");
}
StoreType sessionStoreType = SessionStoreMappings
.getType(((AnnotationMetadata) metadata).getClassName());

View File

@ -32,7 +32,7 @@ import org.springframework.session.data.redis.RedisFlushMode;
public class SessionProperties {
/**
* Session store type, auto-detected according to the environment by default.
* Session store type.
*/
private StoreType storeType;

View File

@ -30,11 +30,6 @@ public enum StoreType {
*/
REDIS,
/**
* Hazelcast backed sessions.
*/
HAZELCAST,
/**
* Mongo backed sessions.
*/
@ -45,6 +40,11 @@ public enum StoreType {
*/
JDBC,
/**
* Hazelcast backed sessions.
*/
HAZELCAST,
/**
* Simple in-memory map of sessions.
*/

View File

@ -53,6 +53,18 @@ import static org.mockito.Mockito.verify;
*/
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
public void backOffIfSessionRepositoryIsPresent() {
load(Collections.<Class<?>>singletonList(SessionRepositoryConfiguration.class),
@ -86,12 +98,6 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat
assertThat(getSessionTimeout(repository)).isNull();
}
@Test
public void hashMapSessionStoreIsDefault() {
load();
validateSessionRepository(MapSessionRepository.class);
}
@Test
public void jdbcSessionStore() {
load(Arrays.asList(EmbeddedDataSourceConfiguration.class,
@ -153,6 +159,7 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat
.isEqualTo("foobar");
}
@Configuration
static class SessionRepositoryConfiguration {

View File

@ -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.redis.flush-mode= # Flush mode for the Redis 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.auto-connection-views=false # Enable the connection status view for supported providers.

View File

@ -4392,9 +4392,7 @@ class for more details.
[[boot-features-session]]
== Spring Session
Spring Boot provides Spring Session auto-configuration for a wide range of stores. If
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):
Spring Boot provides Spring Session auto-configuration for a wide range of stores:
* JDBC
* MongoDB
@ -4402,9 +4400,18 @@ Spring Boot tries to detect the following session stores (in this order):
* Hazelcast
* HashMap
It is also possible to _force_ the store to use via the `spring.session.store-type`
property. Each store have specific additional settings. For instance it is possible
to customize the name of the table for the jdbc store:
If Spring Session is available, you only need to chose the
{sc-spring-boot-autoconfigure}/session/StoreType.{sc-ext}[`StoreType`] that you wish to
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]
----