Validate Spring Session has been configured

This commit validates that a `SessionRepository` has been configured if
Spring Boot is meant to auto-configure one.

Closes gh-5943
pull/5385/merge
Stephane Nicoll 9 years ago
parent 5deca28a9d
commit 2b006aacb3

@ -16,6 +16,9 @@
package org.springframework.boot.autoconfigure.session;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -26,6 +29,7 @@ import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfigurati
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration.SessionConfigurationImportSelector;
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration.SessionRepositoryValidator;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@ -33,6 +37,7 @@ import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.session.Session;
import org.springframework.session.SessionRepository;
import org.springframework.util.Assert;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Session.
@ -50,7 +55,7 @@ import org.springframework.session.SessionRepository;
@EnableConfigurationProperties(SessionProperties.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, HazelcastAutoConfiguration.class,
MongoAutoConfiguration.class, RedisAutoConfiguration.class })
@Import(SessionConfigurationImportSelector.class)
@Import({ SessionConfigurationImportSelector.class, SessionRepositoryValidator.class})
public class SessionAutoConfiguration {
/**
@ -70,4 +75,32 @@ public class SessionAutoConfiguration {
}
/**
* Bean used to validate that a {@link SessionRepository} exists and provide a
* meaningful if that's not the case.
*/
static class SessionRepositoryValidator {
private SessionProperties sessionProperties;
private ObjectProvider<SessionRepository<?>> sessionRepositoryProvider;
SessionRepositoryValidator(SessionProperties sessionProperties,
ObjectProvider<SessionRepository<?>> sessionRepositoryProvider) {
this.sessionProperties = sessionProperties;
this.sessionRepositoryProvider = sessionRepositoryProvider;
}
@PostConstruct
public void checkSessionRepository() {
StoreType storeType = this.sessionProperties.getStoreType();
if (storeType != StoreType.NONE) {
Assert.notNull(this.sessionRepositoryProvider.getIfAvailable(),
"No session repository could "
+ "be auto-configured, check your configuration (session "
+ "store type is '" + storeType + "')");
}
}
}
}

@ -22,9 +22,12 @@ import java.util.Collections;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
@ -53,10 +56,16 @@ import static org.mockito.Mockito.verify;
*/
public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void autoConfigurationDisabledIfStoreTypeNotSet() {
public void contextFailsIfStoreTypeNotSet() {
this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("No session repository could be auto-configured");
this.thrown.expectMessage("session store type is 'null'");
load();
assertThat(this.context.getBeansOfType(SessionRepository.class)).hasSize(0);
}
@Test
@ -159,6 +168,14 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat
.isEqualTo("foobar");
}
@Test
public void validationFailsIfSessionRepositoryIsNotConfigured() {
this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("No session repository could be auto-configured");
this.thrown.expectMessage("session store type is 'JDBC'");
load("spring.session.store-type=jdbc");
}
@Configuration
static class SessionRepositoryConfiguration {

Loading…
Cancel
Save