Use java.time in all session repositories and configurations
This commit reworks all session repository implementations and their respective configurations to use java.time for managing maxInactiveInterval and other temporal values.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
* Copyright 2014-2022 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.
|
||||
@@ -34,6 +34,7 @@ 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.MapSession;
|
||||
import org.springframework.session.events.SessionCreatedEvent;
|
||||
import org.springframework.session.events.SessionDeletedEvent;
|
||||
import org.springframework.session.events.SessionExpiredEvent;
|
||||
@@ -46,6 +47,7 @@ import org.springframework.session.events.SessionExpiredEvent;
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
* @author Greg Turnquist
|
||||
* @author Vedran Pavic
|
||||
* @since 2.2.0
|
||||
*/
|
||||
public class MongoIndexedSessionRepository
|
||||
@@ -53,8 +55,11 @@ public class MongoIndexedSessionRepository
|
||||
|
||||
/**
|
||||
* The default time period in seconds in which a session will expire.
|
||||
* @deprecated since 3.0.0 in favor of
|
||||
* {@link MapSession#DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS}
|
||||
*/
|
||||
public static final int DEFAULT_INACTIVE_INTERVAL = 1800;
|
||||
@Deprecated
|
||||
public static final int DEFAULT_INACTIVE_INTERVAL = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
|
||||
|
||||
/**
|
||||
* the default collection name for storing session.
|
||||
@@ -65,12 +70,12 @@ public class MongoIndexedSessionRepository
|
||||
|
||||
private final MongoOperations mongoOperations;
|
||||
|
||||
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
|
||||
private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
|
||||
|
||||
private String collectionName = DEFAULT_COLLECTION_NAME;
|
||||
|
||||
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
|
||||
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
|
||||
this.defaultMaxInactiveInterval);
|
||||
|
||||
private ApplicationEventPublisher eventPublisher;
|
||||
|
||||
@@ -83,9 +88,7 @@ public class MongoIndexedSessionRepository
|
||||
|
||||
MongoSession session = new MongoSession();
|
||||
|
||||
if (this.maxInactiveIntervalInSeconds != null) {
|
||||
session.setMaxInactiveInterval(Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
|
||||
}
|
||||
session.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
|
||||
|
||||
publishEvent(new SessionCreatedEvent(this, session));
|
||||
|
||||
@@ -178,8 +181,16 @@ public class MongoIndexedSessionRepository
|
||||
}
|
||||
}
|
||||
|
||||
public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) {
|
||||
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
|
||||
/**
|
||||
* Set the maximum inactive interval in seconds between requests before newly created
|
||||
* sessions will be invalidated. A negative time indicates that the session will never
|
||||
* time out. The default is 30 minutes.
|
||||
* @param defaultMaxInactiveInterval the default maxInactiveInterval
|
||||
*/
|
||||
public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
|
||||
org.springframework.util.Assert.notNull(defaultMaxInactiveInterval,
|
||||
"defaultMaxInactiveInterval must not be null");
|
||||
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
|
||||
}
|
||||
|
||||
public void setCollectionName(final String collectionName) {
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.session.MapSession;
|
||||
import org.springframework.session.Session;
|
||||
|
||||
/**
|
||||
@@ -66,7 +67,7 @@ public class MongoSession implements Session {
|
||||
private Map<String, Object> attrs = new HashMap<>();
|
||||
|
||||
public MongoSession() {
|
||||
this(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
this(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
|
||||
}
|
||||
|
||||
public MongoSession(long maxInactiveIntervalInSeconds) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
* Copyright 2014-2022 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.
|
||||
@@ -32,14 +32,17 @@ import org.springframework.data.mongodb.core.ReactiveMongoOperations;
|
||||
import org.springframework.data.mongodb.core.index.IndexOperations;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.session.MapSession;
|
||||
import org.springframework.session.ReactiveSessionRepository;
|
||||
import org.springframework.session.events.SessionCreatedEvent;
|
||||
import org.springframework.session.events.SessionDeletedEvent;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A {@link ReactiveSessionRepository} implementation that uses Spring Data MongoDB.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
* @author Vedran Pavic
|
||||
* @since 2.2.0
|
||||
*/
|
||||
public class ReactiveMongoSessionRepository
|
||||
@@ -47,8 +50,11 @@ public class ReactiveMongoSessionRepository
|
||||
|
||||
/**
|
||||
* The default time period in seconds in which a session will expire.
|
||||
* @deprecated since 3.0.0 in favor of
|
||||
* {@link MapSession#DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS}
|
||||
*/
|
||||
public static final int DEFAULT_INACTIVE_INTERVAL = 1800;
|
||||
@Deprecated
|
||||
public static final int DEFAULT_INACTIVE_INTERVAL = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
|
||||
|
||||
/**
|
||||
* The default collection name for storing session.
|
||||
@@ -59,12 +65,12 @@ public class ReactiveMongoSessionRepository
|
||||
|
||||
private final ReactiveMongoOperations mongoOperations;
|
||||
|
||||
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
|
||||
private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
|
||||
|
||||
private String collectionName = DEFAULT_COLLECTION_NAME;
|
||||
|
||||
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
|
||||
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
|
||||
this.defaultMaxInactiveInterval);
|
||||
|
||||
private MongoOperations blockingMongoOperations;
|
||||
|
||||
@@ -88,7 +94,7 @@ public class ReactiveMongoSessionRepository
|
||||
@Override
|
||||
public Mono<MongoSession> createSession() {
|
||||
|
||||
return Mono.justOrEmpty(this.maxInactiveIntervalInSeconds) //
|
||||
return Mono.justOrEmpty(this.defaultMaxInactiveInterval.toSeconds()) //
|
||||
.map(MongoSession::new) //
|
||||
.doOnNext((mongoSession) -> publishEvent(new SessionCreatedEvent(this, mongoSession))) //
|
||||
.switchIfEmpty(Mono.just(new MongoSession()));
|
||||
@@ -170,12 +176,15 @@ public class ReactiveMongoSessionRepository
|
||||
}
|
||||
}
|
||||
|
||||
public Integer getMaxInactiveIntervalInSeconds() {
|
||||
return this.maxInactiveIntervalInSeconds;
|
||||
}
|
||||
|
||||
public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) {
|
||||
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
|
||||
/**
|
||||
* Set the maximum inactive interval in seconds between requests before newly created
|
||||
* sessions will be invalidated. A negative time indicates that the session will never
|
||||
* time out. The default is 30 minutes.
|
||||
* @param defaultMaxInactiveInterval the default maxInactiveInterval
|
||||
*/
|
||||
public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
|
||||
Assert.notNull(defaultMaxInactiveInterval, "defaultMaxInactiveInterval must not be null");
|
||||
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
|
||||
}
|
||||
|
||||
public String getCollectionName() {
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.session.MapSession;
|
||||
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||
|
||||
/**
|
||||
@@ -58,7 +59,7 @@ public @interface EnableMongoHttpSession {
|
||||
* The maximum time a session will be kept if it is inactive.
|
||||
* @return default max inactive interval in seconds
|
||||
*/
|
||||
int maxInactiveIntervalInSeconds() default MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL;
|
||||
int maxInactiveIntervalInSeconds() default MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
|
||||
|
||||
/**
|
||||
* The collection name to use.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2022 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.
|
||||
@@ -33,6 +33,7 @@ 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.MapSession;
|
||||
import org.springframework.session.config.SessionRepositoryCustomizer;
|
||||
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
|
||||
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
|
||||
@@ -56,7 +57,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
||||
|
||||
private AbstractMongoSessionConverter mongoSessionConverter;
|
||||
|
||||
private Integer maxInactiveIntervalInSeconds;
|
||||
private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL;
|
||||
|
||||
private String collectionName;
|
||||
|
||||
@@ -72,7 +73,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
||||
public MongoIndexedSessionRepository mongoSessionRepository(MongoOperations mongoOperations) {
|
||||
|
||||
MongoIndexedSessionRepository repository = new MongoIndexedSessionRepository(mongoOperations);
|
||||
repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds);
|
||||
repository.setDefaultMaxInactiveInterval(this.maxInactiveInterval);
|
||||
|
||||
if (this.mongoSessionConverter != null) {
|
||||
repository.setMongoSessionConverter(this.mongoSessionConverter);
|
||||
@@ -84,7 +85,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
||||
else {
|
||||
JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(),
|
||||
new DeserializingConverter(this.classLoader),
|
||||
Duration.ofSeconds(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL));
|
||||
Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS));
|
||||
|
||||
if (this.indexResolver != null) {
|
||||
mongoSessionConverter.setIndexResolver(this.indexResolver);
|
||||
@@ -107,8 +108,13 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
||||
this.collectionName = collectionName;
|
||||
}
|
||||
|
||||
public void setMaxInactiveInterval(Duration maxInactiveInterval) {
|
||||
this.maxInactiveInterval = maxInactiveInterval;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) {
|
||||
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
|
||||
setMaxInactiveInterval(Duration.ofSeconds(maxInactiveIntervalInSeconds));
|
||||
}
|
||||
|
||||
public void setImportMetadata(AnnotationMetadata importMetadata) {
|
||||
@@ -117,10 +123,8 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
|
||||
.fromMap(importMetadata.getAnnotationAttributes(EnableMongoHttpSession.class.getName()));
|
||||
|
||||
if (attributes != null) {
|
||||
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
|
||||
}
|
||||
else {
|
||||
this.maxInactiveIntervalInSeconds = MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL;
|
||||
this.maxInactiveInterval = Duration
|
||||
.ofSeconds(attributes.<Integer>getNumber("maxInactiveIntervalInSeconds"));
|
||||
}
|
||||
|
||||
String collectionNameValue = (attributes != null) ? attributes.getString("collectionName") : "";
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.session.MapSession;
|
||||
import org.springframework.session.data.mongo.ReactiveMongoSessionRepository;
|
||||
|
||||
/**
|
||||
@@ -44,7 +45,7 @@ import org.springframework.session.data.mongo.ReactiveMongoSessionRepository;
|
||||
* </code> </pre>
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
* @author Vedran Pavić
|
||||
* @author Vedran Pavic
|
||||
* @since 2.0
|
||||
*/
|
||||
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
|
||||
@@ -57,7 +58,7 @@ public @interface EnableMongoWebSession {
|
||||
* The maximum time a session will be kept if it is inactive.
|
||||
* @return default max inactive interval in seconds
|
||||
*/
|
||||
int maxInactiveIntervalInSeconds() default ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL;
|
||||
int maxInactiveIntervalInSeconds() default MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
|
||||
|
||||
/**
|
||||
* The collection name to use.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2016-2022 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.
|
||||
@@ -34,6 +34,7 @@ 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.MapSession;
|
||||
import org.springframework.session.config.ReactiveSessionRepositoryCustomizer;
|
||||
import org.springframework.session.config.annotation.web.server.SpringWebSessionConfiguration;
|
||||
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
|
||||
@@ -56,7 +57,7 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
|
||||
|
||||
private AbstractMongoSessionConverter mongoSessionConverter;
|
||||
|
||||
private Integer maxInactiveIntervalInSeconds;
|
||||
private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL;
|
||||
|
||||
private String collectionName;
|
||||
|
||||
@@ -87,7 +88,7 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
|
||||
else {
|
||||
JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(),
|
||||
new DeserializingConverter(this.classLoader),
|
||||
Duration.ofSeconds(ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL));
|
||||
Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS));
|
||||
|
||||
if (this.indexResolver != null) {
|
||||
mongoSessionConverter.setIndexResolver(this.indexResolver);
|
||||
@@ -96,9 +97,7 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
|
||||
repository.setMongoSessionConverter(mongoSessionConverter);
|
||||
}
|
||||
|
||||
if (this.maxInactiveIntervalInSeconds != null) {
|
||||
repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds);
|
||||
}
|
||||
repository.setDefaultMaxInactiveInterval(this.maxInactiveInterval);
|
||||
|
||||
if (this.collectionName != null) {
|
||||
repository.setCollectionName(this.collectionName);
|
||||
@@ -126,10 +125,8 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
|
||||
.fromMap(importMetadata.getAnnotationAttributes(EnableMongoWebSession.class.getName()));
|
||||
|
||||
if (attributes != null) {
|
||||
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
|
||||
}
|
||||
else {
|
||||
this.maxInactiveIntervalInSeconds = ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL;
|
||||
this.maxInactiveInterval = Duration
|
||||
.ofSeconds(attributes.<Integer>getNumber("maxInactiveIntervalInSeconds"));
|
||||
}
|
||||
|
||||
String collectionNameValue = (attributes != null) ? attributes.getString("collectionName") : "";
|
||||
@@ -149,12 +146,17 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
|
||||
this.embeddedValueResolver = embeddedValueResolver;
|
||||
}
|
||||
|
||||
public Integer getMaxInactiveIntervalInSeconds() {
|
||||
return this.maxInactiveIntervalInSeconds;
|
||||
public Duration getMaxInactiveInterval() {
|
||||
return this.maxInactiveInterval;
|
||||
}
|
||||
|
||||
public void setMaxInactiveInterval(Duration maxInactiveInterval) {
|
||||
this.maxInactiveInterval = maxInactiveInterval;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) {
|
||||
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
|
||||
setMaxInactiveInterval(Duration.ofSeconds(maxInactiveIntervalInSeconds));
|
||||
}
|
||||
|
||||
public String getCollectionName() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
* Copyright 2014-2022 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.
|
||||
@@ -33,6 +33,7 @@ import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||
import org.springframework.session.MapSession;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -76,20 +77,7 @@ public class MongoIndexedSessionRepositoryTest {
|
||||
// then
|
||||
assertThat(session.getId()).isNotEmpty();
|
||||
assertThat(session.getMaxInactiveInterval().getSeconds())
|
||||
.isEqualTo(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCreateSessionWhenMaxInactiveIntervalNotDefined() {
|
||||
|
||||
// when
|
||||
this.repository.setMaxInactiveIntervalInSeconds(null);
|
||||
MongoSession session = this.repository.createSession();
|
||||
|
||||
// then
|
||||
assertThat(session.getId()).isNotEmpty();
|
||||
assertThat(session.getMaxInactiveInterval().getSeconds())
|
||||
.isEqualTo(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
.isEqualTo(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -164,8 +152,7 @@ public class MongoIndexedSessionRepositoryTest {
|
||||
Document sessionDocument = new Document();
|
||||
sessionDocument.put("id", sessionId);
|
||||
|
||||
MongoSession mongoSession = new MongoSession(sessionId,
|
||||
MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
MongoSession mongoSession = new MongoSession(sessionId, MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
|
||||
|
||||
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(mongoSession);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
* Copyright 2014-2022 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.
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.session.data.mongo;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
@@ -35,6 +36,7 @@ import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
|
||||
import org.springframework.data.mongodb.core.index.IndexOperations;
|
||||
import org.springframework.session.MapSession;
|
||||
import org.springframework.session.events.SessionDeletedEvent;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -84,26 +86,8 @@ public class ReactiveMongoSessionRepositoryTest {
|
||||
.as(StepVerifier::create) //
|
||||
.expectNextMatches((mongoSession) -> {
|
||||
assertThat(mongoSession.getId()).isNotEmpty();
|
||||
assertThat(mongoSession.getMaxInactiveInterval().getSeconds())
|
||||
.isEqualTo(ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
return true;
|
||||
}) //
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCreateSessionWhenMaxInactiveIntervalNotDefined() {
|
||||
|
||||
// when
|
||||
this.repository.setMaxInactiveIntervalInSeconds(null);
|
||||
|
||||
// then
|
||||
this.repository.createSession() //
|
||||
.as(StepVerifier::create) //
|
||||
.expectNextMatches((mongoSession) -> {
|
||||
assertThat(mongoSession.getId()).isNotEmpty();
|
||||
assertThat(mongoSession.getMaxInactiveInterval().getSeconds())
|
||||
.isEqualTo(ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL);
|
||||
assertThat(mongoSession.getMaxInactiveInterval())
|
||||
.isEqualTo(Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS));
|
||||
return true;
|
||||
}) //
|
||||
.verifyComplete();
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.session.data.mongo.config.annotation.web.http;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Order;
|
||||
@@ -112,9 +113,8 @@ public class MongoHttpSessionConfigurationTest {
|
||||
|
||||
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds"))
|
||||
.isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS);
|
||||
assertThat(repository).extracting("defaultMaxInactiveInterval")
|
||||
.isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -124,9 +124,8 @@ public class MongoHttpSessionConfigurationTest {
|
||||
|
||||
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds"))
|
||||
.isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS);
|
||||
assertThat(repository).extracting("defaultMaxInactiveInterval")
|
||||
.isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -161,7 +160,7 @@ public class MongoHttpSessionConfigurationTest {
|
||||
|
||||
MongoIndexedSessionRepository sessionRepository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||
|
||||
assertThat(sessionRepository).hasFieldOrPropertyWithValue("maxInactiveIntervalInSeconds", 10000);
|
||||
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ofSeconds(10000));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -198,7 +197,7 @@ public class MongoHttpSessionConfigurationTest {
|
||||
void importConfigAndCustomize() {
|
||||
registerAndRefresh(ImportConfigAndCustomizeConfiguration.class);
|
||||
MongoIndexedSessionRepository sessionRepository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||
assertThat(sessionRepository).extracting("maxInactiveIntervalInSeconds").isEqualTo(0);
|
||||
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ZERO);
|
||||
}
|
||||
|
||||
private void registerAndRefresh(Class<?>... annotatedClasses) {
|
||||
@@ -266,7 +265,7 @@ public class MongoHttpSessionConfigurationTest {
|
||||
static class CustomMaxInactiveIntervalInSecondsSetConfiguration extends MongoHttpSessionConfiguration {
|
||||
|
||||
CustomMaxInactiveIntervalInSecondsSetConfiguration() {
|
||||
setMaxInactiveIntervalInSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS);
|
||||
setMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -300,13 +299,13 @@ public class MongoHttpSessionConfigurationTest {
|
||||
@Bean
|
||||
@Order(0)
|
||||
SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizerOne() {
|
||||
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0);
|
||||
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(1)
|
||||
SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizerTwo() {
|
||||
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(10000);
|
||||
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ofSeconds(10000));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -346,7 +345,7 @@ public class MongoHttpSessionConfigurationTest {
|
||||
|
||||
@Bean
|
||||
SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizer() {
|
||||
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0);
|
||||
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.session.data.mongo.config.annotation.web.reactive;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
@@ -56,7 +57,7 @@ import static org.mockito.BDDMockito.verify;
|
||||
* Verify various configurations through {@link EnableSpringWebSession}.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
* @author Vedran Pavić
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
public class ReactiveMongoWebSessionConfigurationTest {
|
||||
|
||||
@@ -128,7 +129,7 @@ public class ReactiveMongoWebSessionConfigurationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void overridingIntervalAndCollectionNameThroughAnnotationShouldWork() throws IllegalAccessException {
|
||||
void overridingIntervalAndCollectionNameThroughAnnotationShouldWork() {
|
||||
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(OverrideMongoParametersConfig.class);
|
||||
@@ -136,17 +137,8 @@ public class ReactiveMongoWebSessionConfigurationTest {
|
||||
|
||||
ReactiveMongoSessionRepository repository = this.context.getBean(ReactiveMongoSessionRepository.class);
|
||||
|
||||
Field inactiveField = ReflectionUtils.findField(ReactiveMongoSessionRepository.class,
|
||||
"maxInactiveIntervalInSeconds");
|
||||
ReflectionUtils.makeAccessible(inactiveField);
|
||||
Integer inactiveSeconds = (Integer) inactiveField.get(repository);
|
||||
|
||||
Field collectionNameField = ReflectionUtils.findField(ReactiveMongoSessionRepository.class, "collectionName");
|
||||
ReflectionUtils.makeAccessible(collectionNameField);
|
||||
String collectionName = (String) collectionNameField.get(repository);
|
||||
|
||||
assertThat(inactiveSeconds).isEqualTo(123);
|
||||
assertThat(collectionName).isEqualTo("test-case");
|
||||
assertThat(repository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ofSeconds(123));
|
||||
assertThat(repository).extracting("collectionName").isEqualTo("test-case");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -174,7 +166,7 @@ public class ReactiveMongoWebSessionConfigurationTest {
|
||||
ReactiveMongoSessionRepository repository = this.context.getBean(ReactiveMongoSessionRepository.class);
|
||||
|
||||
assertThat(repository.getCollectionName()).isEqualTo("custom-collection");
|
||||
assertThat(repository.getMaxInactiveIntervalInSeconds()).isEqualTo(123);
|
||||
assertThat(repository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ofSeconds(123));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -186,7 +178,7 @@ public class ReactiveMongoWebSessionConfigurationTest {
|
||||
|
||||
ReactiveMongoSessionRepository repository = this.context.getBean(ReactiveMongoSessionRepository.class);
|
||||
|
||||
assertThat(repository).hasFieldOrPropertyWithValue("maxInactiveIntervalInSeconds", 10000);
|
||||
assertThat(repository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ofSeconds(10000));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -227,7 +219,7 @@ public class ReactiveMongoWebSessionConfigurationTest {
|
||||
this.context.register(ImportConfigAndCustomizeConfiguration.class);
|
||||
this.context.refresh();
|
||||
ReactiveMongoSessionRepository sessionRepository = this.context.getBean(ReactiveMongoSessionRepository.class);
|
||||
assertThat(sessionRepository).extracting("maxInactiveIntervalInSeconds").isEqualTo(0);
|
||||
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ZERO);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -331,7 +323,7 @@ public class ReactiveMongoWebSessionConfigurationTest {
|
||||
CustomizedReactiveConfiguration() {
|
||||
|
||||
this.setCollectionName("custom-collection");
|
||||
this.setMaxInactiveIntervalInSeconds(123);
|
||||
this.setMaxInactiveInterval(Duration.ofSeconds(123));
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -353,13 +345,13 @@ public class ReactiveMongoWebSessionConfigurationTest {
|
||||
@Bean
|
||||
@Order(0)
|
||||
ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> sessionRepositoryCustomizerOne() {
|
||||
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0);
|
||||
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(1)
|
||||
ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> sessionRepositoryCustomizerTwo() {
|
||||
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(10000);
|
||||
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ofSeconds(10000));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -414,7 +406,7 @@ public class ReactiveMongoWebSessionConfigurationTest {
|
||||
|
||||
@Bean
|
||||
ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> sessionRepositoryCustomizer() {
|
||||
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0);
|
||||
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user