Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 1c7d5e5

Browse files
committed
Implement support for Spring Session's repository customiers.
Resolves #105.
1 parent ebac581 commit 1c7d5e5

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

src/main/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfiguration.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
package org.springframework.session.data.mongo.config.annotation.web.http;
1717

1818
import java.time.Duration;
19+
import java.util.List;
20+
import java.util.stream.Collectors;
1921

2022
import org.springframework.beans.factory.BeanClassLoaderAware;
23+
import org.springframework.beans.factory.ObjectProvider;
2124
import org.springframework.beans.factory.annotation.Autowired;
2225
import org.springframework.context.EmbeddedValueResolverAware;
2326
import org.springframework.context.annotation.Bean;
@@ -28,6 +31,7 @@
2831
import org.springframework.core.serializer.support.SerializingConverter;
2932
import org.springframework.core.type.AnnotationMetadata;
3033
import org.springframework.data.mongodb.core.MongoOperations;
34+
import org.springframework.session.config.SessionRepositoryCustomizer;
3135
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
3236
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
3337
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
@@ -51,6 +55,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
5155
private Integer maxInactiveIntervalInSeconds;
5256
private String collectionName;
5357
private StringValueResolver embeddedValueResolver;
58+
private List<SessionRepositoryCustomizer<MongoIndexedSessionRepository>> sessionRepositoryCustomizers;
5459
private ClassLoader classLoader;
5560

5661
@Bean
@@ -72,6 +77,9 @@ public MongoIndexedSessionRepository mongoSessionRepository(MongoOperations mong
7277
repository.setCollectionName(this.collectionName);
7378
}
7479

80+
this.sessionRepositoryCustomizers
81+
.forEach(sessionRepositoryCustomizer -> sessionRepositoryCustomizer.customize(repository));
82+
7583
return repository;
7684
}
7785

@@ -105,6 +113,12 @@ public void setMongoSessionConverter(AbstractMongoSessionConverter mongoSessionC
105113
this.mongoSessionConverter = mongoSessionConverter;
106114
}
107115

116+
@Autowired(required = false)
117+
public void setSessionRepositoryCustomizers(
118+
ObjectProvider<SessionRepositoryCustomizer<MongoIndexedSessionRepository>> sessionRepositoryCustomizers) {
119+
this.sessionRepositoryCustomizers = sessionRepositoryCustomizers.orderedStream().collect(Collectors.toList());
120+
}
121+
108122
@Override
109123
public void setBeanClassLoader(ClassLoader classLoader) {
110124
this.classLoader = classLoader;

src/main/java/org/springframework/session/data/mongo/config/annotation/web/reactive/ReactiveMongoWebSessionConfiguration.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
package org.springframework.session.data.mongo.config.annotation.web.reactive;
1717

1818
import java.time.Duration;
19+
import java.util.List;
20+
import java.util.stream.Collectors;
1921

2022
import org.springframework.beans.factory.BeanClassLoaderAware;
23+
import org.springframework.beans.factory.ObjectProvider;
2124
import org.springframework.beans.factory.annotation.Autowired;
2225
import org.springframework.context.EmbeddedValueResolverAware;
2326
import org.springframework.context.annotation.Bean;
@@ -29,6 +32,7 @@
2932
import org.springframework.core.type.AnnotationMetadata;
3033
import org.springframework.data.mongodb.core.MongoOperations;
3134
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
35+
import org.springframework.session.config.ReactiveSessionRepositoryCustomizer;
3236
import org.springframework.session.config.annotation.web.server.SpringWebSessionConfiguration;
3337
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
3438
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
@@ -50,6 +54,7 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
5054
private Integer maxInactiveIntervalInSeconds;
5155
private String collectionName;
5256
private StringValueResolver embeddedValueResolver;
57+
private List<ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository>> sessionRepositoryCustomizers;
5358

5459
@Autowired(required = false) private MongoOperations mongoOperations;
5560
private ClassLoader classLoader;
@@ -80,6 +85,9 @@ public ReactiveMongoSessionRepository reactiveMongoSessionRepository(ReactiveMon
8085
repository.setBlockingMongoOperations(this.mongoOperations);
8186
}
8287

88+
this.sessionRepositoryCustomizers
89+
.forEach(sessionRepositoryCustomizer -> sessionRepositoryCustomizer.customize(repository));
90+
8391
return repository;
8492
}
8593

@@ -132,4 +140,10 @@ public String getCollectionName() {
132140
public void setCollectionName(String collectionName) {
133141
this.collectionName = collectionName;
134142
}
143+
144+
@Autowired(required = false)
145+
public void setSessionRepositoryCustomizers(
146+
ObjectProvider<ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository>> sessionRepositoryCustomizers) {
147+
this.sessionRepositoryCustomizers = sessionRepositoryCustomizers.orderedStream().collect(Collectors.toList());
148+
}
135149
}

src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.net.UnknownHostException;
2323

