diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HashMapSessionConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HashMapSessionConfiguration.java index 28f161ea0f15..00c4428aaa1a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HashMapSessionConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HashMapSessionConfiguration.java @@ -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 @@ @EnableSpringHttpSession @Conditional(SessionCondition.class) @ConditionalOnMissingBean(SessionRepository.class) +@EnableConfigurationProperties(ServerProperties.class) class HashMapSessionConfiguration { + private final ServerProperties serverProperties; + + HashMapSessionConfiguration(ObjectProvider 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; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionConfiguration.java index d2726fef2f84..a620bf8d2ffc 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionConfiguration.java @@ -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 @@ 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 @@ * @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) { + 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()); } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionProperties.java new file mode 100644 index 000000000000..df3f9bf8b9ad --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/HazelcastSessionProperties.java @@ -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; + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java index 621573958f48..1a9b72fbe106 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java @@ -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 @@ 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 @@ * @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 @@ public JdbcSessionDatabaseInitializer jdbcSessionDatabaseInitializer( 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) { + 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()); } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java index b8c2bc8924cd..5894555ff811 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java @@ -30,13 +30,13 @@ */ 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 diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionProperties.java new file mode 100644 index 000000000000..8b193bfb1a7c --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionProperties.java @@ -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; + } + + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java index 0f7e9bd73c55..fdb6230f0832 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java @@ -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 @@ * @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) { 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()); } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionProperties.java new file mode 100644 index 000000000000..3d87e1cb6d71 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionProperties.java @@ -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; + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java index f1c9fb32fc2b..fc1fdc18bf47 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java @@ -31,10 +31,11 @@ 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 @@ @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 String[] selectImports(AnnotationMetadata importingClassMetadata) { */ static class SessionRepositoryValidator { - private SessionProperties sessionProperties; + private Environment environment; private ObjectProvider> sessionRepositoryProvider; - SessionRepositoryValidator(SessionProperties sessionProperties, + SessionRepositoryValidator(Environment environment, ObjectProvider> 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) { diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java deleted file mode 100644 index 64fce887292c..000000000000 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java +++ /dev/null @@ -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 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; - } - - } - -} diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 04bf53776c35..c60bf0cc5d15 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -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", diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java index 82b1277544ec..e336e6880fa1 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java @@ -54,8 +54,8 @@ public void defaultConfig() { 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 void disableDatabaseInitializer() { 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 void customTableName() { 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 void customTableNameWithDefaultSchemaDisablesInitializer() { 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(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java index 90eabbccac60..3c9917a63d26 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java @@ -96,13 +96,6 @@ public void hashMapSessionStoreCustomTimeout() { 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() {