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

Commit db93654

Browse files
committed
Leverage Spring Session's IndexResolver.
Resolves #104.
1 parent 1c7d5e5 commit db93654

File tree

7 files changed

+173
-108
lines changed

7 files changed

+173
-108
lines changed

src/main/java/org/springframework/session/data/mongo/AbstractMongoSessionConverter.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
import org.springframework.data.mongodb.core.index.IndexOperations;
3131
import org.springframework.data.mongodb.core.query.Query;
3232
import org.springframework.lang.Nullable;
33+
import org.springframework.session.DelegatingIndexResolver;
3334
import org.springframework.session.FindByIndexNameSessionRepository;
34-
import org.springframework.session.Session;
35+
import org.springframework.session.IndexResolver;
36+
import org.springframework.session.PrincipalNameIndexResolver;
3537

3638
import com.mongodb.DBObject;
3739

@@ -49,6 +51,8 @@ public abstract class AbstractMongoSessionConverter implements GenericConverter
4951
private static final Log LOG = LogFactory.getLog(AbstractMongoSessionConverter.class);
5052
private static final String SPRING_SECURITY_CONTEXT = "SPRING_SECURITY_CONTEXT";
5153

54+
private IndexResolver<MongoSession> indexResolver = new DelegatingIndexResolver<>(new PrincipalNameIndexResolver<>());
55+
5256
/**
5357
* Returns query to be executed to return sessions based on a particular index.
5458
*
@@ -80,15 +84,9 @@ protected void ensureIndexes(IndexOperations sessionCollectionIndexes) {
8084
.ensureIndex(new Index(EXPIRE_AT_FIELD_NAME, Sort.Direction.ASC).named(EXPIRE_AT_FIELD_NAME).expire(0));
8185
}
8286

83-
protected String extractPrincipal(Session expiringSession) {
84-
85-
String resolvedPrincipal = AuthenticationParser.extractName(expiringSession.getAttribute(SPRING_SECURITY_CONTEXT));
86-
87-
if (resolvedPrincipal != null) {
88-
return resolvedPrincipal;
89-
} else {
90-
return expiringSession.getAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME);
91-
}
87+
protected String extractPrincipal(MongoSession expiringSession) {
88+
return this.indexResolver.resolveIndexesFor(expiringSession)
89+
.get(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME);
9290
}
9391

9492
public Set<ConvertiblePair> getConvertibleTypes() {
@@ -116,4 +114,8 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
116114
protected abstract DBObject convert(MongoSession session);
117115

118116
protected abstract MongoSession convert(Document sessionWrapper);
117+
118+
public void setIndexResolver(IndexResolver<MongoSession> indexResolver) {
119+
this.indexResolver = Assert.requireNonNull(indexResolver, "indexResolver must not be null!");
120+
}
119121
}

src/main/java/org/springframework/session/data/mongo/AuthenticationParser.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
import org.springframework.core.serializer.support.SerializingConverter;
3232
import org.springframework.core.type.AnnotationMetadata;
3333
import org.springframework.data.mongodb.core.MongoOperations;
34+
import org.springframework.session.IndexResolver;
3435
import org.springframework.session.config.SessionRepositoryCustomizer;
3536
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
3637
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
3738
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
3839
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
40+
import org.springframework.session.data.mongo.MongoSession;
3941
import org.springframework.util.StringUtils;
4042
import org.springframework.util.StringValueResolver;
4143

@@ -57,6 +59,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
5759
private StringValueResolver embeddedValueResolver;
5860
private List<SessionRepositoryCustomizer<MongoIndexedSessionRepository>> sessionRepositoryCustomizers;
5961
private ClassLoader classLoader;
62+
private IndexResolver<MongoSession> indexResolver;
6063

6164
@Bean
6265
public MongoIndexedSessionRepository mongoSessionRepository(MongoOperations mongoOperations) {
@@ -66,10 +69,19 @@ public MongoIndexedSessionRepository mongoSessionRepository(MongoOperations mong
6669

6770
if (this.mongoSessionConverter != null) {
6871
repository.setMongoSessionConverter(this.mongoSessionConverter);
72+
73+
if (this.indexResolver != null) {
74+
this.mongoSessionConverter.setIndexResolver(this.indexResolver);
75+
}
6976
} else {
7077
JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(),
7178
new DeserializingConverter(this.classLoader),
7279
Duration.ofSeconds(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL));
80+
81+
if (this.indexResolver != null) {
82+
mongoSessionConverter.setIndexResolver(this.indexResolver);
83+
}
84+
7385
repository.setMongoSessionConverter(mongoSessionConverter);
7486
}
7587

@@ -129,4 +141,8 @@ public void setEmbeddedValueResolver(StringValueResolver resolver) {
129141
this.embeddedValueResolver = resolver;
130142
}
131143

144+
@Autowired(required = false)
145+
public void setIndexResolver(IndexResolver<MongoSession> indexResolver) {
146+
this.indexResolver = indexResolver;
147+
}
132148
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
import org.springframework.core.type.AnnotationMetadata;
3333
import org.springframework.data.mongodb.core.MongoOperations;
3434
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
35+
import org.springframework.session.IndexResolver;
3536
import org.springframework.session.config.ReactiveSessionRepositoryCustomizer;
3637
import org.springframework.session.config.annotation.web.server.SpringWebSessionConfiguration;
3738
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
3839
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
40+
import org.springframework.session.data.mongo.MongoSession;
3941
import org.springframework.session.data.mongo.ReactiveMongoSessionRepository;
4042
import org.springframework.util.StringUtils;
4143
import org.springframework.util.StringValueResolver;
@@ -58,6 +60,8 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
5860

5961
@Autowired(required = false) private MongoOperations mongoOperations;
6062
private ClassLoader classLoader;
63+
private IndexResolver<MongoSession> indexResolver;
64+
6165

6266
@Bean
6367
public ReactiveMongoSessionRepository reactiveMongoSessionRepository(ReactiveMongoOperations operations) {
@@ -66,10 +70,20 @@ public ReactiveMongoSessionRepository reactiveMongoSessionRepository(ReactiveMon
6670

6771
if (this.mongoSessionConverter != null) {
6872
repository.setMongoSessionConverter(this.mongoSessionConverter);
73+
74+
if (this.indexResolver != null) {
75+
this.mongoSessionConverter.setIndexResolver(this.indexResolver);
76+
}
77+
6978
} else {
7079
JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(),
7180
new DeserializingConverter(this.classLoader),
7281
Duration.ofSeconds(ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL));
82+
83+
if (this.indexResolver != null) {
84+
mongoSessionConverter.setIndexResolver(this.indexResolver);
85+
}
86+
7387
repository.setMongoSessionConverter(mongoSessionConverter);
7488
}
7589

@@ -146,4 +160,9 @@ public void setSessionRepositoryCustomizers(
146160
ObjectProvider<ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository>> sessionRepositoryCustomizers) {
147161
this.sessionRepositoryCustomizers = sessionRepositoryCustomizers.orderedStream().collect(Collectors.toList());
148162
}
163+
164+
@Autowired(required = false)
165+
public void setIndexResolver(IndexResolver<MongoSession> indexResolver) {
166+
this.indexResolver = indexResolver;
167+
}
149168
}

src/test/java/org/springframework/session/data/mongo/AuthenticationParserTest.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@
3333
import org.springframework.data.mongodb.core.MongoOperations;
3434
import org.springframework.data.mongodb.core.index.IndexOperations;
3535
import org.springframework.mock.env.MockEnvironment;
36+
import org.springframework.session.IndexResolver;
3637
import org.springframework.session.config.SessionRepositoryCustomizer;
3738
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
39+
import org.springframework.session.data.mongo.JacksonMongoSessionConverter;
3840
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
41+
import org.springframework.session.data.mongo.MongoSession;
3942
import org.springframework.test.util.ReflectionTestUtils;
4043

4144
/**
@@ -156,6 +159,32 @@ public void sessionRepositoryCustomizer() {
156159
assertThat(sessionRepository).hasFieldOrPropertyWithValue("maxInactiveIntervalInSeconds", 10000);
157160
}
158161

162+
@Test
163+
void customIndexResolverConfigurationWithDefaultMongoSessionConverter() {
164+
165+
registerAndRefresh(MongoConfiguration.class, CustomIndexResolverConfigurationWithDefaultMongoSessionConverter.class);
166+
167+
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
168+
IndexResolver<MongoSession> indexResolver = this.context.getBean(IndexResolver.class);
169+
170+
assertThat(repository).isNotNull();
171+
assertThat(indexResolver).isNotNull();
172+
assertThat(repository).extracting("mongoSessionConverter").hasFieldOrPropertyWithValue("indexResolver", indexResolver);
173+
}
174+
175+
@Test
176+
void customIndexResolverConfigurationWithProvidedMongoSessionConverter() {
177+
178+
registerAndRefresh(MongoConfiguration.class, CustomIndexResolverConfigurationWithProvidedMongoSessionConverter.class);
179+
180+
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
181+
IndexResolver<MongoSession> indexResolver = this.context.getBean(IndexResolver.class);
182+
183+
assertThat(repository).isNotNull();
184+
assertThat(indexResolver).isNotNull();
185+
assertThat(repository).extracting("mongoSessionConverter").hasFieldOrPropertyWithValue("indexResolver", indexResolver);
186+
}
187+
159188
private void registerAndRefresh(Class<?>... annotatedClasses) {
160189

161190
this.context.register(annotatedClasses);
@@ -264,4 +293,30 @@ public SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionReposit
264293
}
265294
}
266295

296+
@Configuration
297+
@EnableMongoHttpSession
298+
static class CustomIndexResolverConfigurationWithDefaultMongoSessionConverter {
299+
300+
@Bean
301+
@SuppressWarnings("unchecked")
302+
public IndexResolver<MongoSession> indexResolver() {
303+
return mock(IndexResolver.class);
304+
}
305+
}
306+
307+
@Configuration
308+
@EnableMongoHttpSession
309+
static class CustomIndexResolverConfigurationWithProvidedMongoSessionConverter {
310+
311+
@Bean
312+
public AbstractMongoSessionConverter mongoSessionConverter() {
313+
return new JacksonMongoSessionConverter();
314+
}
315+
316+
@Bean
317+
@SuppressWarnings("unchecked")
318+
public IndexResolver<MongoSession> indexResolver() {
319+
return mock(IndexResolver.class);
320+
}
321+
}
267322
}

0 commit comments

Comments
 (0)