Skip to content

Commit f39a7e7

Browse files
committed
Reinstate Spring Session MongoDB auto-config
1 parent 2892039 commit f39a7e7

File tree

10 files changed

+183
-1
lines changed

10 files changed

+183
-1
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,11 @@
531531
<artifactId>spring-session-core</artifactId>
532532
<optional>true</optional>
533533
</dependency>
534+
<dependency>
535+
<groupId>org.springframework.session</groupId>
536+
<artifactId>spring-session-data-mongodb</artifactId>
537+
<optional>true</optional>
538+
</dependency>
534539
<dependency>
535540
<groupId>org.springframework.session</groupId>
536541
<artifactId>spring-session-data-redis</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.session;
18+
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
22+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
23+
import org.springframework.context.annotation.Conditional;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.data.mongodb.core.MongoOperations;
26+
import org.springframework.session.SessionRepository;
27+
import org.springframework.session.data.mongo.config.annotation.web.http.MongoHttpSessionConfiguration;
28+
29+
/**
30+
* MongoDB backed session configuration.
31+
*
32+
* @author Eddú Meléndez
33+
* @author Stephane Nicoll
34+
* @author Vedran Pavic
35+
*/
36+
@Configuration
37+
@ConditionalOnMissingBean(SessionRepository.class)
38+
@ConditionalOnBean(MongoOperations.class)
39+
@Conditional(SessionCondition.class)
40+
@EnableConfigurationProperties(MongoSessionProperties.class)
41+
class MongoSessionConfiguration {
42+
43+
@Configuration
44+
public static class SpringBootMongoHttpSessionConfiguration
45+
extends MongoHttpSessionConfiguration {
46+
47+
@Autowired
48+
public void customize(SessionProperties sessionProperties,
49+
MongoSessionProperties mongoSessionProperties) {
50+
Integer timeout = sessionProperties.getTimeout();
51+
if (timeout != null) {
52+
setMaxInactiveIntervalInSeconds(timeout);
53+
}
54+
setCollectionName(mongoSessionProperties.getCollectionName());
55+
}
56+
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.session;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Configuration properties for MongoDB backed Spring Session.
23+
*
24+
* @author Vedran Pavic
25+
* @since 2.0.0
26+
*/
27+
@ConfigurationProperties(prefix = "spring.session.mongodb")
28+
public class MongoSessionProperties {
29+
30+
/**
31+
* Collection name used to store sessions.
32+
*/
33+
private String collectionName = "sessions";
34+
35+
public String getCollectionName() {
36+
return this.collectionName;
37+
}
38+
39+
public void setCollectionName(String collectionName) {
40+
this.collectionName = collectionName;
41+
}
42+
43+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
3030
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
3131
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
32+
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
3233
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration.SessionConfigurationImportSelector;
3334
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration.SessionRepositoryValidator;
3435
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -55,7 +56,8 @@
5556
@ConditionalOnWebApplication(type = Type.SERVLET)
5657
@EnableConfigurationProperties(SessionProperties.class)
5758
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, HazelcastAutoConfiguration.class,
58-
JdbcTemplateAutoConfiguration.class, RedisAutoConfiguration.class })
59+
JdbcTemplateAutoConfiguration.class, MongoAutoConfiguration.class,
60+
RedisAutoConfiguration.class })
5961
@Import({ SessionConfigurationImportSelector.class, SessionRepositoryValidator.class,
6062
SessionRepositoryFilterConfiguration.class })
6163
public class SessionAutoConfiguration {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ final class SessionStoreMappings {
3535
static {
3636
Map<StoreType, Class<?>> mappings = new HashMap<>();
3737
mappings.put(StoreType.REDIS, RedisSessionConfiguration.class);
38+
mappings.put(StoreType.MONGODB, MongoSessionConfiguration.class);
3839
mappings.put(StoreType.JDBC, JdbcSessionConfiguration.class);
3940
mappings.put(StoreType.HAZELCAST, HazelcastSessionConfiguration.class);
4041
mappings.put(StoreType.HASH_MAP, HashMapSessionConfiguration.class);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public enum StoreType {
3131
*/
3232
REDIS,
3333

34+
/**
35+
* MongoDB backed sessions.
36+
*/
37+
MONGODB,
38+
3439
/**
3540
* JDBC backed sessions.
3641
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.session;
18+
19+
import java.util.Arrays;
20+
21+
import org.junit.Test;
22+
23+
import org.springframework.beans.DirectFieldAccessor;
24+
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
25+
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
27+
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* MongoDB specific tests for {@link SessionAutoConfiguration}.
33+
*
34+
* @author Vedran Pavic
35+
*/
36+
public class SessionAutoConfigurationMongoTests
37+
extends AbstractSessionAutoConfigurationTests {
38+
39+
@Test
40+
public void mongoSessionStore() {
41+
load(Arrays.asList(EmbeddedMongoAutoConfiguration.class,
42+
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class),
43+
"spring.session.store-type=mongodb");
44+
validateSessionRepository(MongoOperationsSessionRepository.class);
45+
}
46+
47+
@Test
48+
public void mongoSessionStoreWithCustomizations() {
49+
load(Arrays.asList(EmbeddedMongoAutoConfiguration.class,
50+
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class),
51+
"spring.session.store-type=mongodb",
52+
"spring.session.mongodb.collection-name=foobar");
53+
MongoOperationsSessionRepository repository = validateSessionRepository(
54+
MongoOperationsSessionRepository.class);
55+
assertThat(new DirectFieldAccessor(repository).getPropertyValue("collectionName"))
56+
.isEqualTo("foobar");
57+
}
58+
59+
}

spring-boot-dependencies/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
<spring-security-jwt.version>1.0.8.RELEASE</spring-security-jwt.version>
171171
<spring-security-oauth.version>2.1.2.BUILD-SNAPSHOT</spring-security-oauth.version>
172172
<spring-session.version>2.0.0.BUILD-SNAPSHOT</spring-session.version>
173+
<spring-session-mongodb.version>2.0.0.BUILD-SNAPSHOT</spring-session-mongodb.version>
173174
<spring-social.version>2.0.0.M3</spring-social.version>
174175
<spring-social-facebook.version>3.0.0.M2</spring-social-facebook.version>
175176
<spring-social-linkedin.version>2.0.0.M2</spring-social-linkedin.version>
@@ -2318,6 +2319,11 @@
23182319
</exclusion>
23192320
</exclusions>
23202321
</dependency>
2322+
<dependency>
2323+
<groupId>org.springframework.session</groupId>
2324+
<artifactId>spring-session-data-mongodb</artifactId>
2325+
<version>${spring-session-mongodb.version}</version>
2326+
</dependency>
23212327
<dependency>
23222328
<groupId>org.springframework.session</groupId>
23232329
<artifactId>spring-session-data-redis</artifactId>

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ content into your application; rather pick only the properties that you need.
419419
spring.session.jdbc.initializer.enabled= # Create the required session tables on startup if necessary. Enabled automatically if the default table name is set or a custom schema is configured.
420420
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
421421
spring.session.jdbc.table-name=SPRING_SESSION # Name of database table used to store sessions.
422+
spring.session.mongodb.collection-name=sessions # Collection name used to store sessions.
422423
spring.session.redis.flush-mode=on-save # Sessions flush mode.
423424
spring.session.redis.namespace= # Namespace for keys used to store sessions.
424425
spring.session.store-type= # Session store type.

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5280,6 +5280,7 @@ classes for more details.
52805280
Spring Boot provides Spring Session auto-configuration for a wide range of stores:
52815281

52825282
* JDBC
5283+
* MongoDB
52835284
* Redis
52845285
* Hazelcast
52855286
* HashMap

0 commit comments

Comments
 (0)