2424
import org.junit.jupiter.api.AfterEach;
25+
import org.junit.jupiter.api.Order;
2526
import org.junit.jupiter.api.Test;
2627
import org.springframework.beans.factory.UnsatisfiedDependencyException;
2728
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -32,6 +33,7 @@
3233
import org.springframework.data.mongodb.core.MongoOperations;
3334
import org.springframework.data.mongodb.core.index.IndexOperations;
3435
import org.springframework.mock.env.MockEnvironment;
36+
import org.springframework.session.config.SessionRepositoryCustomizer;
3537
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
3638
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
3739
import org.springframework.test.util.ReflectionTestUtils;
@@ -144,6 +146,16 @@ public void resolveCollectionNameByPropertyPlaceholder() {
144146
assertThat(ReflectionTestUtils.getField(configuration, "collectionName")).isEqualTo(COLLECTION_NAME);
145147
}
146148

149+
@Test
150+
public void sessionRepositoryCustomizer() {
151+
152+
registerAndRefresh(MongoConfiguration.class, SessionRepositoryCustomizerConfiguration.class);
153+
154+
MongoIndexedSessionRepository sessionRepository = this.context.getBean(MongoIndexedSessionRepository.class);
155+
156+
assertThat(sessionRepository).hasFieldOrPropertyWithValue("maxInactiveIntervalInSeconds", 10000);
157+
}
158+
147159
private void registerAndRefresh(Class<?>... annotatedClasses) {
148160

149161
this.context.register(annotatedClasses);
@@ -236,4 +248,20 @@ public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer
236248

237249
}
238250

251+
@EnableMongoHttpSession
252+
static class SessionRepositoryCustomizerConfiguration {
253+
254+
@Bean
255+
@Order(0)
256+
public SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizerOne() {
257+
return sessionRepository -> sessionRepository.setMaxInactiveIntervalInSeconds(0);
258+
}
259+
260+
@Bean
261+
@Order(1)
262+
public SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizerTwo() {
263+
return sessionRepository -> sessionRepository.setMaxInactiveIntervalInSeconds(10000);
264+
}
265+
}
266+
239267
}

src/test/java/org/springframework/session/data/mongo/config/annotation/web/reactive/ReactiveMongoWebSessionConfigurationTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Collections;
2323

2424
import org.junit.jupiter.api.AfterEach;
25+
import org.junit.jupiter.api.Order;
2526
import org.junit.jupiter.api.Test;
2627
import org.springframework.beans.factory.UnsatisfiedDependencyException;
2728
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -30,6 +31,7 @@
3031
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
3132
import org.springframework.data.mongodb.core.index.IndexOperations;
3233
import org.springframework.session.ReactiveSessionRepository;
34+
import org.springframework.session.config.ReactiveSessionRepositoryCustomizer;
3335
import org.springframework.session.config.annotation.web.server.EnableSpringWebSession;
3436
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
3537
import org.springframework.session.data.mongo.JacksonMongoSessionConverter;
@@ -163,6 +165,18 @@ public void overrideCollectionAndInactiveIntervalThroughConfigurationOptions() {
163165
assertThat(repository.getMaxInactiveIntervalInSeconds()).isEqualTo(123);
164166
}
165167

168+
@Test
169+
public void sessionRepositoryCustomizer() {
170+
171+
this.context = new AnnotationConfigApplicationContext();
172+
this.context.register(SessionRepositoryCustomizerConfiguration.class);
173+
this.context.refresh();
174+
175+
ReactiveMongoSessionRepository repository = this.context.getBean(ReactiveMongoSessionRepository.class);
176+
177+
assertThat(repository).hasFieldOrPropertyWithValue("maxInactiveIntervalInSeconds", 10000);
178+
}
179+
166180
/**
167181
* Reflectively extract the {@link AbstractMongoSessionConverter} from the {@link ReactiveMongoSessionRepository}.
168182
* This is to avoid expanding the surface area of the API.
@@ -263,4 +277,26 @@ ReactiveMongoOperations reactiveMongoOperations() {
263277
return mock(ReactiveMongoOperations.class);
264278
}
265279
}
280+
281+
@EnableMongoWebSession
282+
static class SessionRepositoryCustomizerConfiguration {
283+
284+
@Bean
285+
ReactiveMongoOperations operations() {
286+
return mock(ReactiveMongoOperations.class);
287+
}
288+
289+
@Bean
290+
@Order(0)
291+
public ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> sessionRepositoryCustomizerOne() {
292+
return sessionRepository -> sessionRepository.setMaxInactiveIntervalInSeconds(0);
293+
}
294+
295+
@Bean
296+
@Order(1)
297+
public ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> sessionRepositoryCustomizerTwo() {
298+
return sessionRepository -> sessionRepository.setMaxInactiveIntervalInSeconds(10000);
299+
}
300+
301+
}
266302
}

0 commit comments

Comments
 (0)