Browse Source

Ensure compatibility with Spring Session module split

This commit updates Spring Session auto-configuration to ensure
compatibility with extraction of `SessionRepository` implementations into
separate Spring Session modules.

See gh-9554
pull/9566/merge
Vedran Pavic 9 years ago committed by Stephane Nicoll
parent
commit
ccb1eaf8ed
  1. 20
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HashMapSessionConfiguration.java
  2. 41
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionConfiguration.java
  3. 57
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionProperties.java
  4. 37
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java
  5. 8
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java
  6. 91
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionProperties.java
  7. 36
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java
  8. 57
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionProperties.java
  9. 14
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java
  10. 210
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java
  11. 5
      spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json
  12. 16
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java
  13. 7
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java

20
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HashMapSessionConfiguration.java

@ -16,7 +16,10 @@ @@ -16,7 +16,10 @@
package org.springframework.boot.autoconfigure.session;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@ -35,14 +38,23 @@ import org.springframework.session.config.annotation.web.http.EnableSpringHttpSe @@ -35,14 +38,23 @@ import org.springframework.session.config.annotation.web.http.EnableSpringHttpSe
@EnableSpringHttpSession
@Conditional(SessionCondition.class)
@ConditionalOnMissingBean(SessionRepository.class)
@EnableConfigurationProperties(ServerProperties.class)
class HashMapSessionConfiguration {
private final ServerProperties serverProperties;
HashMapSessionConfiguration(ObjectProvider<ServerProperties> serverProperties) {
this.serverProperties = serverProperties.getIfUnique();
}
@Bean
public MapSessionRepository sessionRepository(SessionProperties properties) {
public MapSessionRepository sessionRepository() {
MapSessionRepository repository = new MapSessionRepository();
Integer timeout = properties.getTimeout();
if (timeout != null) {
repository.setDefaultMaxInactiveInterval(timeout);
if (this.serverProperties != null) {
Integer timeout = this.serverProperties.getSession().getTimeout();
if (timeout != null) {
repository.setDefaultMaxInactiveInterval(timeout);
}
}
return repository;
}

41
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionConfiguration.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@ -16,14 +16,20 @@ @@ -16,14 +16,20 @@
package org.springframework.boot.autoconfigure.session;
import javax.annotation.PostConstruct;
import com.hazelcast.core.HazelcastInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.SessionRepository;
import org.springframework.session.hazelcast.HazelcastSessionRepository;
import org.springframework.session.hazelcast.config.annotation.web.http.HazelcastHttpSessionConfiguration;
/**
@ -35,24 +41,39 @@ import org.springframework.session.hazelcast.config.annotation.web.http.Hazelcas @@ -35,24 +41,39 @@ import org.springframework.session.hazelcast.config.annotation.web.http.Hazelcas
* @author Vedran Pavic
*/
@Configuration
@ConditionalOnClass(HazelcastSessionRepository.class)
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnBean(HazelcastInstance.class)
@Conditional(SessionCondition.class)
@EnableConfigurationProperties({ ServerProperties.class,
HazelcastSessionProperties.class })
class HazelcastSessionConfiguration {
@Configuration
public static class SpringBootHazelcastHttpSessionConfiguration
extends HazelcastHttpSessionConfiguration {
@Autowired
public void customize(SessionProperties sessionProperties) {
Integer timeout = sessionProperties.getTimeout();
if (timeout != null) {
setMaxInactiveIntervalInSeconds(timeout);
private final HazelcastSessionProperties sessionProperties;
private final ServerProperties serverProperties;
SpringBootHazelcastHttpSessionConfiguration(
HazelcastSessionProperties sessionProperties,
ObjectProvider<ServerProperties> serverProperties) {
this.sessionProperties = sessionProperties;
this.serverProperties = serverProperties.getIfUnique();
}
@PostConstruct
public void init() {
if (this.serverProperties != null) {
Integer timeout = this.serverProperties.getSession().getTimeout();
if (timeout != null) {
setMaxInactiveIntervalInSeconds(timeout);
}
}
SessionProperties.Hazelcast hazelcast = sessionProperties.getHazelcast();
setSessionMapName(hazelcast.getMapName());
setHazelcastFlushMode(hazelcast.getFlushMode());
setSessionMapName(this.sessionProperties.getMapName());
setHazelcastFlushMode(this.sessionProperties.getFlushMode());
}
}

57
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionProperties.java

@ -0,0 +1,57 @@ @@ -0,0 +1,57 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.session;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.session.hazelcast.HazelcastFlushMode;
/**
* Configuration properties for Hazelcast backed Spring Session.
*
* @author Vedran Pavic
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "spring.session.hazelcast")
public class HazelcastSessionProperties {
/**
* Name of the map used to store sessions.
*/
private String mapName = "spring:session:sessions";
/**
* Sessions flush mode.
*/
private HazelcastFlushMode flushMode = HazelcastFlushMode.ON_SAVE;
public String getMapName() {
return this.mapName;
}
public void setMapName(String mapName) {
this.mapName = mapName;
}
public HazelcastFlushMode getFlushMode() {
return this.flushMode;
}
public void setFlushMode(HazelcastFlushMode flushMode) {
this.flushMode = flushMode;
}
}

37
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@ -16,18 +16,22 @@ @@ -16,18 +16,22 @@
package org.springframework.boot.autoconfigure.session;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.session.SessionRepository;
import org.springframework.session.jdbc.JdbcOperationsSessionRepository;
import org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration;
/**
@ -38,17 +42,18 @@ import org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessi @@ -38,17 +42,18 @@ import org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessi
* @author Vedran Pavic
*/
@Configuration
@ConditionalOnClass(JdbcTemplate.class)
@ConditionalOnClass({ JdbcTemplate.class, JdbcOperationsSessionRepository.class })
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnBean(DataSource.class)
@Conditional(SessionCondition.class)
@EnableConfigurationProperties({ ServerProperties.class, JdbcSessionProperties.class })
class JdbcSessionConfiguration {
@Bean
@ConditionalOnMissingBean
public JdbcSessionDatabaseInitializer jdbcSessionDatabaseInitializer(
DataSource dataSource, ResourceLoader resourceLoader,
SessionProperties properties) {
JdbcSessionProperties properties) {
return new JdbcSessionDatabaseInitializer(dataSource, resourceLoader, properties);
}
@ -56,13 +61,25 @@ class JdbcSessionConfiguration { @@ -56,13 +61,25 @@ class JdbcSessionConfiguration {
public static class SpringBootJdbcHttpSessionConfiguration
extends JdbcHttpSessionConfiguration {
@Autowired
public void customize(SessionProperties sessionProperties) {
Integer timeout = sessionProperties.getTimeout();
if (timeout != null) {
setMaxInactiveIntervalInSeconds(timeout);
private final JdbcSessionProperties sessionProperties;
private final ServerProperties serverProperties;
SpringBootJdbcHttpSessionConfiguration(JdbcSessionProperties sessionProperties,
ObjectProvider<ServerProperties> serverProperties) {
this.sessionProperties = sessionProperties;
this.serverProperties = serverProperties.getIfUnique();
}
@PostConstruct
public void init() {
if (this.serverProperties != null) {
Integer timeout = this.serverProperties.getSession().getTimeout();
if (timeout != null) {
setMaxInactiveIntervalInSeconds(timeout);
}
}
setTableName(sessionProperties.getJdbc().getTableName());
setTableName(this.sessionProperties.getTableName());
}
}

8
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java

@ -30,13 +30,13 @@ import org.springframework.util.Assert; @@ -30,13 +30,13 @@ import org.springframework.util.Assert;
*/
public class JdbcSessionDatabaseInitializer extends AbstractDatabaseInitializer {
private final SessionProperties.Jdbc properties;
private final JdbcSessionProperties properties;
public JdbcSessionDatabaseInitializer(DataSource dataSource,
ResourceLoader resourceLoader, SessionProperties properties) {
ResourceLoader resourceLoader, JdbcSessionProperties properties) {
super(dataSource, resourceLoader);
Assert.notNull(properties, "SessionProperties must not be null");
this.properties = properties.getJdbc();
Assert.notNull(properties, "JdbcSessionProperties must not be null");
this.properties = properties;
}
@Override

91
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionProperties.java

@ -0,0 +1,91 @@ @@ -0,0 +1,91 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.session;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for JDBC backed Spring Session.
*
* @author Vedran Pavic
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "spring.session.jdbc")
public class JdbcSessionProperties {
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
+ "session/jdbc/schema-@@platform@@.sql";
private static final String DEFAULT_TABLE_NAME = "SPRING_SESSION";
/**
* Path to the SQL file to use to initialize the database schema.
*/
private String schema = DEFAULT_SCHEMA_LOCATION;
/**
* Name of database table used to store sessions.
*/
private String tableName = DEFAULT_TABLE_NAME;
private final Initializer initializer = new Initializer();
public String getSchema() {
return this.schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
public String getTableName() {
return this.tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public Initializer getInitializer() {
return this.initializer;
}
public class Initializer {
/**
* Create the required session tables on startup if necessary. Enabled
* automatically if the default table name is set or a custom schema is
* configured.
*/
private Boolean enabled;
public boolean isEnabled() {
if (this.enabled != null) {
return this.enabled;
}
boolean defaultTableName = DEFAULT_TABLE_NAME.equals(getTableName());
boolean customSchema = !DEFAULT_SCHEMA_LOCATION.equals(getSchema());
return (defaultTableName || customSchema);
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
}

36
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java

@ -16,15 +16,20 @@ @@ -16,15 +16,20 @@
package org.springframework.boot.autoconfigure.session;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.session.SessionRepository;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration;
/**
@ -37,28 +42,37 @@ import org.springframework.session.data.redis.config.annotation.web.http.RedisHt @@ -37,28 +42,37 @@ import org.springframework.session.data.redis.config.annotation.web.http.RedisHt
* @author Vedran Pavic
*/
@Configuration
@ConditionalOnClass(RedisTemplate.class)
@ConditionalOnClass({ RedisTemplate.class, RedisOperationsSessionRepository.class })
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnBean(RedisConnectionFactory.class)
@Conditional(SessionCondition.class)
@EnableConfigurationProperties({ ServerProperties.class, RedisSessionProperties.class })
class RedisSessionConfiguration {
@Configuration
public static class SpringBootRedisHttpSessionConfiguration
extends RedisHttpSessionConfiguration {
private SessionProperties sessionProperties;
private final RedisSessionProperties sessionProperties;
private final ServerProperties serverProperties;
@Autowired
public void customize(SessionProperties sessionProperties) {
SpringBootRedisHttpSessionConfiguration(RedisSessionProperties sessionProperties,
ObjectProvider<ServerProperties> serverProperties) {
this.sessionProperties = sessionProperties;
Integer timeout = this.sessionProperties.getTimeout();
if (timeout != null) {
setMaxInactiveIntervalInSeconds(timeout);
this.serverProperties = serverProperties.getIfUnique();
}
@PostConstruct
public void init() {
if (this.serverProperties != null) {
Integer timeout = this.serverProperties.getSession().getTimeout();
if (timeout != null) {
setMaxInactiveIntervalInSeconds(timeout);
}
}
SessionProperties.Redis redis = this.sessionProperties.getRedis();
setRedisNamespace(redis.getNamespace());
setRedisFlushMode(redis.getFlushMode());
setRedisNamespace(this.sessionProperties.getNamespace());
setRedisFlushMode(this.sessionProperties.getFlushMode());
}
}

57
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionProperties.java

@ -0,0 +1,57 @@ @@ -0,0 +1,57 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.session;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.session.data.redis.RedisFlushMode;
/**
* Configuration properties for Redis backed Spring Session.
*
* @author Vedran Pavic
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "spring.session.redis")
public class RedisSessionProperties {
/**
* Namespace for keys used to store sessions.
*/
private String namespace = "";
/**
* Sessions flush mode.
*/
private RedisFlushMode flushMode = RedisFlushMode.ON_SAVE;
public String getNamespace() {
return this.namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public RedisFlushMode getFlushMode() {
return this.flushMode;
}
public void setFlushMode(RedisFlushMode flushMode) {
this.flushMode = flushMode;
}
}

14
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java

@ -31,10 +31,11 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @@ -31,10 +31,11 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
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.boot.context.properties.bind.Binder;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.session.Session;
import org.springframework.session.SessionRepository;
@ -53,7 +54,6 @@ import org.springframework.session.SessionRepository; @@ -53,7 +54,6 @@ import org.springframework.session.SessionRepository;
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnClass(Session.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(SessionProperties.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, HazelcastAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class, RedisAutoConfiguration.class })
@Import({ SessionConfigurationImportSelector.class, SessionRepositoryValidator.class,
@ -83,19 +83,21 @@ public class SessionAutoConfiguration { @@ -83,19 +83,21 @@ public class SessionAutoConfiguration {
*/
static class SessionRepositoryValidator {
private SessionProperties sessionProperties;
private Environment environment;
private ObjectProvider<SessionRepository<?>> sessionRepositoryProvider;
SessionRepositoryValidator(SessionProperties sessionProperties,
SessionRepositoryValidator(Environment environment,
ObjectProvider<SessionRepository<?>> sessionRepositoryProvider) {
this.sessionProperties = sessionProperties;
this.environment = environment;
this.sessionRepositoryProvider = sessionRepositoryProvider;
}
@PostConstruct
public void checkSessionRepository() {
StoreType storeType = this.sessionProperties.getStoreType();
Binder binder = Binder.get(this.environment);
StoreType storeType = binder
.bind("spring.session.store-type", StoreType.class).orElse(null);
if (storeType != StoreType.NONE
&& this.sessionRepositoryProvider.getIfAvailable() == null) {
if (storeType != null) {

210
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java

@ -1,210 +0,0 @@ @@ -1,210 +0,0 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.session;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.session.data.redis.RedisFlushMode;
import org.springframework.session.hazelcast.HazelcastFlushMode;
/**
* Configuration properties for Spring Session.
*
* @author Tommy Ludwig
* @author Stephane Nicoll
* @author Vedran Pavic
* @since 1.4.0
*/
@ConfigurationProperties(prefix = "spring.session")
public class SessionProperties {
/**
* Session store type.
*/
private StoreType storeType;
private Integer timeout;
private final Hazelcast hazelcast = new Hazelcast();
private final Jdbc jdbc = new Jdbc();
private final Redis redis = new Redis();
public SessionProperties(ObjectProvider<ServerProperties> serverProperties) {
ServerProperties properties = serverProperties.getIfUnique();
this.timeout = (properties != null ? properties.getSession().getTimeout() : null);
}
public StoreType getStoreType() {
return this.storeType;
}
public void setStoreType(StoreType storeType) {
this.storeType = storeType;
}
/**
* Return the session timeout in seconds.
* @return the session timeout in seconds
* @see ServerProperties#getSession()
*/
public Integer getTimeout() {
return this.timeout;
}
public Hazelcast getHazelcast() {
return this.hazelcast;
}
public Jdbc getJdbc() {
return this.jdbc;
}
public Redis getRedis() {
return this.redis;
}
public static class Hazelcast {
/**
* Name of the map used to store sessions.
*/
private String mapName = "spring:session:sessions";
/**
* Sessions flush mode.
*/
private HazelcastFlushMode flushMode = HazelcastFlushMode.ON_SAVE;
public String getMapName() {
return this.mapName;
}
public void setMapName(String mapName) {
this.mapName = mapName;
}
public HazelcastFlushMode getFlushMode() {
return this.flushMode;
}
public void setFlushMode(HazelcastFlushMode flushMode) {
this.flushMode = flushMode;
}
}
public static class Jdbc {
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
+ "session/jdbc/schema-@@platform@@.sql";
private static final String DEFAULT_TABLE_NAME = "SPRING_SESSION";
/**
* Path to the SQL file to use to initialize the database schema.
*/
private String schema = DEFAULT_SCHEMA_LOCATION;
/**
* Name of database table used to store sessions.
*/
private String tableName = DEFAULT_TABLE_NAME;
private final Initializer initializer = new Initializer();
public String getSchema() {
return this.schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
public String getTableName() {
return this.tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public Initializer getInitializer() {
return this.initializer;
}
public class Initializer {
/**
* Create the required session tables on startup if necessary. Enabled
* automatically if the default table name is set or a custom schema is
* configured.
*/
private Boolean enabled;
public boolean isEnabled() {
if (this.enabled != null) {
return this.enabled;
}
boolean defaultTableName = DEFAULT_TABLE_NAME
.equals(Jdbc.this.getTableName());
boolean customSchema = !DEFAULT_SCHEMA_LOCATION
.equals(Jdbc.this.getSchema());
return (defaultTableName || customSchema);
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
}
public static class Redis {
/**
* Namespace for keys used to store sessions.
*/
private String namespace = "";
/**
* Sessions flush mode.
*/
private RedisFlushMode flushMode = RedisFlushMode.ON_SAVE;
public String getNamespace() {
return this.namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public RedisFlushMode getFlushMode() {
return this.flushMode;
}
public void setFlushMode(RedisFlushMode flushMode) {
this.flushMode = flushMode;
}
}
}

5
spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

@ -383,6 +383,11 @@ @@ -383,6 +383,11 @@
"name": "spring.session.redis.flush-mode",
"defaultValue": "on-save"
},
{
"name": "spring.session.store-type",
"type": "org.springframework.boot.autoconfigure.session.StoreType",
"description": "Session store type."
},
{
"name": "spring.social.auto-connection-views",
"type": "java.lang.Boolean",

16
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java

@ -54,8 +54,8 @@ public class SessionAutoConfigurationJdbcTests @@ -54,8 +54,8 @@ public class SessionAutoConfigurationJdbcTests
JdbcOperationsSessionRepository.class);
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
.isEqualTo("SPRING_SESSION");
assertThat(this.context.getBean(SessionProperties.class).getJdbc()
.getInitializer().isEnabled()).isTrue();
assertThat(this.context.getBean(JdbcSessionProperties.class).getInitializer()
.isEnabled()).isTrue();
assertThat(this.context.getBean(JdbcOperations.class)
.queryForList("select * from SPRING_SESSION")).isEmpty();
}
@ -70,8 +70,8 @@ public class SessionAutoConfigurationJdbcTests @@ -70,8 +70,8 @@ public class SessionAutoConfigurationJdbcTests
JdbcOperationsSessionRepository.class);
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
.isEqualTo("SPRING_SESSION");
assertThat(this.context.getBean(SessionProperties.class).getJdbc()
.getInitializer().isEnabled()).isFalse();
assertThat(this.context.getBean(JdbcSessionProperties.class).getInitializer()
.isEnabled()).isFalse();
this.thrown.expect(BadSqlGrammarException.class);
assertThat(this.context.getBean(JdbcOperations.class)
.queryForList("select * from SPRING_SESSION")).isEmpty();
@ -88,8 +88,8 @@ public class SessionAutoConfigurationJdbcTests @@ -88,8 +88,8 @@ public class SessionAutoConfigurationJdbcTests
JdbcOperationsSessionRepository.class);
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
.isEqualTo("FOO_BAR");
assertThat(this.context.getBean(SessionProperties.class).getJdbc()
.getInitializer().isEnabled()).isTrue();
assertThat(this.context.getBean(JdbcSessionProperties.class).getInitializer()
.isEnabled()).isTrue();
assertThat(this.context.getBean(JdbcOperations.class)
.queryForList("select * from FOO_BAR")).isEmpty();
}
@ -104,8 +104,8 @@ public class SessionAutoConfigurationJdbcTests @@ -104,8 +104,8 @@ public class SessionAutoConfigurationJdbcTests
JdbcOperationsSessionRepository.class);
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
.isEqualTo("FOO_BAR");
assertThat(this.context.getBean(SessionProperties.class).getJdbc()
.getInitializer().isEnabled()).isFalse();
assertThat(this.context.getBean(JdbcSessionProperties.class).getInitializer()
.isEnabled()).isFalse();
this.thrown.expect(BadSqlGrammarException.class);
assertThat(this.context.getBean(JdbcOperations.class)
.queryForList("select * from SPRING_SESSION")).isEmpty();

7
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java

@ -96,13 +96,6 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat @@ -96,13 +96,6 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat
assertThat(getSessionTimeout(repository)).isEqualTo(3000);
}
@Test
public void springSessionTimeoutIsNotAValidProperty() {
this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("Could not bind");
load("spring.session.store-type=hash-map", "spring.session.timeout=3000");
}
@SuppressWarnings("unchecked")
@Test
public void filterIsRegisteredWithAsyncErrorAndRequestDispatcherTypes() {

Loading…
Cancel
Save