@@ -30,8 +30,10 @@ import org.springframework.data.mongodb.core.index.IndexInfo;
|
||||
import org.springframework.data.mongodb.core.index.IndexOperations;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.session.DelegatingIndexResolver;
|
||||
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||
import org.springframework.session.Session;
|
||||
import org.springframework.session.IndexResolver;
|
||||
import org.springframework.session.PrincipalNameIndexResolver;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
@@ -49,6 +51,8 @@ public abstract class AbstractMongoSessionConverter implements GenericConverter
|
||||
private static final Log LOG = LogFactory.getLog(AbstractMongoSessionConverter.class);
|
||||
private static final String SPRING_SECURITY_CONTEXT = "SPRING_SECURITY_CONTEXT";
|
||||
|
||||
private IndexResolver<MongoSession> indexResolver = new DelegatingIndexResolver<>(new PrincipalNameIndexResolver<>());
|
||||
|
||||
/**
|
||||
* Returns query to be executed to return sessions based on a particular index.
|
||||
*
|
||||
@@ -80,15 +84,9 @@ public abstract class AbstractMongoSessionConverter implements GenericConverter
|
||||
.ensureIndex(new Index(EXPIRE_AT_FIELD_NAME, Sort.Direction.ASC).named(EXPIRE_AT_FIELD_NAME).expire(0));
|
||||
}
|
||||
|
||||
protected String extractPrincipal(Session expiringSession) {
|
||||
|
||||
String resolvedPrincipal = AuthenticationParser.extractName(expiringSession.getAttribute(SPRING_SECURITY_CONTEXT));
|
||||
|
||||
if (resolvedPrincipal != null) {
|
||||
return resolvedPrincipal;
|
||||
} else {
|
||||
return expiringSession.getAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME);
|
||||
}
|
||||
protected String extractPrincipal(MongoSession expiringSession) {
|
||||
return this.indexResolver.resolveIndexesFor(expiringSession)
|
||||
.get(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME);
|
||||
}
|
||||
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
@@ -116,4 +114,8 @@ public abstract class AbstractMongoSessionConverter implements GenericConverter
|
||||
protected abstract DBObject convert(MongoSession session);
|
||||
|
||||
protected abstract MongoSession convert(Document sessionWrapper);
|
||||
|
||||
public void setIndexResolver(IndexResolver<MongoSession> indexResolver) {
|
||||
this.indexResolver = Assert.requireNonNull(indexResolver, "indexResolver must not be null!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2016 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
|
||||
*
|
||||
* https://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.session.data.mongo;
|
||||
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Utility class to extract principal name from {@code Authentication} object.
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
final class AuthenticationParser {
|
||||
|
||||
private static final String NAME_EXPRESSION = "authentication?.name";
|
||||
|
||||
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
|
||||
|
||||
private AuthenticationParser() {}
|
||||
|
||||
/**
|
||||
* Extracts principal name from authentication.
|
||||
*
|
||||
* @param authentication Authentication object
|
||||
* @return principal name
|
||||
*/
|
||||
@Nullable
|
||||
static String extractName(@Nullable Object authentication) {
|
||||
|
||||
if (authentication == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Expression expression = PARSER.parseExpression(NAME_EXPRESSION);
|
||||
|
||||
return expression.getValue(authentication, String.class);
|
||||
}
|
||||
}
|
||||
@@ -31,11 +31,13 @@ import org.springframework.core.serializer.support.DeserializingConverter;
|
||||
import org.springframework.core.serializer.support.SerializingConverter;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.session.IndexResolver;
|
||||
import org.springframework.session.config.SessionRepositoryCustomizer;
|
||||
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
|
||||
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
|
||||
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
|
||||
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||
import org.springframework.session.data.mongo.MongoSession;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.StringValueResolver;
|
||||
|
||||
@@ -57,6 +59,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
||||
private StringValueResolver embeddedValueResolver;
|
||||
private List<SessionRepositoryCustomizer<MongoIndexedSessionRepository>> sessionRepositoryCustomizers;
|
||||
private ClassLoader classLoader;
|
||||
private IndexResolver<MongoSession> indexResolver;
|
||||
|
||||
@Bean
|
||||
public MongoIndexedSessionRepository mongoSessionRepository(MongoOperations mongoOperations) {
|
||||
@@ -66,10 +69,19 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
||||
|
||||
if (this.mongoSessionConverter != null) {
|
||||
repository.setMongoSessionConverter(this.mongoSessionConverter);
|
||||
|
||||
if (this.indexResolver != null) {
|
||||
this.mongoSessionConverter.setIndexResolver(this.indexResolver);
|
||||
}
|
||||
} else {
|
||||
JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(),
|
||||
new DeserializingConverter(this.classLoader),
|
||||
Duration.ofSeconds(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL));
|
||||
|
||||
if (this.indexResolver != null) {
|
||||
mongoSessionConverter.setIndexResolver(this.indexResolver);
|
||||
}
|
||||
|
||||
repository.setMongoSessionConverter(mongoSessionConverter);
|
||||
}
|
||||
|
||||
@@ -129,4 +141,8 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
||||
this.embeddedValueResolver = resolver;
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
public void setIndexResolver(IndexResolver<MongoSession> indexResolver) {
|
||||
this.indexResolver = indexResolver;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,10 +32,12 @@ import org.springframework.core.serializer.support.SerializingConverter;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
|
||||
import org.springframework.session.IndexResolver;
|
||||
import org.springframework.session.config.ReactiveSessionRepositoryCustomizer;
|
||||
import org.springframework.session.config.annotation.web.server.SpringWebSessionConfiguration;
|
||||
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
|
||||
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
|
||||
import org.springframework.session.data.mongo.MongoSession;
|
||||
import org.springframework.session.data.mongo.ReactiveMongoSessionRepository;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.StringValueResolver;
|
||||
@@ -58,6 +60,8 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
|
||||
|
||||
@Autowired(required = false) private MongoOperations mongoOperations;
|
||||
private ClassLoader classLoader;
|
||||
private IndexResolver<MongoSession> indexResolver;
|
||||
|
||||
|
||||
@Bean
|
||||
public ReactiveMongoSessionRepository reactiveMongoSessionRepository(ReactiveMongoOperations operations) {
|
||||
@@ -66,10 +70,20 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
|
||||
|
||||
if (this.mongoSessionConverter != null) {
|
||||
repository.setMongoSessionConverter(this.mongoSessionConverter);
|
||||
|
||||
if (this.indexResolver != null) {
|
||||
this.mongoSessionConverter.setIndexResolver(this.indexResolver);
|
||||
}
|
||||
|
||||
} else {
|
||||
JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(),
|
||||
new DeserializingConverter(this.classLoader),
|
||||
Duration.ofSeconds(ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL));
|
||||
|
||||
if (this.indexResolver != null) {
|
||||
mongoSessionConverter.setIndexResolver(this.indexResolver);
|
||||
}
|
||||
|
||||
repository.setMongoSessionConverter(mongoSessionConverter);
|
||||
}
|
||||
|
||||
@@ -146,4 +160,9 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
|
||||
ObjectProvider<ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository>> sessionRepositoryCustomizers) {
|
||||
this.sessionRepositoryCustomizers = sessionRepositoryCustomizers.orderedStream().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
public void setIndexResolver(IndexResolver<MongoSession> indexResolver) {
|
||||
this.indexResolver = indexResolver;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user