Introduce MongoIndexedSessionRepository.
This commit is contained in:
@@ -0,0 +1,180 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2019 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 static org.springframework.session.data.mongo.MongoSessionUtils.*;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.bson.Document;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.context.ApplicationEvent;
|
||||||
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
|
import org.springframework.context.ApplicationEventPublisherAware;
|
||||||
|
import org.springframework.data.mongodb.core.MongoOperations;
|
||||||
|
import org.springframework.data.mongodb.core.index.IndexOperations;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||||
|
import org.springframework.session.events.SessionCreatedEvent;
|
||||||
|
import org.springframework.session.events.SessionDeletedEvent;
|
||||||
|
import org.springframework.session.events.SessionExpiredEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Session repository implementation which stores sessions in Mongo. Uses {@link AbstractMongoSessionConverter} to
|
||||||
|
* transform session objects from/to native Mongo representation ({@code DBObject}). Repository is also responsible for
|
||||||
|
* removing expired sessions from database. Cleanup is done every minute.
|
||||||
|
*
|
||||||
|
* @author Jakub Kubrynski
|
||||||
|
* @author Greg Turnquist
|
||||||
|
* @since 2.2.0
|
||||||
|
*/
|
||||||
|
public class MongoIndexedSessionRepository
|
||||||
|
implements FindByIndexNameSessionRepository<MongoSession>, ApplicationEventPublisherAware, InitializingBean {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default time period in seconds in which a session will expire.
|
||||||
|
*/
|
||||||
|
public static final int DEFAULT_INACTIVE_INTERVAL = 1800;
|
||||||
|
/**
|
||||||
|
* the default collection name for storing session.
|
||||||
|
*/
|
||||||
|
public static final String DEFAULT_COLLECTION_NAME = "sessions";
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(MongoIndexedSessionRepository.class);
|
||||||
|
private final MongoOperations mongoOperations;
|
||||||
|
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
|
||||||
|
private String collectionName = DEFAULT_COLLECTION_NAME;
|
||||||
|
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
|
||||||
|
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
|
||||||
|
private ApplicationEventPublisher eventPublisher;
|
||||||
|
|
||||||
|
public MongoIndexedSessionRepository(MongoOperations mongoOperations) {
|
||||||
|
this.mongoOperations = mongoOperations;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MongoSession createSession() {
|
||||||
|
|
||||||
|
MongoSession session = new MongoSession();
|
||||||
|
|
||||||
|
if (this.maxInactiveIntervalInSeconds != null) {
|
||||||
|
session.setMaxInactiveInterval(Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
|
||||||
|
}
|
||||||
|
|
||||||
|
publishEvent(new SessionCreatedEvent(this, session));
|
||||||
|
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(MongoSession session) {
|
||||||
|
this.mongoOperations.save(Assert.requireNonNull(convertToDBObject(this.mongoSessionConverter, session),
|
||||||
|
"convertToDBObject must not null!"), this.collectionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public MongoSession findById(String id) {
|
||||||
|
|
||||||
|
Document sessionWrapper = findSession(id);
|
||||||
|
|
||||||
|
if (sessionWrapper == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
MongoSession session = convertToSession(this.mongoSessionConverter, sessionWrapper);
|
||||||
|
|
||||||
|
if (session != null && session.isExpired()) {
|
||||||
|
publishEvent(new SessionExpiredEvent(this, session));
|
||||||
|
deleteById(id);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Currently this repository allows only querying against {@code PRINCIPAL_NAME_INDEX_NAME}.
|
||||||
|
*
|
||||||
|
* @param indexName the name if the index (i.e. {@link FindByIndexNameSessionRepository#PRINCIPAL_NAME_INDEX_NAME})
|
||||||
|
* @param indexValue the value of the index to search for.
|
||||||
|
* @return sessions map
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Map<String, MongoSession> findByIndexNameAndIndexValue(String indexName, String indexValue) {
|
||||||
|
|
||||||
|
return Optional.ofNullable(this.mongoSessionConverter.getQueryForIndex(indexName, indexValue))
|
||||||
|
.map(query -> this.mongoOperations.find(query, Document.class, this.collectionName))
|
||||||
|
.orElse(Collections.emptyList()).stream()
|
||||||
|
.map(dbSession -> convertToSession(this.mongoSessionConverter, dbSession))
|
||||||
|
.collect(Collectors.toMap(MongoSession::getId, mapSession -> mapSession));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteById(String id) {
|
||||||
|
|
||||||
|
Optional.ofNullable(findSession(id)).ifPresent(document -> {
|
||||||
|
MongoSession session = convertToSession(this.mongoSessionConverter, document);
|
||||||
|
if (session != null) {
|
||||||
|
publishEvent(new SessionDeletedEvent(this, session));
|
||||||
|
}
|
||||||
|
this.mongoOperations.remove(document, this.collectionName);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() {
|
||||||
|
|
||||||
|
IndexOperations indexOperations = this.mongoOperations.indexOps(this.collectionName);
|
||||||
|
this.mongoSessionConverter.ensureIndexes(indexOperations);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Document findSession(String id) {
|
||||||
|
return this.mongoOperations.findById(id, Document.class, this.collectionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
|
||||||
|
this.eventPublisher = eventPublisher;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void publishEvent(ApplicationEvent event) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.eventPublisher.publishEvent(event);
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
logger.error("Error publishing " + event + ".", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) {
|
||||||
|
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCollectionName(final String collectionName) {
|
||||||
|
this.collectionName = collectionName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMongoSessionConverter(final AbstractMongoSessionConverter mongoSessionConverter) {
|
||||||
|
this.mongoSessionConverter = mongoSessionConverter;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,159 +15,19 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.session.data.mongo;
|
package org.springframework.session.data.mongo;
|
||||||
|
|
||||||
import static org.springframework.session.data.mongo.MongoSessionUtils.*;
|
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import org.bson.Document;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
import org.springframework.context.ApplicationEvent;
|
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
|
||||||
import org.springframework.context.ApplicationEventPublisherAware;
|
|
||||||
import org.springframework.data.mongodb.core.MongoOperations;
|
import org.springframework.data.mongodb.core.MongoOperations;
|
||||||
import org.springframework.data.mongodb.core.index.IndexOperations;
|
|
||||||
import org.springframework.lang.Nullable;
|
|
||||||
import org.springframework.session.FindByIndexNameSessionRepository;
|
|
||||||
import org.springframework.session.events.SessionCreatedEvent;
|
|
||||||
import org.springframework.session.events.SessionDeletedEvent;
|
|
||||||
import org.springframework.session.events.SessionExpiredEvent;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Session repository implementation which stores sessions in Mongo. Uses {@link AbstractMongoSessionConverter} to
|
* This {@link org.springframework.session.FindByIndexNameSessionRepository} implementation is kept to support backwards
|
||||||
* transform session objects from/to native Mongo representation ({@code DBObject}). Repository is also responsible for
|
* compatibility.
|
||||||
* removing expired sessions from database. Cleanup is done every minute.
|
|
||||||
*
|
*
|
||||||
* @author Jakub Kubrynski
|
|
||||||
* @author Greg Turnquist
|
|
||||||
* @since 1.2
|
* @since 1.2
|
||||||
|
* @deprecated since 2.2.0 in favor of {@link MongoIndexedSessionRepository}.
|
||||||
*/
|
*/
|
||||||
public class MongoOperationsSessionRepository
|
@Deprecated
|
||||||
implements FindByIndexNameSessionRepository<MongoSession>, ApplicationEventPublisherAware, InitializingBean {
|
public class MongoOperationsSessionRepository extends MongoIndexedSessionRepository {
|
||||||
/**
|
|
||||||
* The default time period in seconds in which a session will expire.
|
|
||||||
*/
|
|
||||||
public static final int DEFAULT_INACTIVE_INTERVAL = 1800;
|
|
||||||
/**
|
|
||||||
* the default collection name for storing session.
|
|
||||||
*/
|
|
||||||
public static final String DEFAULT_COLLECTION_NAME = "sessions";
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(MongoOperationsSessionRepository.class);
|
|
||||||
private final MongoOperations mongoOperations;
|
|
||||||
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
|
|
||||||
private String collectionName = DEFAULT_COLLECTION_NAME;
|
|
||||||
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
|
|
||||||
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
|
|
||||||
private ApplicationEventPublisher eventPublisher;
|
|
||||||
|
|
||||||
public MongoOperationsSessionRepository(MongoOperations mongoOperations) {
|
public MongoOperationsSessionRepository(MongoOperations mongoOperations) {
|
||||||
this.mongoOperations = mongoOperations;
|
super(mongoOperations);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public MongoSession createSession() {
|
|
||||||
MongoSession session = new MongoSession();
|
|
||||||
if (this.maxInactiveIntervalInSeconds != null) {
|
|
||||||
session.setMaxInactiveInterval(Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
|
|
||||||
}
|
|
||||||
publishEvent(new SessionCreatedEvent(this, session));
|
|
||||||
return session;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void save(MongoSession session) {
|
|
||||||
this.mongoOperations.save(Assert.requireNonNull(convertToDBObject(this.mongoSessionConverter, session),
|
|
||||||
"convertToDBObject must not null!"), this.collectionName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Nullable
|
|
||||||
public MongoSession findById(String id) {
|
|
||||||
|
|
||||||
Document sessionWrapper = findSession(id);
|
|
||||||
|
|
||||||
if (sessionWrapper == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
MongoSession session = convertToSession(this.mongoSessionConverter, sessionWrapper);
|
|
||||||
|
|
||||||
if (session != null && session.isExpired()) {
|
|
||||||
publishEvent(new SessionExpiredEvent(this, session));
|
|
||||||
deleteById(id);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return session;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Currently this repository allows only querying against {@code PRINCIPAL_NAME_INDEX_NAME}.
|
|
||||||
*
|
|
||||||
* @param indexName the name if the index (i.e. {@link FindByIndexNameSessionRepository#PRINCIPAL_NAME_INDEX_NAME})
|
|
||||||
* @param indexValue the value of the index to search for.
|
|
||||||
* @return sessions map
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Map<String, MongoSession> findByIndexNameAndIndexValue(String indexName, String indexValue) {
|
|
||||||
|
|
||||||
return Optional.ofNullable(this.mongoSessionConverter.getQueryForIndex(indexName, indexValue))
|
|
||||||
.map(query -> this.mongoOperations.find(query, Document.class, this.collectionName))
|
|
||||||
.orElse(Collections.emptyList()).stream()
|
|
||||||
.map(dbSession -> convertToSession(this.mongoSessionConverter, dbSession))
|
|
||||||
.collect(Collectors.toMap(MongoSession::getId, mapSession -> mapSession));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void deleteById(String id) {
|
|
||||||
|
|
||||||
Optional.ofNullable(findSession(id)).ifPresent(document -> {
|
|
||||||
MongoSession session = convertToSession(this.mongoSessionConverter, document);
|
|
||||||
if (session != null) {
|
|
||||||
publishEvent(new SessionDeletedEvent(this, session));
|
|
||||||
}
|
|
||||||
this.mongoOperations.remove(document, this.collectionName);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterPropertiesSet() {
|
|
||||||
IndexOperations indexOperations = this.mongoOperations.indexOps(this.collectionName);
|
|
||||||
this.mongoSessionConverter.ensureIndexes(indexOperations);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private Document findSession(String id) {
|
|
||||||
return this.mongoOperations.findById(id, Document.class, this.collectionName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
|
|
||||||
this.eventPublisher = eventPublisher;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void publishEvent(ApplicationEvent event) {
|
|
||||||
try {
|
|
||||||
this.eventPublisher.publishEvent(event);
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
logger.error("Error publishing " + event + ".", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) {
|
|
||||||
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCollectionName(final String collectionName) {
|
|
||||||
this.collectionName = collectionName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMongoSessionConverter(final AbstractMongoSessionConverter mongoSessionConverter) {
|
|
||||||
this.mongoSessionConverter = mongoSessionConverter;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ public class MongoSession implements Session {
|
|||||||
private Map<String, Object> attrs = new HashMap<>();
|
private Map<String, Object> attrs = new HashMap<>();
|
||||||
|
|
||||||
public MongoSession() {
|
public MongoSession() {
|
||||||
this(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
this(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MongoSession(long maxInactiveIntervalInSeconds) {
|
public MongoSession(long maxInactiveIntervalInSeconds) {
|
||||||
@@ -147,7 +147,7 @@ public class MongoSession implements Session {
|
|||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(id);
|
return Objects.hash(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
return this.id;
|
return this.id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import java.lang.annotation.Target;
|
|||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add this annotation to a {@code @Configuration} class to expose the SessionRepositoryFilter as a bean named
|
* Add this annotation to a {@code @Configuration} class to expose the SessionRepositoryFilter as a bean named
|
||||||
@@ -59,12 +59,12 @@ public @interface EnableMongoHttpSession {
|
|||||||
*
|
*
|
||||||
* @return default max inactive interval in seconds
|
* @return default max inactive interval in seconds
|
||||||
*/
|
*/
|
||||||
int maxInactiveIntervalInSeconds() default MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL;
|
int maxInactiveIntervalInSeconds() default MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The collection name to use.
|
* The collection name to use.
|
||||||
*
|
*
|
||||||
* @return name of the collection to store session
|
* @return name of the collection to store session
|
||||||
*/
|
*/
|
||||||
String collectionName() default MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME;
|
String collectionName() default MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ import org.springframework.data.mongodb.core.MongoOperations;
|
|||||||
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
|
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
|
||||||
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
|
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
|
||||||
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
|
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
|
||||||
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.util.StringValueResolver;
|
import org.springframework.util.StringValueResolver;
|
||||||
|
|
||||||
@@ -54,9 +54,9 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
|||||||
private ClassLoader classLoader;
|
private ClassLoader classLoader;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public MongoOperationsSessionRepository mongoSessionRepository(MongoOperations mongoOperations) {
|
public MongoIndexedSessionRepository mongoSessionRepository(MongoOperations mongoOperations) {
|
||||||
|
|
||||||
MongoOperationsSessionRepository repository = new MongoOperationsSessionRepository(mongoOperations);
|
MongoIndexedSessionRepository repository = new MongoIndexedSessionRepository(mongoOperations);
|
||||||
repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds);
|
repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds);
|
||||||
|
|
||||||
if (this.mongoSessionConverter != null) {
|
if (this.mongoSessionConverter != null) {
|
||||||
@@ -64,7 +64,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
|||||||
} else {
|
} else {
|
||||||
JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(),
|
JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(),
|
||||||
new DeserializingConverter(this.classLoader),
|
new DeserializingConverter(this.classLoader),
|
||||||
Duration.ofSeconds(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL));
|
Duration.ofSeconds(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL));
|
||||||
repository.setMongoSessionConverter(mongoSessionConverter);
|
repository.setMongoSessionConverter(mongoSessionConverter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +91,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
|||||||
if (attributes != null) {
|
if (attributes != null) {
|
||||||
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
|
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
|
||||||
} else {
|
} else {
|
||||||
this.maxInactiveIntervalInSeconds = MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL;
|
this.maxInactiveIntervalInSeconds = MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
String collectionNameValue = attributes != null ? attributes.getString("collectionName") : "";
|
String collectionNameValue = attributes != null ? attributes.getString("collectionName") : "";
|
||||||
|
|||||||
@@ -17,12 +17,8 @@
|
|||||||
package org.springframework.session.data.mongo;
|
package org.springframework.session.data.mongo;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
import static org.assertj.core.api.Assertions.*;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
|
||||||
import static org.mockito.BDDMockito.*;
|
import static org.mockito.BDDMockito.*;
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -43,25 +39,25 @@ import com.mongodb.BasicDBObject;
|
|||||||
import com.mongodb.DBObject;
|
import com.mongodb.DBObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link MongoOperationsSessionRepository}.
|
* Tests for {@link MongoIndexedSessionRepository}.
|
||||||
*
|
*
|
||||||
* @author Jakub Kubrynski
|
* @author Jakub Kubrynski
|
||||||
* @author Vedran Pavic
|
* @author Vedran Pavic
|
||||||
* @author Greg Turnquist
|
* @author Greg Turnquist
|
||||||
*/
|
*/
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
public class MongoOperationsSessionRepositoryTest {
|
public class MongoIndexedSessionRepositoryTest {
|
||||||
|
|
||||||
@Mock private AbstractMongoSessionConverter converter;
|
@Mock private AbstractMongoSessionConverter converter;
|
||||||
|
|
||||||
@Mock private MongoOperations mongoOperations;
|
@Mock private MongoOperations mongoOperations;
|
||||||
|
|
||||||
private MongoOperationsSessionRepository repository;
|
private MongoIndexedSessionRepository repository;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
|
|
||||||
this.repository = new MongoOperationsSessionRepository(this.mongoOperations);
|
this.repository = new MongoIndexedSessionRepository(this.mongoOperations);
|
||||||
this.repository.setMongoSessionConverter(this.converter);
|
this.repository.setMongoSessionConverter(this.converter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +70,7 @@ public class MongoOperationsSessionRepositoryTest {
|
|||||||
// then
|
// then
|
||||||
assertThat(session.getId()).isNotEmpty();
|
assertThat(session.getId()).isNotEmpty();
|
||||||
assertThat(session.getMaxInactiveInterval().getSeconds())
|
assertThat(session.getMaxInactiveInterval().getSeconds())
|
||||||
.isEqualTo(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
.isEqualTo(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -87,7 +83,7 @@ public class MongoOperationsSessionRepositoryTest {
|
|||||||
// then
|
// then
|
||||||
assertThat(session.getId()).isNotEmpty();
|
assertThat(session.getId()).isNotEmpty();
|
||||||
assertThat(session.getMaxInactiveInterval().getSeconds())
|
assertThat(session.getMaxInactiveInterval().getSeconds())
|
||||||
.isEqualTo(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
.isEqualTo(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -103,7 +99,7 @@ public class MongoOperationsSessionRepositoryTest {
|
|||||||
this.repository.save(session);
|
this.repository.save(session);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(this.mongoOperations).save(dbSession, MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME);
|
verify(this.mongoOperations).save(dbSession, MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -113,8 +109,9 @@ public class MongoOperationsSessionRepositoryTest {
|
|||||||
String sessionId = UUID.randomUUID().toString();
|
String sessionId = UUID.randomUUID().toString();
|
||||||
Document sessionDocument = new Document();
|
Document sessionDocument = new Document();
|
||||||
|
|
||||||
given(this.mongoOperations.findById(sessionId, Document.class,
|
given(
|
||||||
MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(sessionDocument);
|
this.mongoOperations.findById(sessionId, Document.class, MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME))
|
||||||
|
.willReturn(sessionDocument);
|
||||||
|
|
||||||
MongoSession session = new MongoSession();
|
MongoSession session = new MongoSession();
|
||||||
|
|
||||||
@@ -135,8 +132,9 @@ public class MongoOperationsSessionRepositoryTest {
|
|||||||
String sessionId = UUID.randomUUID().toString();
|
String sessionId = UUID.randomUUID().toString();
|
||||||
Document sessionDocument = new Document();
|
Document sessionDocument = new Document();
|
||||||
|
|
||||||
given(this.mongoOperations.findById(sessionId, Document.class,
|
given(
|
||||||
MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(sessionDocument);
|
this.mongoOperations.findById(sessionId, Document.class, MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME))
|
||||||
|
.willReturn(sessionDocument);
|
||||||
|
|
||||||
MongoSession session = mock(MongoSession.class);
|
MongoSession session = mock(MongoSession.class);
|
||||||
|
|
||||||
@@ -149,8 +147,7 @@ public class MongoOperationsSessionRepositoryTest {
|
|||||||
this.repository.findById(sessionId);
|
this.repository.findById(sessionId);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(this.mongoOperations).remove(any(Document.class),
|
verify(this.mongoOperations).remove(any(Document.class), eq(MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME));
|
||||||
eq(MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -162,19 +159,18 @@ public class MongoOperationsSessionRepositoryTest {
|
|||||||
Document sessionDocument = new Document();
|
Document sessionDocument = new Document();
|
||||||
sessionDocument.put("id", sessionId);
|
sessionDocument.put("id", sessionId);
|
||||||
|
|
||||||
MongoSession mongoSession = new MongoSession(sessionId, MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
MongoSession mongoSession = new MongoSession(sessionId, MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||||
|
|
||||||
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
||||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(mongoSession);
|
TypeDescriptor.valueOf(MongoSession.class))).willReturn(mongoSession);
|
||||||
given(this.mongoOperations.findById(eq(sessionId), eq(Document.class),
|
given(this.mongoOperations.findById(eq(sessionId), eq(Document.class),
|
||||||
eq(MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME))).willReturn(sessionDocument);
|
eq(MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME))).willReturn(sessionDocument);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
this.repository.deleteById(sessionId);
|
this.repository.deleteById(sessionId);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(this.mongoOperations).remove(any(Document.class),
|
verify(this.mongoOperations).remove(any(Document.class), eq(MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME));
|
||||||
eq(MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -187,7 +183,7 @@ public class MongoOperationsSessionRepositoryTest {
|
|||||||
|
|
||||||
given(this.converter.getQueryForIndex(anyString(), any(Object.class))).willReturn(mock(Query.class));
|
given(this.converter.getQueryForIndex(anyString(), any(Object.class))).willReturn(mock(Query.class));
|
||||||
given(this.mongoOperations.find(any(Query.class), eq(Document.class),
|
given(this.mongoOperations.find(any(Query.class), eq(Document.class),
|
||||||
eq(MongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME))).willReturn(Collections.singletonList(document));
|
eq(MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME))).willReturn(Collections.singletonList(document));
|
||||||
|
|
||||||
String sessionId = UUID.randomUUID().toString();
|
String sessionId = UUID.randomUUID().toString();
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
package org.springframework.session.data.mongo.config.annotation.web.http;
|
package org.springframework.session.data.mongo.config.annotation.web.http;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
import static org.assertj.core.api.Assertions.*;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
import static org.mockito.BDDMockito.*;
|
import static org.mockito.BDDMockito.*;
|
||||||
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
@@ -33,7 +33,7 @@ import org.springframework.data.mongodb.core.MongoOperations;
|
|||||||
import org.springframework.data.mongodb.core.index.IndexOperations;
|
import org.springframework.data.mongodb.core.index.IndexOperations;
|
||||||
import org.springframework.mock.env.MockEnvironment;
|
import org.springframework.mock.env.MockEnvironment;
|
||||||
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
|
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
|
||||||
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||||
import org.springframework.test.util.ReflectionTestUtils;
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -71,7 +71,7 @@ public class MongoHttpSessionConfigurationTest {
|
|||||||
|
|
||||||
registerAndRefresh(DefaultConfiguration.class);
|
registerAndRefresh(DefaultConfiguration.class);
|
||||||
|
|
||||||
assertThat(this.context.getBean(MongoOperationsSessionRepository.class)).isNotNull();
|
assertThat(this.context.getBean(MongoIndexedSessionRepository.class)).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -79,7 +79,7 @@ public class MongoHttpSessionConfigurationTest {
|
|||||||
|
|
||||||
registerAndRefresh(CustomCollectionNameConfiguration.class);
|
registerAndRefresh(CustomCollectionNameConfiguration.class);
|
||||||
|
|
||||||
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
|
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||||
|
|
||||||
assertThat(repository).isNotNull();
|
assertThat(repository).isNotNull();
|
||||||
assertThat(ReflectionTestUtils.getField(repository, "collectionName")).isEqualTo(COLLECTION_NAME);
|
assertThat(ReflectionTestUtils.getField(repository, "collectionName")).isEqualTo(COLLECTION_NAME);
|
||||||
@@ -101,7 +101,7 @@ public class MongoHttpSessionConfigurationTest {
|
|||||||
|
|
||||||
registerAndRefresh(CustomMaxInactiveIntervalInSecondsConfiguration.class);
|
registerAndRefresh(CustomMaxInactiveIntervalInSecondsConfiguration.class);
|
||||||
|
|
||||||
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
|
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||||
|
|
||||||
assertThat(repository).isNotNull();
|
assertThat(repository).isNotNull();
|
||||||
assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds"))
|
assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds"))
|
||||||
@@ -113,7 +113,7 @@ public class MongoHttpSessionConfigurationTest {
|
|||||||
|
|
||||||
registerAndRefresh(CustomMaxInactiveIntervalInSecondsSetConfiguration.class);
|
registerAndRefresh(CustomMaxInactiveIntervalInSecondsSetConfiguration.class);
|
||||||
|
|
||||||
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
|
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||||
|
|
||||||
assertThat(repository).isNotNull();
|
assertThat(repository).isNotNull();
|
||||||
assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds"))
|
assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds"))
|
||||||
@@ -125,7 +125,7 @@ public class MongoHttpSessionConfigurationTest {
|
|||||||
|
|
||||||
registerAndRefresh(CustomSessionConverterConfiguration.class);
|
registerAndRefresh(CustomSessionConverterConfiguration.class);
|
||||||
|
|
||||||
MongoOperationsSessionRepository repository = this.context.getBean(MongoOperationsSessionRepository.class);
|
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||||
AbstractMongoSessionConverter mongoSessionConverter = this.context.getBean(AbstractMongoSessionConverter.class);
|
AbstractMongoSessionConverter mongoSessionConverter = this.context.getBean(AbstractMongoSessionConverter.class);
|
||||||
|
|
||||||
assertThat(repository).isNotNull();
|
assertThat(repository).isNotNull();
|
||||||
|
|||||||
@@ -38,14 +38,14 @@ import org.springframework.security.core.context.SecurityContext;
|
|||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.session.FindByIndexNameSessionRepository;
|
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||||
import org.springframework.session.Session;
|
import org.springframework.session.Session;
|
||||||
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||||
import org.springframework.session.data.mongo.MongoSession;
|
import org.springframework.session.data.mongo.MongoSession;
|
||||||
import org.springframework.util.SocketUtils;
|
import org.springframework.util.SocketUtils;
|
||||||
|
|
||||||
import com.mongodb.MongoClient;
|
import com.mongodb.MongoClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract base class for {@link org.springframework.session.data.mongo.MongoOperationsSessionRepository} tests.
|
* Abstract base class for {@link MongoIndexedSessionRepository} tests.
|
||||||
*
|
*
|
||||||
* @author Jakub Kubrynski
|
* @author Jakub Kubrynski
|
||||||
* @author Vedran Pavic
|
* @author Vedran Pavic
|
||||||
@@ -57,7 +57,7 @@ abstract public class AbstractMongoRepositoryITest extends AbstractITest {
|
|||||||
|
|
||||||
protected static final String INDEX_NAME = FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME;
|
protected static final String INDEX_NAME = FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME;
|
||||||
|
|
||||||
@Autowired protected MongoOperationsSessionRepository repository;
|
@Autowired protected MongoIndexedSessionRepository repository;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void saves() {
|
public void saves() {
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import org.springframework.session.data.mongo.config.annotation.web.http.EnableM
|
|||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration tests for {@link org.springframework.session.data.mongo.MongoOperationsSessionRepository} that use
|
* Integration tests for {@link org.springframework.session.data.mongo.MongoIndexedSessionRepository} that use
|
||||||
* {@link JacksonMongoSessionConverter} based session serialization.
|
* {@link JacksonMongoSessionConverter} based session serialization.
|
||||||
*
|
*
|
||||||
* @author Jakub Kubrynski
|
* @author Jakub Kubrynski
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ import org.springframework.session.data.mongo.config.annotation.web.http.EnableM
|
|||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration tests for {@link org.springframework.session.data.mongo.MongoOperationsSessionRepository} that use
|
* Integration tests for {@link org.springframework.session.data.mongo.MongoIndexedSessionRepository} that use
|
||||||
* {@link JdkMongoSessionConverter} based session serialization.
|
* {@link JdkMongoSessionConverter} based session serialization.
|
||||||
*
|
*
|
||||||
* @author Jakub Kubrynski
|
* @author Jakub Kubrynski
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
package org.springframework.session.data.mongo.integration;
|
package org.springframework.session.data.mongo.integration;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||||
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession;
|
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession;
|
||||||
import org.springframework.session.data.mongo.integration.AbstractMongoRepositoryITest.BaseConfig;
|
import org.springframework.session.data.mongo.integration.AbstractMongoRepositoryITest.BaseConfig;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
@@ -25,7 +25,7 @@ import org.springframework.test.context.ContextConfiguration;
|
|||||||
* @author Greg Turnquist
|
* @author Greg Turnquist
|
||||||
*/
|
*/
|
||||||
@ContextConfiguration
|
@ContextConfiguration
|
||||||
public class TraditionalConfigurationTest extends AbstractClassLoaderTest<MongoOperationsSessionRepository> {
|
public class TraditionalConfigurationTest extends AbstractClassLoaderTest<MongoIndexedSessionRepository> {
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableMongoHttpSession
|
@EnableMongoHttpSession
|
||||||
|
|||||||
Reference in New Issue
Block a user