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:
Vedran Pavic
2022-09-26 10:19:37 +02:00
committed by Rob Winch
parent da8e5fbbac
commit 6d74cf5f35
37 changed files with 281 additions and 285 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2019 the original author or authors. * Copyright 2014-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -49,10 +49,16 @@ import java.util.UUID;
public final class MapSession implements Session, Serializable { public final class MapSession implements Session, Serializable {
/** /**
* Default {@link #setMaxInactiveInterval(Duration)} (30 minutes). * Default {@link #setMaxInactiveInterval(Duration)} (30 minutes) in seconds.
*/ */
public static final int DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS = 1800; public static final int DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS = 1800;
/**
* Default {@link #setMaxInactiveInterval(Duration)} (30 minutes).
*/
public static final Duration DEFAULT_MAX_INACTIVE_INTERVAL = Duration
.ofSeconds(DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
private String id; private String id;
private final String originalId; private final String originalId;
@@ -66,7 +72,7 @@ public final class MapSession implements Session, Serializable {
/** /**
* Defaults to 30 minutes. * Defaults to 30 minutes.
*/ */
private Duration maxInactiveInterval = Duration.ofSeconds(DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS); private Duration maxInactiveInterval = DEFAULT_MAX_INACTIVE_INTERVAL;
/** /**
* Creates a new instance with a secure randomly generated identifier. * Creates a new instance with a secure randomly generated identifier.

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2019 the original author or authors. * Copyright 2014-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.Map;
import org.springframework.session.events.SessionDeletedEvent; import org.springframework.session.events.SessionDeletedEvent;
import org.springframework.session.events.SessionExpiredEvent; import org.springframework.session.events.SessionExpiredEvent;
import org.springframework.util.Assert;
/** /**
* A {@link SessionRepository} backed by a {@link java.util.Map} and that uses a * A {@link SessionRepository} backed by a {@link java.util.Map} and that uses a
@@ -38,11 +39,7 @@ import org.springframework.session.events.SessionExpiredEvent;
*/ */
public class MapSessionRepository implements SessionRepository<MapSession> { public class MapSessionRepository implements SessionRepository<MapSession> {
/** private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
* If non-null, this value is used to override
* {@link Session#setMaxInactiveInterval(Duration)}.
*/
private Integer defaultMaxInactiveInterval;
private final Map<String, Session> sessions; private final Map<String, Session> sessions;
@@ -59,12 +56,13 @@ public class MapSessionRepository implements SessionRepository<MapSession> {
} }
/** /**
* If non-null, this value is used to override * Set the maximum inactive interval in seconds between requests before newly created
* {@link Session#setMaxInactiveInterval(Duration)}. * sessions will be invalidated. A negative time indicates that the session will never
* @param defaultMaxInactiveInterval the number of seconds that the {@link Session} * time out. The default is 30 minutes.
* should be kept alive between client requests. * @param defaultMaxInactiveInterval the default maxInactiveInterval
*/ */
public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) { public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
Assert.notNull(defaultMaxInactiveInterval, "defaultMaxInactiveInterval must not be null");
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval; this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
} }
@@ -97,9 +95,7 @@ public class MapSessionRepository implements SessionRepository<MapSession> {
@Override @Override
public MapSession createSession() { public MapSession createSession() {
MapSession result = new MapSession(); MapSession result = new MapSession();
if (this.defaultMaxInactiveInterval != null) { result.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
result.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
return result; return result;
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2019 the original author or authors. * Copyright 2014-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import reactor.core.publisher.Mono;
import org.springframework.session.events.SessionDeletedEvent; import org.springframework.session.events.SessionDeletedEvent;
import org.springframework.session.events.SessionExpiredEvent; import org.springframework.session.events.SessionExpiredEvent;
import org.springframework.util.Assert;
/** /**
* A {@link ReactiveSessionRepository} backed by a {@link Map} and that uses a * A {@link ReactiveSessionRepository} backed by a {@link Map} and that uses a
@@ -40,11 +41,7 @@ import org.springframework.session.events.SessionExpiredEvent;
*/ */
public class ReactiveMapSessionRepository implements ReactiveSessionRepository<MapSession> { public class ReactiveMapSessionRepository implements ReactiveSessionRepository<MapSession> {
/** private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
* If non-null, this value is used to override
* {@link Session#setMaxInactiveInterval(Duration)}.
*/
private Integer defaultMaxInactiveInterval;
private final Map<String, Session> sessions; private final Map<String, Session> sessions;
@@ -61,12 +58,13 @@ public class ReactiveMapSessionRepository implements ReactiveSessionRepository<M
} }
/** /**
* If non-null, this value is used to override * Set the maximum inactive interval in seconds between requests before newly created
* {@link Session#setMaxInactiveInterval(Duration)}. * sessions will be invalidated. A negative time indicates that the session will never
* @param defaultMaxInactiveInterval the number of seconds that the {@link Session} * time out. The default is 30 minutes.
* should be kept alive between client requests. * @param defaultMaxInactiveInterval the default maxInactiveInterval
*/ */
public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) { public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
Assert.notNull(defaultMaxInactiveInterval, "defaultMaxInactiveInterval must not be null");
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval; this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
} }
@@ -99,9 +97,7 @@ public class ReactiveMapSessionRepository implements ReactiveSessionRepository<M
public Mono<MapSession> createSession() { public Mono<MapSession> createSession() {
return Mono.defer(() -> { return Mono.defer(() -> {
MapSession result = new MapSession(); MapSession result = new MapSession();
if (this.defaultMaxInactiveInterval != null) { result.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
result.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
return Mono.just(result); return Mono.just(result);
}); });
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2019 the original author or authors. * Copyright 2014-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -60,8 +60,8 @@ class MapSessionRepositoryTests {
@Test @Test
void createSessionCustomDefaultExpiration() { void createSessionCustomDefaultExpiration() {
final Duration expectedMaxInterval = new MapSession().getMaxInactiveInterval().plusSeconds(10); Duration expectedMaxInterval = new MapSession().getMaxInactiveInterval().plusSeconds(10);
this.repository.setDefaultMaxInactiveInterval((int) expectedMaxInterval.getSeconds()); this.repository.setDefaultMaxInactiveInterval(expectedMaxInterval);
Session session = this.repository.createSession(); Session session = this.repository.createSession();

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2019 the original author or authors. * Copyright 2014-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -103,8 +103,8 @@ class ReactiveMapSessionRepositoryTests {
@Test @Test
void createSessionWhenCustomMaxInactiveIntervalThenCustomMaxInactiveInterval() { void createSessionWhenCustomMaxInactiveIntervalThenCustomMaxInactiveInterval() {
final Duration expectedMaxInterval = new MapSession().getMaxInactiveInterval().plusSeconds(10); Duration expectedMaxInterval = new MapSession().getMaxInactiveInterval().plusSeconds(10);
this.repository.setDefaultMaxInactiveInterval((int) expectedMaxInterval.getSeconds()); this.repository.setDefaultMaxInactiveInterval(expectedMaxInterval);
Session session = this.repository.createSession().block(); Session session = this.repository.createSession().block();

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.data.mongodb.core.index.IndexOperations;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.session.FindByIndexNameSessionRepository; import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.MapSession;
import org.springframework.session.events.SessionCreatedEvent; import org.springframework.session.events.SessionCreatedEvent;
import org.springframework.session.events.SessionDeletedEvent; import org.springframework.session.events.SessionDeletedEvent;
import org.springframework.session.events.SessionExpiredEvent; import org.springframework.session.events.SessionExpiredEvent;
@@ -46,6 +47,7 @@ import org.springframework.session.events.SessionExpiredEvent;
* *
* @author Jakub Kubrynski * @author Jakub Kubrynski
* @author Greg Turnquist * @author Greg Turnquist
* @author Vedran Pavic
* @since 2.2.0 * @since 2.2.0
*/ */
public class MongoIndexedSessionRepository public class MongoIndexedSessionRepository
@@ -53,8 +55,11 @@ public class MongoIndexedSessionRepository
/** /**
* The default time period in seconds in which a session will expire. * 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. * the default collection name for storing session.
@@ -65,12 +70,12 @@ public class MongoIndexedSessionRepository
private final MongoOperations mongoOperations; 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 String collectionName = DEFAULT_COLLECTION_NAME;
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter( private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
Duration.ofSeconds(this.maxInactiveIntervalInSeconds)); this.defaultMaxInactiveInterval);
private ApplicationEventPublisher eventPublisher; private ApplicationEventPublisher eventPublisher;
@@ -83,9 +88,7 @@ public class MongoIndexedSessionRepository
MongoSession session = new MongoSession(); MongoSession session = new MongoSession();
if (this.maxInactiveIntervalInSeconds != null) { session.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
session.setMaxInactiveInterval(Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
}
publishEvent(new SessionCreatedEvent(this, session)); 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) { public void setCollectionName(final String collectionName) {

View File

@@ -27,6 +27,7 @@ import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.session.MapSession;
import org.springframework.session.Session; import org.springframework.session.Session;
/** /**
@@ -66,7 +67,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(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL); this(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
} }
public MongoSession(long maxInactiveIntervalInSeconds) { public MongoSession(long maxInactiveIntervalInSeconds) {

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.index.IndexOperations;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.session.MapSession;
import org.springframework.session.ReactiveSessionRepository; import org.springframework.session.ReactiveSessionRepository;
import org.springframework.session.events.SessionCreatedEvent; import org.springframework.session.events.SessionCreatedEvent;
import org.springframework.session.events.SessionDeletedEvent; import org.springframework.session.events.SessionDeletedEvent;
import org.springframework.util.Assert;
/** /**
* A {@link ReactiveSessionRepository} implementation that uses Spring Data MongoDB. * A {@link ReactiveSessionRepository} implementation that uses Spring Data MongoDB.
* *
* @author Greg Turnquist * @author Greg Turnquist
* @author Vedran Pavic
* @since 2.2.0 * @since 2.2.0
*/ */
public class ReactiveMongoSessionRepository public class ReactiveMongoSessionRepository
@@ -47,8 +50,11 @@ public class ReactiveMongoSessionRepository
/** /**
* The default time period in seconds in which a session will expire. * 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. * The default collection name for storing session.
@@ -59,12 +65,12 @@ public class ReactiveMongoSessionRepository
private final ReactiveMongoOperations mongoOperations; 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 String collectionName = DEFAULT_COLLECTION_NAME;
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter( private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
Duration.ofSeconds(this.maxInactiveIntervalInSeconds)); this.defaultMaxInactiveInterval);
private MongoOperations blockingMongoOperations; private MongoOperations blockingMongoOperations;
@@ -88,7 +94,7 @@ public class ReactiveMongoSessionRepository
@Override @Override
public Mono<MongoSession> createSession() { public Mono<MongoSession> createSession() {
return Mono.justOrEmpty(this.maxInactiveIntervalInSeconds) // return Mono.justOrEmpty(this.defaultMaxInactiveInterval.toSeconds()) //
.map(MongoSession::new) // .map(MongoSession::new) //
.doOnNext((mongoSession) -> publishEvent(new SessionCreatedEvent(this, mongoSession))) // .doOnNext((mongoSession) -> publishEvent(new SessionCreatedEvent(this, mongoSession))) //
.switchIfEmpty(Mono.just(new MongoSession())); .switchIfEmpty(Mono.just(new MongoSession()));
@@ -170,12 +176,15 @@ public class ReactiveMongoSessionRepository
} }
} }
public Integer getMaxInactiveIntervalInSeconds() { /**
return this.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.
public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) { * @param defaultMaxInactiveInterval the default maxInactiveInterval
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; */
public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
Assert.notNull(defaultMaxInactiveInterval, "defaultMaxInactiveInterval must not be null");
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
} }
public String getCollectionName() { public String getCollectionName() {

View File

@@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.session.MapSession;
import org.springframework.session.data.mongo.MongoIndexedSessionRepository; 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. * The maximum time a session will be kept if it is inactive.
* @return default max inactive interval in seconds * @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. * The collection name to use.

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.core.type.AnnotationMetadata;
import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.session.IndexResolver; import org.springframework.session.IndexResolver;
import org.springframework.session.MapSession;
import org.springframework.session.config.SessionRepositoryCustomizer; import org.springframework.session.config.SessionRepositoryCustomizer;
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;
@@ -56,7 +57,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
private AbstractMongoSessionConverter mongoSessionConverter; private AbstractMongoSessionConverter mongoSessionConverter;
private Integer maxInactiveIntervalInSeconds; private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL;
private String collectionName; private String collectionName;
@@ -72,7 +73,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
public MongoIndexedSessionRepository mongoSessionRepository(MongoOperations mongoOperations) { public MongoIndexedSessionRepository mongoSessionRepository(MongoOperations mongoOperations) {
MongoIndexedSessionRepository repository = new MongoIndexedSessionRepository(mongoOperations); MongoIndexedSessionRepository repository = new MongoIndexedSessionRepository(mongoOperations);
repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds); repository.setDefaultMaxInactiveInterval(this.maxInactiveInterval);
if (this.mongoSessionConverter != null) { if (this.mongoSessionConverter != null) {
repository.setMongoSessionConverter(this.mongoSessionConverter); repository.setMongoSessionConverter(this.mongoSessionConverter);
@@ -84,7 +85,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(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL)); Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS));
if (this.indexResolver != null) { if (this.indexResolver != null) {
mongoSessionConverter.setIndexResolver(this.indexResolver); mongoSessionConverter.setIndexResolver(this.indexResolver);
@@ -107,8 +108,13 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
this.collectionName = collectionName; this.collectionName = collectionName;
} }
public void setMaxInactiveInterval(Duration maxInactiveInterval) {
this.maxInactiveInterval = maxInactiveInterval;
}
@Deprecated
public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) { public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) {
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; setMaxInactiveInterval(Duration.ofSeconds(maxInactiveIntervalInSeconds));
} }
public void setImportMetadata(AnnotationMetadata importMetadata) { public void setImportMetadata(AnnotationMetadata importMetadata) {
@@ -117,10 +123,8 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio
.fromMap(importMetadata.getAnnotationAttributes(EnableMongoHttpSession.class.getName())); .fromMap(importMetadata.getAnnotationAttributes(EnableMongoHttpSession.class.getName()));
if (attributes != null) { if (attributes != null) {
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds"); this.maxInactiveInterval = Duration
} .ofSeconds(attributes.<Integer>getNumber("maxInactiveIntervalInSeconds"));
else {
this.maxInactiveIntervalInSeconds = MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL;
} }
String collectionNameValue = (attributes != null) ? attributes.getString("collectionName") : ""; String collectionNameValue = (attributes != null) ? attributes.getString("collectionName") : "";

View File

@@ -21,6 +21,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.session.MapSession;
import org.springframework.session.data.mongo.ReactiveMongoSessionRepository; import org.springframework.session.data.mongo.ReactiveMongoSessionRepository;
/** /**
@@ -44,7 +45,7 @@ import org.springframework.session.data.mongo.ReactiveMongoSessionRepository;
* </code> </pre> * </code> </pre>
* *
* @author Greg Turnquist * @author Greg Turnquist
* @author Vedran Pavić * @author Vedran Pavic
* @since 2.0 * @since 2.0
*/ */
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME) @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. * The maximum time a session will be kept if it is inactive.
* @return default max inactive interval in seconds * @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. * The collection name to use.

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.MongoOperations;
import org.springframework.data.mongodb.core.ReactiveMongoOperations; import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.session.IndexResolver; import org.springframework.session.IndexResolver;
import org.springframework.session.MapSession;
import org.springframework.session.config.ReactiveSessionRepositoryCustomizer; import org.springframework.session.config.ReactiveSessionRepositoryCustomizer;
import org.springframework.session.config.annotation.web.server.SpringWebSessionConfiguration; import org.springframework.session.config.annotation.web.server.SpringWebSessionConfiguration;
import org.springframework.session.data.mongo.AbstractMongoSessionConverter; import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
@@ -56,7 +57,7 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
private AbstractMongoSessionConverter mongoSessionConverter; private AbstractMongoSessionConverter mongoSessionConverter;
private Integer maxInactiveIntervalInSeconds; private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL;
private String collectionName; private String collectionName;
@@ -87,7 +88,7 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
else { else {
JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(), JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(),
new DeserializingConverter(this.classLoader), new DeserializingConverter(this.classLoader),
Duration.ofSeconds(ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL)); Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS));
if (this.indexResolver != null) { if (this.indexResolver != null) {
mongoSessionConverter.setIndexResolver(this.indexResolver); mongoSessionConverter.setIndexResolver(this.indexResolver);
@@ -96,9 +97,7 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
repository.setMongoSessionConverter(mongoSessionConverter); repository.setMongoSessionConverter(mongoSessionConverter);
} }
if (this.maxInactiveIntervalInSeconds != null) { repository.setDefaultMaxInactiveInterval(this.maxInactiveInterval);
repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds);
}
if (this.collectionName != null) { if (this.collectionName != null) {
repository.setCollectionName(this.collectionName); repository.setCollectionName(this.collectionName);
@@ -126,10 +125,8 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
.fromMap(importMetadata.getAnnotationAttributes(EnableMongoWebSession.class.getName())); .fromMap(importMetadata.getAnnotationAttributes(EnableMongoWebSession.class.getName()));
if (attributes != null) { if (attributes != null) {
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds"); this.maxInactiveInterval = Duration
} .ofSeconds(attributes.<Integer>getNumber("maxInactiveIntervalInSeconds"));
else {
this.maxInactiveIntervalInSeconds = ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL;
} }
String collectionNameValue = (attributes != null) ? attributes.getString("collectionName") : ""; String collectionNameValue = (attributes != null) ? attributes.getString("collectionName") : "";
@@ -149,12 +146,17 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig
this.embeddedValueResolver = embeddedValueResolver; this.embeddedValueResolver = embeddedValueResolver;
} }
public Integer getMaxInactiveIntervalInSeconds() { public Duration getMaxInactiveInterval() {
return this.maxInactiveIntervalInSeconds; return this.maxInactiveInterval;
} }
public void setMaxInactiveInterval(Duration maxInactiveInterval) {
this.maxInactiveInterval = maxInactiveInterval;
}
@Deprecated
public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) { public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) {
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; setMaxInactiveInterval(Duration.ofSeconds(maxInactiveIntervalInSeconds));
} }
public String getCollectionName() { public String getCollectionName() {

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.MongoOperations;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.session.FindByIndexNameSessionRepository; import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.MapSession;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
@@ -76,20 +77,7 @@ public class MongoIndexedSessionRepositoryTest {
// then // then
assertThat(session.getId()).isNotEmpty(); assertThat(session.getId()).isNotEmpty();
assertThat(session.getMaxInactiveInterval().getSeconds()) assertThat(session.getMaxInactiveInterval().getSeconds())
.isEqualTo(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL); .isEqualTo(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
}
@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);
} }
@Test @Test
@@ -164,8 +152,7 @@ public class MongoIndexedSessionRepositoryTest {
Document sessionDocument = new Document(); Document sessionDocument = new Document();
sessionDocument.put("id", sessionId); sessionDocument.put("id", sessionId);
MongoSession mongoSession = new MongoSession(sessionId, MongoSession mongoSession = new MongoSession(sessionId, MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
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);

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.session.data.mongo; package org.springframework.session.data.mongo;
import java.time.Duration;
import java.util.UUID; import java.util.UUID;
import com.mongodb.BasicDBObject; 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.MongoOperations;
import org.springframework.data.mongodb.core.ReactiveMongoOperations; import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.index.IndexOperations; import org.springframework.data.mongodb.core.index.IndexOperations;
import org.springframework.session.MapSession;
import org.springframework.session.events.SessionDeletedEvent; import org.springframework.session.events.SessionDeletedEvent;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@@ -84,26 +86,8 @@ public class ReactiveMongoSessionRepositoryTest {
.as(StepVerifier::create) // .as(StepVerifier::create) //
.expectNextMatches((mongoSession) -> { .expectNextMatches((mongoSession) -> {
assertThat(mongoSession.getId()).isNotEmpty(); assertThat(mongoSession.getId()).isNotEmpty();
assertThat(mongoSession.getMaxInactiveInterval().getSeconds()) assertThat(mongoSession.getMaxInactiveInterval())
.isEqualTo(ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL); .isEqualTo(Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS));
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);
return true; return true;
}) // }) //
.verifyComplete(); .verifyComplete();

View File

@@ -17,6 +17,7 @@
package org.springframework.session.data.mongo.config.annotation.web.http; package org.springframework.session.data.mongo.config.annotation.web.http;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.time.Duration;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Order;
@@ -112,9 +113,8 @@ public class MongoHttpSessionConfigurationTest {
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class); MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
assertThat(repository).isNotNull(); assertThat(repository).extracting("defaultMaxInactiveInterval")
assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds")) .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
.isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS);
} }
@Test @Test
@@ -124,9 +124,8 @@ public class MongoHttpSessionConfigurationTest {
MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class); MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class);
assertThat(repository).isNotNull(); assertThat(repository).extracting("defaultMaxInactiveInterval")
assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds")) .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
.isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS);
} }
@Test @Test
@@ -161,7 +160,7 @@ public class MongoHttpSessionConfigurationTest {
MongoIndexedSessionRepository sessionRepository = this.context.getBean(MongoIndexedSessionRepository.class); MongoIndexedSessionRepository sessionRepository = this.context.getBean(MongoIndexedSessionRepository.class);
assertThat(sessionRepository).hasFieldOrPropertyWithValue("maxInactiveIntervalInSeconds", 10000); assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ofSeconds(10000));
} }
@Test @Test
@@ -198,7 +197,7 @@ public class MongoHttpSessionConfigurationTest {
void importConfigAndCustomize() { void importConfigAndCustomize() {
registerAndRefresh(ImportConfigAndCustomizeConfiguration.class); registerAndRefresh(ImportConfigAndCustomizeConfiguration.class);
MongoIndexedSessionRepository sessionRepository = this.context.getBean(MongoIndexedSessionRepository.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) { private void registerAndRefresh(Class<?>... annotatedClasses) {
@@ -266,7 +265,7 @@ public class MongoHttpSessionConfigurationTest {
static class CustomMaxInactiveIntervalInSecondsSetConfiguration extends MongoHttpSessionConfiguration { static class CustomMaxInactiveIntervalInSecondsSetConfiguration extends MongoHttpSessionConfiguration {
CustomMaxInactiveIntervalInSecondsSetConfiguration() { CustomMaxInactiveIntervalInSecondsSetConfiguration() {
setMaxInactiveIntervalInSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS); setMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
} }
} }
@@ -300,13 +299,13 @@ public class MongoHttpSessionConfigurationTest {
@Bean @Bean
@Order(0) @Order(0)
SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizerOne() { SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizerOne() {
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
} }
@Bean @Bean
@Order(1) @Order(1)
SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizerTwo() { SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizerTwo() {
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(10000); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ofSeconds(10000));
} }
} }
@@ -346,7 +345,7 @@ public class MongoHttpSessionConfigurationTest {
@Bean @Bean
SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizer() { SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
} }
} }

View File

@@ -17,6 +17,7 @@
package org.springframework.session.data.mongo.config.annotation.web.reactive; package org.springframework.session.data.mongo.config.annotation.web.reactive;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.time.Duration;
import java.util.Collections; import java.util.Collections;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
@@ -56,7 +57,7 @@ import static org.mockito.BDDMockito.verify;
* Verify various configurations through {@link EnableSpringWebSession}. * Verify various configurations through {@link EnableSpringWebSession}.
* *
* @author Greg Turnquist * @author Greg Turnquist
* @author Vedran Pavić * @author Vedran Pavic
*/ */
public class ReactiveMongoWebSessionConfigurationTest { public class ReactiveMongoWebSessionConfigurationTest {
@@ -128,7 +129,7 @@ public class ReactiveMongoWebSessionConfigurationTest {
} }
@Test @Test
void overridingIntervalAndCollectionNameThroughAnnotationShouldWork() throws IllegalAccessException { void overridingIntervalAndCollectionNameThroughAnnotationShouldWork() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(OverrideMongoParametersConfig.class); this.context.register(OverrideMongoParametersConfig.class);
@@ -136,17 +137,8 @@ public class ReactiveMongoWebSessionConfigurationTest {
ReactiveMongoSessionRepository repository = this.context.getBean(ReactiveMongoSessionRepository.class); ReactiveMongoSessionRepository repository = this.context.getBean(ReactiveMongoSessionRepository.class);
Field inactiveField = ReflectionUtils.findField(ReactiveMongoSessionRepository.class, assertThat(repository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ofSeconds(123));
"maxInactiveIntervalInSeconds"); assertThat(repository).extracting("collectionName").isEqualTo("test-case");
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");
} }
@Test @Test
@@ -174,7 +166,7 @@ public class ReactiveMongoWebSessionConfigurationTest {
ReactiveMongoSessionRepository repository = this.context.getBean(ReactiveMongoSessionRepository.class); ReactiveMongoSessionRepository repository = this.context.getBean(ReactiveMongoSessionRepository.class);
assertThat(repository.getCollectionName()).isEqualTo("custom-collection"); assertThat(repository.getCollectionName()).isEqualTo("custom-collection");
assertThat(repository.getMaxInactiveIntervalInSeconds()).isEqualTo(123); assertThat(repository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ofSeconds(123));
} }
@Test @Test
@@ -186,7 +178,7 @@ public class ReactiveMongoWebSessionConfigurationTest {
ReactiveMongoSessionRepository repository = this.context.getBean(ReactiveMongoSessionRepository.class); ReactiveMongoSessionRepository repository = this.context.getBean(ReactiveMongoSessionRepository.class);
assertThat(repository).hasFieldOrPropertyWithValue("maxInactiveIntervalInSeconds", 10000); assertThat(repository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ofSeconds(10000));
} }
@Test @Test
@@ -227,7 +219,7 @@ public class ReactiveMongoWebSessionConfigurationTest {
this.context.register(ImportConfigAndCustomizeConfiguration.class); this.context.register(ImportConfigAndCustomizeConfiguration.class);
this.context.refresh(); this.context.refresh();
ReactiveMongoSessionRepository sessionRepository = this.context.getBean(ReactiveMongoSessionRepository.class); 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() { CustomizedReactiveConfiguration() {
this.setCollectionName("custom-collection"); this.setCollectionName("custom-collection");
this.setMaxInactiveIntervalInSeconds(123); this.setMaxInactiveInterval(Duration.ofSeconds(123));
} }
@Bean @Bean
@@ -353,13 +345,13 @@ public class ReactiveMongoWebSessionConfigurationTest {
@Bean @Bean
@Order(0) @Order(0)
ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> sessionRepositoryCustomizerOne() { ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> sessionRepositoryCustomizerOne() {
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
} }
@Bean @Bean
@Order(1) @Order(1)
ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> sessionRepositoryCustomizerTwo() { ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> sessionRepositoryCustomizerTwo() {
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(10000); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ofSeconds(10000));
} }
} }
@@ -414,7 +406,7 @@ public class ReactiveMongoWebSessionConfigurationTest {
@Bean @Bean
ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> sessionRepositoryCustomizer() { ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> sessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2021 the original author or authors. * Copyright 2014-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -55,11 +55,7 @@ public class ReactiveRedisSessionRepository
*/ */
private String namespace = DEFAULT_NAMESPACE + ":"; private String namespace = DEFAULT_NAMESPACE + ":";
/** private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
* If non-null, this value is used to override the default value for
* {@link RedisSession#setMaxInactiveInterval(Duration)}.
*/
private Integer defaultMaxInactiveInterval;
private SaveMode saveMode = SaveMode.ON_SET_ATTRIBUTE; private SaveMode saveMode = SaveMode.ON_SET_ATTRIBUTE;
@@ -79,13 +75,13 @@ public class ReactiveRedisSessionRepository
} }
/** /**
* Sets the maximum inactive interval in seconds between requests before newly created * 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 * sessions will be invalidated. A negative time indicates that the session will never
* timeout. The default is 1800 (30 minutes). * time out. The default is 30 minutes.
* @param defaultMaxInactiveInterval the number of seconds that the {@link Session} * @param defaultMaxInactiveInterval the default maxInactiveInterval
* should be kept alive between client requests.
*/ */
public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) { public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
Assert.notNull(defaultMaxInactiveInterval, "defaultMaxInactiveInterval must not be null");
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval; this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
} }
@@ -110,9 +106,7 @@ public class ReactiveRedisSessionRepository
public Mono<RedisSession> createSession() { public Mono<RedisSession> createSession() {
return Mono.defer(() -> { return Mono.defer(() -> {
MapSession cached = new MapSession(); MapSession cached = new MapSession();
if (this.defaultMaxInactiveInterval != null) { cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
RedisSession session = new RedisSession(cached, true); RedisSession session = new RedisSession(cached, true);
return Mono.just(session); return Mono.just(session);
}); });

View File

@@ -307,11 +307,7 @@ public class RedisIndexedSessionRepository
private ApplicationEventPublisher eventPublisher = (event) -> { private ApplicationEventPublisher eventPublisher = (event) -> {
}; };
/** private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
* If non-null, this value is used to override the default value for
* {@link RedisSession#setMaxInactiveInterval(Duration)}.
*/
private Integer defaultMaxInactiveInterval;
private IndexResolver<Session> indexResolver = new DelegatingIndexResolver<>(new PrincipalNameIndexResolver<>()); private IndexResolver<Session> indexResolver = new DelegatingIndexResolver<>(new PrincipalNameIndexResolver<>());
@@ -373,13 +369,13 @@ public class RedisIndexedSessionRepository
} }
/** /**
* Sets the maximum inactive interval in seconds between requests before newly created * 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 * sessions will be invalidated. A negative time indicates that the session will never
* timeout. The default is 1800 (30 minutes). * time out. The default is 30 minutes.
* @param defaultMaxInactiveInterval the number of seconds that the {@link Session} * @param defaultMaxInactiveInterval the default maxInactiveInterval
* should be kept alive between client requests.
*/ */
public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) { public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
Assert.notNull(defaultMaxInactiveInterval, "defaultMaxInactiveInterval must not be null");
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval; this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
} }
@@ -560,9 +556,7 @@ public class RedisIndexedSessionRepository
@Override @Override
public RedisSession createSession() { public RedisSession createSession() {
MapSession cached = new MapSession(); MapSession cached = new MapSession();
if (this.defaultMaxInactiveInterval != null) { cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
RedisSession session = new RedisSession(cached, true); RedisSession session = new RedisSession(cached, true);
session.flushImmediateIfNecessary(); session.flushImmediateIfNecessary();
return session; return session;

View File

@@ -18,7 +18,6 @@ package org.springframework.session.data.redis;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@@ -68,7 +67,9 @@ public class RedisSessionRepository implements SessionRepository<RedisSessionRep
} }
/** /**
* Set the default maxInactiveInterval. * 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 * @param defaultMaxInactiveInterval the default maxInactiveInterval
*/ */
public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) { public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
@@ -296,8 +297,8 @@ public class RedisSessionRepository implements SessionRepository<RedisSessionRep
String key = getSessionKey(getId()); String key = getSessionKey(getId());
RedisSessionRepository.this.sessionRedisOperations.opsForHash().putAll(key, new HashMap<>(this.delta)); RedisSessionRepository.this.sessionRedisOperations.opsForHash().putAll(key, new HashMap<>(this.delta));
RedisSessionRepository.this.sessionRedisOperations.expireAt(key, RedisSessionRepository.this.sessionRedisOperations.expireAt(key,
Date.from(Instant.ofEpochMilli(getLastAccessedTime().toEpochMilli()) Instant.ofEpochMilli(getLastAccessedTime().toEpochMilli())
.plusSeconds(getMaxInactiveInterval().getSeconds()))); .plusSeconds(getMaxInactiveInterval().getSeconds()));
this.delta.clear(); this.delta.clear();
} }

View File

@@ -16,6 +16,7 @@
package org.springframework.session.data.redis.config.annotation.web.http; package org.springframework.session.data.redis.config.annotation.web.http;
import java.time.Duration;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -53,7 +54,7 @@ import org.springframework.util.Assert;
public abstract class AbstractRedisHttpSessionConfiguration<T extends SessionRepository<? extends Session>> public abstract class AbstractRedisHttpSessionConfiguration<T extends SessionRepository<? extends Session>>
extends SpringHttpSessionConfiguration implements BeanClassLoaderAware { extends SpringHttpSessionConfiguration implements BeanClassLoaderAware {
private Integer maxInactiveIntervalInSeconds = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS; private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL;
private String redisNamespace = RedisSessionRepository.DEFAULT_KEY_NAMESPACE; private String redisNamespace = RedisSessionRepository.DEFAULT_KEY_NAMESPACE;
@@ -71,12 +72,17 @@ public abstract class AbstractRedisHttpSessionConfiguration<T extends SessionRep
public abstract T sessionRepository(); public abstract T sessionRepository();
public void setMaxInactiveIntervalInSeconds(int maxInactiveIntervalInSeconds) { public void setMaxInactiveInterval(Duration maxInactiveInterval) {
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; this.maxInactiveInterval = maxInactiveInterval;
} }
protected Integer getMaxInactiveIntervalInSeconds() { @Deprecated
return this.maxInactiveIntervalInSeconds; public void setMaxInactiveIntervalInSeconds(int maxInactiveIntervalInSeconds) {
setMaxInactiveInterval(Duration.ofSeconds(maxInactiveIntervalInSeconds));
}
protected Duration getMaxInactiveInterval() {
return this.maxInactiveInterval;
} }
public void setRedisNamespace(String namespace) { public void setRedisNamespace(String namespace) {

View File

@@ -54,7 +54,7 @@ public class RedisHttpSessionConfiguration extends AbstractRedisHttpSessionConfi
public RedisSessionRepository sessionRepository() { public RedisSessionRepository sessionRepository() {
RedisTemplate<String, Object> redisTemplate = createRedisTemplate(); RedisTemplate<String, Object> redisTemplate = createRedisTemplate();
RedisSessionRepository sessionRepository = new RedisSessionRepository(redisTemplate); RedisSessionRepository sessionRepository = new RedisSessionRepository(redisTemplate);
sessionRepository.setDefaultMaxInactiveInterval(Duration.ofSeconds(getMaxInactiveIntervalInSeconds())); sessionRepository.setDefaultMaxInactiveInterval(getMaxInactiveInterval());
if (StringUtils.hasText(getRedisNamespace())) { if (StringUtils.hasText(getRedisNamespace())) {
sessionRepository.setRedisKeyNamespace(getRedisNamespace()); sessionRepository.setRedisKeyNamespace(getRedisNamespace());
} }
@@ -78,7 +78,7 @@ public class RedisHttpSessionConfiguration extends AbstractRedisHttpSessionConfi
if (attributes == null) { if (attributes == null) {
return; return;
} }
setMaxInactiveIntervalInSeconds(attributes.getNumber("maxInactiveIntervalInSeconds")); setMaxInactiveInterval(Duration.ofSeconds(attributes.<Integer>getNumber("maxInactiveIntervalInSeconds")));
String redisNamespaceValue = attributes.getString("redisNamespace"); String redisNamespaceValue = attributes.getString("redisNamespace");
if (StringUtils.hasText(redisNamespaceValue)) { if (StringUtils.hasText(redisNamespaceValue)) {
setRedisNamespace(this.embeddedValueResolver.resolveStringValue(redisNamespaceValue)); setRedisNamespace(this.embeddedValueResolver.resolveStringValue(redisNamespaceValue));

View File

@@ -16,6 +16,7 @@
package org.springframework.session.data.redis.config.annotation.web.http; package org.springframework.session.data.redis.config.annotation.web.http;
import java.time.Duration;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
@@ -91,7 +92,7 @@ public class RedisIndexedHttpSessionConfiguration
if (getDefaultRedisSerializer() != null) { if (getDefaultRedisSerializer() != null) {
sessionRepository.setDefaultSerializer(getDefaultRedisSerializer()); sessionRepository.setDefaultSerializer(getDefaultRedisSerializer());
} }
sessionRepository.setDefaultMaxInactiveInterval(getMaxInactiveIntervalInSeconds()); sessionRepository.setDefaultMaxInactiveInterval(getMaxInactiveInterval());
if (StringUtils.hasText(getRedisNamespace())) { if (StringUtils.hasText(getRedisNamespace())) {
sessionRepository.setRedisKeyNamespace(getRedisNamespace()); sessionRepository.setRedisKeyNamespace(getRedisNamespace());
} }
@@ -178,7 +179,7 @@ public class RedisIndexedHttpSessionConfiguration
if (attributes == null) { if (attributes == null) {
return; return;
} }
setMaxInactiveIntervalInSeconds(attributes.getNumber("maxInactiveIntervalInSeconds")); setMaxInactiveInterval(Duration.ofSeconds(attributes.<Integer>getNumber("maxInactiveIntervalInSeconds")));
String redisNamespaceValue = attributes.getString("redisNamespace"); String redisNamespaceValue = attributes.getString("redisNamespace");
if (StringUtils.hasText(redisNamespaceValue)) { if (StringUtils.hasText(redisNamespaceValue)) {
setRedisNamespace(this.embeddedValueResolver.resolveStringValue(redisNamespaceValue)); setRedisNamespace(this.embeddedValueResolver.resolveStringValue(redisNamespaceValue));

View File

@@ -16,6 +16,7 @@
package org.springframework.session.data.redis.config.annotation.web.server; package org.springframework.session.data.redis.config.annotation.web.server;
import java.time.Duration;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -59,7 +60,7 @@ import org.springframework.web.server.session.WebSessionManager;
public class RedisWebSessionConfiguration extends SpringWebSessionConfiguration public class RedisWebSessionConfiguration extends SpringWebSessionConfiguration
implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware { implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware {
private Integer maxInactiveIntervalInSeconds = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS; private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL;
private String redisNamespace = ReactiveRedisSessionRepository.DEFAULT_NAMESPACE; private String redisNamespace = ReactiveRedisSessionRepository.DEFAULT_NAMESPACE;
@@ -79,7 +80,7 @@ public class RedisWebSessionConfiguration extends SpringWebSessionConfiguration
public ReactiveRedisSessionRepository sessionRepository() { public ReactiveRedisSessionRepository sessionRepository() {
ReactiveRedisTemplate<String, Object> reactiveRedisTemplate = createReactiveRedisTemplate(); ReactiveRedisTemplate<String, Object> reactiveRedisTemplate = createReactiveRedisTemplate();
ReactiveRedisSessionRepository sessionRepository = new ReactiveRedisSessionRepository(reactiveRedisTemplate); ReactiveRedisSessionRepository sessionRepository = new ReactiveRedisSessionRepository(reactiveRedisTemplate);
sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds); sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveInterval);
if (StringUtils.hasText(this.redisNamespace)) { if (StringUtils.hasText(this.redisNamespace)) {
sessionRepository.setRedisKeyNamespace(this.redisNamespace); sessionRepository.setRedisKeyNamespace(this.redisNamespace);
} }
@@ -89,8 +90,13 @@ public class RedisWebSessionConfiguration extends SpringWebSessionConfiguration
return sessionRepository; return sessionRepository;
} }
public void setMaxInactiveInterval(Duration maxInactiveInterval) {
this.maxInactiveInterval = maxInactiveInterval;
}
@Deprecated
public void setMaxInactiveIntervalInSeconds(int maxInactiveIntervalInSeconds) { public void setMaxInactiveIntervalInSeconds(int maxInactiveIntervalInSeconds) {
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; setMaxInactiveInterval(Duration.ofSeconds(maxInactiveIntervalInSeconds));
} }
public void setRedisNamespace(String namespace) { public void setRedisNamespace(String namespace) {
@@ -143,7 +149,7 @@ public class RedisWebSessionConfiguration extends SpringWebSessionConfiguration
if (attributes == null) { if (attributes == null) {
return; return;
} }
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds"); this.maxInactiveInterval = Duration.ofSeconds(attributes.<Integer>getNumber("maxInactiveIntervalInSeconds"));
String redisNamespaceValue = attributes.getString("redisNamespace"); String redisNamespaceValue = attributes.getString("redisNamespace");
if (StringUtils.hasText(redisNamespaceValue)) { if (StringUtils.hasText(redisNamespaceValue)) {
this.redisNamespace = this.embeddedValueResolver.resolveStringValue(redisNamespaceValue); this.redisNamespace = this.embeddedValueResolver.resolveStringValue(redisNamespaceValue);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2021 the original author or authors. * Copyright 2014-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -103,9 +103,10 @@ class ReactiveRedisSessionRepositoryTests {
@Test @Test
void customMaxInactiveInterval() { void customMaxInactiveInterval() {
this.repository.setDefaultMaxInactiveInterval(600); Duration interval = Duration.ofMinutes(10);
this.repository.setDefaultMaxInactiveInterval(interval);
assertThat(ReflectionTestUtils.getField(this.repository, "defaultMaxInactiveInterval")).isEqualTo(600); assertThat(this.repository).extracting("defaultMaxInactiveInterval").isEqualTo(interval);
} }
@Test @Test
@@ -118,11 +119,11 @@ class ReactiveRedisSessionRepositoryTests {
@Test @Test
void createSessionCustomMaxInactiveInterval() { void createSessionCustomMaxInactiveInterval() {
this.repository.setDefaultMaxInactiveInterval(600); Duration interval = Duration.ofMinutes(10);
this.repository.setDefaultMaxInactiveInterval(interval);
StepVerifier.create(this.repository.createSession()) StepVerifier.create(this.repository.createSession())
.consumeNextWith( .consumeNextWith((session) -> assertThat(session.getMaxInactiveInterval()).isEqualTo(interval))
(session) -> assertThat(session.getMaxInactiveInterval()).isEqualTo(Duration.ofSeconds(600)))
.verifyComplete(); .verifyComplete();
} }

View File

@@ -158,10 +158,10 @@ class RedisIndexedSessionRepositoryTests {
@Test @Test
void createSessionCustomMaxInactiveInterval() { void createSessionCustomMaxInactiveInterval() {
int interval = 1; Duration interval = Duration.ofSeconds(1);
this.redisRepository.setDefaultMaxInactiveInterval(interval); this.redisRepository.setDefaultMaxInactiveInterval(interval);
Session session = this.redisRepository.createSession(); Session session = this.redisRepository.createSession();
assertThat(session.getMaxInactiveInterval()).isEqualTo(Duration.ofSeconds(interval)); assertThat(session.getMaxInactiveInterval()).isEqualTo(interval);
} }
@Test @Test
@@ -663,7 +663,7 @@ class RedisIndexedSessionRepositoryTests {
given(this.redisOperations.<String, Object>boundHashOps(anyString())).willReturn(this.boundHashOperations); given(this.redisOperations.<String, Object>boundHashOps(anyString())).willReturn(this.boundHashOperations);
given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations); given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations); given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations);
this.redisRepository.setDefaultMaxInactiveInterval(60); this.redisRepository.setDefaultMaxInactiveInterval(Duration.ofSeconds(60));
this.redisRepository.setFlushMode(FlushMode.IMMEDIATE); this.redisRepository.setFlushMode(FlushMode.IMMEDIATE);
this.redisRepository.createSession(); this.redisRepository.createSession();
Map<String, Object> delta = getDelta(); Map<String, Object> delta = getDelta();

View File

@@ -20,7 +20,6 @@ import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -378,9 +377,9 @@ class RedisSessionRepositoryTests {
return "spring:session:sessions:" + sessionId; return "spring:session:sessions:" + sessionId;
} }
private static Date getExpiry(RedisSession session) { private static Instant getExpiry(RedisSession session) {
return Date.from(Instant.ofEpochMilli(session.getLastAccessedTime().toEpochMilli()) return Instant.ofEpochMilli(session.getLastAccessedTime().toEpochMilli())
.plusSeconds(session.getMaxInactiveInterval().getSeconds())); .plusSeconds(session.getMaxInactiveInterval().getSeconds());
} }
private static Map mapOf(Object... objects) { private static Map mapOf(Object... objects) {

View File

@@ -16,6 +16,7 @@
package org.springframework.session.data.redis.config.annotation.web.http; package org.springframework.session.data.redis.config.annotation.web.http;
import java.time.Duration;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@@ -228,15 +229,15 @@ class RedisIndexedHttpSessionConfigurationTests {
void sessionRepositoryCustomizer() { void sessionRepositoryCustomizer() {
registerAndRefresh(RedisConfig.class, SessionRepositoryCustomizerConfiguration.class); registerAndRefresh(RedisConfig.class, SessionRepositoryCustomizerConfiguration.class);
RedisIndexedSessionRepository sessionRepository = this.context.getBean(RedisIndexedSessionRepository.class); RedisIndexedSessionRepository sessionRepository = this.context.getBean(RedisIndexedSessionRepository.class);
assertThat(sessionRepository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", assertThat(sessionRepository).extracting("defaultMaxInactiveInterval")
MAX_INACTIVE_INTERVAL_IN_SECONDS); .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
} }
@Test @Test
void importConfigAndCustomize() { void importConfigAndCustomize() {
registerAndRefresh(RedisConfig.class, ImportConfigAndCustomizeConfiguration.class); registerAndRefresh(RedisConfig.class, ImportConfigAndCustomizeConfiguration.class);
RedisIndexedSessionRepository sessionRepository = this.context.getBean(RedisIndexedSessionRepository.class); RedisIndexedSessionRepository sessionRepository = this.context.getBean(RedisIndexedSessionRepository.class);
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(0); assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ZERO);
} }
private void registerAndRefresh(Class<?>... annotatedClasses) { private void registerAndRefresh(Class<?>... annotatedClasses) {
@@ -420,14 +421,14 @@ class RedisIndexedHttpSessionConfigurationTests {
@Bean @Bean
@Order(0) @Order(0)
SessionRepositoryCustomizer<RedisIndexedSessionRepository> sessionRepositoryCustomizerOne() { SessionRepositoryCustomizer<RedisIndexedSessionRepository> sessionRepositoryCustomizerOne() {
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
} }
@Bean @Bean
@Order(1) @Order(1)
SessionRepositoryCustomizer<RedisIndexedSessionRepository> sessionRepositoryCustomizerTwo() { SessionRepositoryCustomizer<RedisIndexedSessionRepository> sessionRepositoryCustomizerTwo() {
return (sessionRepository) -> sessionRepository return (sessionRepository) -> sessionRepository
.setDefaultMaxInactiveInterval(MAX_INACTIVE_INTERVAL_IN_SECONDS); .setDefaultMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
} }
} }
@@ -438,7 +439,7 @@ class RedisIndexedHttpSessionConfigurationTests {
@Bean @Bean
SessionRepositoryCustomizer<RedisIndexedSessionRepository> sessionRepositoryCustomizer() { SessionRepositoryCustomizer<RedisIndexedSessionRepository> sessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
} }
} }

View File

@@ -16,6 +16,7 @@
package org.springframework.session.data.redis.config.annotation.web.http.gh109; package org.springframework.session.data.redis.config.annotation.web.http.gh109;
import java.time.Duration;
import java.util.Properties; import java.util.Properties;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -60,7 +61,7 @@ class Gh109Tests {
@Configuration @Configuration
static class Config extends RedisHttpSessionConfiguration { static class Config extends RedisHttpSessionConfiguration {
int sessionTimeout = 100; Duration sessionTimeout = Duration.ofSeconds(100);
/** /**
* override sessionRepository construction to set the custom session-timeout * override sessionRepository construction to set the custom session-timeout

View File

@@ -16,6 +16,8 @@
package org.springframework.session.data.redis.config.annotation.web.server; package org.springframework.session.data.redis.config.annotation.web.server;
import java.time.Duration;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -103,9 +105,8 @@ class RedisWebSessionConfigurationTests {
registerAndRefresh(RedisConfig.class, CustomMaxInactiveIntervalConfig.class); registerAndRefresh(RedisConfig.class, CustomMaxInactiveIntervalConfig.class);
ReactiveRedisSessionRepository repository = this.context.getBean(ReactiveRedisSessionRepository.class); ReactiveRedisSessionRepository repository = this.context.getBean(ReactiveRedisSessionRepository.class);
assertThat(repository).isNotNull(); assertThat(repository).extracting("defaultMaxInactiveInterval")
assertThat(ReflectionTestUtils.getField(repository, "defaultMaxInactiveInterval")) .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
.isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS);
} }
@Test @Test
@@ -222,15 +223,15 @@ class RedisWebSessionConfigurationTests {
void sessionRepositoryCustomizer() { void sessionRepositoryCustomizer() {
registerAndRefresh(RedisConfig.class, SessionRepositoryCustomizerConfiguration.class); registerAndRefresh(RedisConfig.class, SessionRepositoryCustomizerConfiguration.class);
ReactiveRedisSessionRepository sessionRepository = this.context.getBean(ReactiveRedisSessionRepository.class); ReactiveRedisSessionRepository sessionRepository = this.context.getBean(ReactiveRedisSessionRepository.class);
assertThat(sessionRepository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", assertThat(sessionRepository).extracting("defaultMaxInactiveInterval")
MAX_INACTIVE_INTERVAL_IN_SECONDS); .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
} }
@Test @Test
void importConfigAndCustomize() { void importConfigAndCustomize() {
registerAndRefresh(RedisConfig.class, ImportConfigAndCustomizeConfiguration.class); registerAndRefresh(RedisConfig.class, ImportConfigAndCustomizeConfiguration.class);
ReactiveRedisSessionRepository sessionRepository = this.context.getBean(ReactiveRedisSessionRepository.class); ReactiveRedisSessionRepository sessionRepository = this.context.getBean(ReactiveRedisSessionRepository.class);
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(0); assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ZERO);
} }
private void registerAndRefresh(Class<?>... annotatedClasses) { private void registerAndRefresh(Class<?>... annotatedClasses) {
@@ -377,14 +378,14 @@ class RedisWebSessionConfigurationTests {
@Bean @Bean
@Order(0) @Order(0)
ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> sessionRepositoryCustomizerOne() { ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> sessionRepositoryCustomizerOne() {
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
} }
@Bean @Bean
@Order(1) @Order(1)
ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> sessionRepositoryCustomizerTwo() { ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> sessionRepositoryCustomizerTwo() {
return (sessionRepository) -> sessionRepository return (sessionRepository) -> sessionRepository
.setDefaultMaxInactiveInterval(MAX_INACTIVE_INTERVAL_IN_SECONDS); .setDefaultMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
} }
} }
@@ -395,7 +396,7 @@ class RedisWebSessionConfigurationTests {
@Bean @Bean
ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> sessionRepositoryCustomizer() { ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> sessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
} }
} }

View File

@@ -137,11 +137,7 @@ public class HazelcastIndexedSessionRepository
private ApplicationEventPublisher eventPublisher = (event) -> { private ApplicationEventPublisher eventPublisher = (event) -> {
}; };
/** private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
* If non-null, this value is used to override
* {@link MapSession#setMaxInactiveInterval(Duration)}.
*/
private Integer defaultMaxInactiveInterval;
private IndexResolver<Session> indexResolver = new DelegatingIndexResolver<>(new PrincipalNameIndexResolver<>()); private IndexResolver<Session> indexResolver = new DelegatingIndexResolver<>(new PrincipalNameIndexResolver<>());
@@ -190,10 +186,11 @@ public class HazelcastIndexedSessionRepository
/** /**
* Set the maximum inactive interval in seconds between requests before newly created * 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 * sessions will be invalidated. A negative time indicates that the session will never
* timeout. The default is 1800 (30 minutes). * time out. The default is 30 minutes.
* @param defaultMaxInactiveInterval the maximum inactive interval in seconds * @param defaultMaxInactiveInterval the default maxInactiveInterval
*/ */
public void setDefaultMaxInactiveInterval(Integer defaultMaxInactiveInterval) { public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
Assert.notNull(defaultMaxInactiveInterval, "defaultMaxInactiveInterval must not be null");
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval; this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
} }
@@ -236,9 +233,7 @@ public class HazelcastIndexedSessionRepository
@Override @Override
public HazelcastSession createSession() { public HazelcastSession createSession() {
MapSession cached = new MapSession(); MapSession cached = new MapSession();
if (this.defaultMaxInactiveInterval != null) { cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
HazelcastSession session = new HazelcastSession(cached, true); HazelcastSession session = new HazelcastSession(cached, true);
session.flushImmediateIfNecessary(); session.flushImmediateIfNecessary();
return session; return session;

View File

@@ -16,6 +16,7 @@
package org.springframework.session.hazelcast.config.annotation.web.http; package org.springframework.session.hazelcast.config.annotation.web.http;
import java.time.Duration;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -56,7 +57,7 @@ import org.springframework.util.StringUtils;
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfiguration implements ImportAware { public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfiguration implements ImportAware {
private Integer maxInactiveIntervalInSeconds = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS; private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL;
private String sessionMapName = HazelcastIndexedSessionRepository.DEFAULT_SESSION_MAP_NAME; private String sessionMapName = HazelcastIndexedSessionRepository.DEFAULT_SESSION_MAP_NAME;
@@ -77,8 +78,13 @@ public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfigur
return createHazelcastIndexedSessionRepository(); return createHazelcastIndexedSessionRepository();
} }
public void setMaxInactiveInterval(Duration maxInactiveInterval) {
this.maxInactiveInterval = maxInactiveInterval;
}
@Deprecated
public void setMaxInactiveIntervalInSeconds(int maxInactiveIntervalInSeconds) { public void setMaxInactiveIntervalInSeconds(int maxInactiveIntervalInSeconds) {
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; setMaxInactiveInterval(Duration.ofSeconds(maxInactiveIntervalInSeconds));
} }
public void setSessionMapName(String sessionMapName) { public void setSessionMapName(String sessionMapName) {
@@ -128,7 +134,7 @@ public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfigur
if (attributes == null) { if (attributes == null) {
return; return;
} }
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds"); this.maxInactiveInterval = Duration.ofSeconds(attributes.<Integer>getNumber("maxInactiveIntervalInSeconds"));
String sessionMapNameValue = attributes.getString("sessionMapName"); String sessionMapNameValue = attributes.getString("sessionMapName");
if (StringUtils.hasText(sessionMapNameValue)) { if (StringUtils.hasText(sessionMapNameValue)) {
this.sessionMapName = sessionMapNameValue; this.sessionMapName = sessionMapNameValue;
@@ -147,7 +153,7 @@ public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfigur
if (StringUtils.hasText(this.sessionMapName)) { if (StringUtils.hasText(this.sessionMapName)) {
sessionRepository.setSessionMapName(this.sessionMapName); sessionRepository.setSessionMapName(this.sessionMapName);
} }
sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds); sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveInterval);
sessionRepository.setFlushMode(this.flushMode); sessionRepository.setFlushMode(this.flushMode);
sessionRepository.setSaveMode(this.saveMode); sessionRepository.setSaveMode(this.saveMode);
this.sessionRepositoryCustomizers this.sessionRepositoryCustomizers

View File

@@ -105,12 +105,12 @@ class HazelcastIndexedSessionRepositoryTests {
void createSessionCustomMaxInactiveInterval() { void createSessionCustomMaxInactiveInterval() {
verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean()); verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean());
int interval = 1; Duration interval = Duration.ofSeconds(1);
this.repository.setDefaultMaxInactiveInterval(interval); this.repository.setDefaultMaxInactiveInterval(interval);
HazelcastSession session = this.repository.createSession(); HazelcastSession session = this.repository.createSession();
assertThat(session.getMaxInactiveInterval()).isEqualTo(Duration.ofSeconds(interval)); assertThat(session.getMaxInactiveInterval()).isEqualTo(interval);
verifyNoMoreInteractions(this.sessions); verifyNoMoreInteractions(this.sessions);
} }

View File

@@ -16,6 +16,8 @@
package org.springframework.session.hazelcast.config.annotation.web.http; package org.springframework.session.hazelcast.config.annotation.web.http;
import java.time.Duration;
import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap; import com.hazelcast.map.IMap;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
@@ -106,9 +108,8 @@ class HazelcastHttpSessionConfigurationTests {
registerAndRefresh(BaseConfiguration.class, CustomMaxInactiveIntervalInSecondsSetConfiguration.class); registerAndRefresh(BaseConfiguration.class, CustomMaxInactiveIntervalInSecondsSetConfiguration.class);
HazelcastIndexedSessionRepository repository = this.context.getBean(HazelcastIndexedSessionRepository.class); HazelcastIndexedSessionRepository repository = this.context.getBean(HazelcastIndexedSessionRepository.class);
assertThat(repository).isNotNull(); assertThat(repository).extracting("defaultMaxInactiveInterval")
assertThat(ReflectionTestUtils.getField(repository, "defaultMaxInactiveInterval")) .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
.isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS);
} }
@Test @Test
@@ -116,9 +117,8 @@ class HazelcastHttpSessionConfigurationTests {
registerAndRefresh(CustomMaxInactiveIntervalInSecondsConfiguration.class); registerAndRefresh(CustomMaxInactiveIntervalInSecondsConfiguration.class);
HazelcastIndexedSessionRepository repository = this.context.getBean(HazelcastIndexedSessionRepository.class); HazelcastIndexedSessionRepository repository = this.context.getBean(HazelcastIndexedSessionRepository.class);
assertThat(repository).isNotNull(); assertThat(repository).extracting("defaultMaxInactiveInterval")
assertThat(ReflectionTestUtils.getField(repository, "defaultMaxInactiveInterval")) .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
.isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS);
} }
@Test @Test
@@ -227,8 +227,8 @@ class HazelcastHttpSessionConfigurationTests {
registerAndRefresh(SessionRepositoryCustomizerConfiguration.class); registerAndRefresh(SessionRepositoryCustomizerConfiguration.class);
HazelcastIndexedSessionRepository sessionRepository = this.context HazelcastIndexedSessionRepository sessionRepository = this.context
.getBean(HazelcastIndexedSessionRepository.class); .getBean(HazelcastIndexedSessionRepository.class);
assertThat(sessionRepository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", assertThat(sessionRepository).extracting("defaultMaxInactiveInterval")
MAX_INACTIVE_INTERVAL_IN_SECONDS); .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
} }
@Test @Test
@@ -236,7 +236,7 @@ class HazelcastHttpSessionConfigurationTests {
registerAndRefresh(ImportConfigAndCustomizeConfiguration.class); registerAndRefresh(ImportConfigAndCustomizeConfiguration.class);
HazelcastIndexedSessionRepository sessionRepository = this.context HazelcastIndexedSessionRepository sessionRepository = this.context
.getBean(HazelcastIndexedSessionRepository.class); .getBean(HazelcastIndexedSessionRepository.class);
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(0); assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ZERO);
} }
private void registerAndRefresh(Class<?>... annotatedClasses) { private void registerAndRefresh(Class<?>... annotatedClasses) {
@@ -289,7 +289,7 @@ class HazelcastHttpSessionConfigurationTests {
static class CustomMaxInactiveIntervalInSecondsSetConfiguration extends HazelcastHttpSessionConfiguration { static class CustomMaxInactiveIntervalInSecondsSetConfiguration extends HazelcastHttpSessionConfiguration {
CustomMaxInactiveIntervalInSecondsSetConfiguration() { CustomMaxInactiveIntervalInSecondsSetConfiguration() {
setMaxInactiveIntervalInSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS); setMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
} }
} }
@@ -443,14 +443,14 @@ class HazelcastHttpSessionConfigurationTests {
@Bean @Bean
@Order(0) @Order(0)
SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> sessionRepositoryCustomizerOne() { SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> sessionRepositoryCustomizerOne() {
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
} }
@Bean @Bean
@Order(1) @Order(1)
SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> sessionRepositoryCustomizerTwo() { SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> sessionRepositoryCustomizerTwo() {
return (sessionRepository) -> sessionRepository return (sessionRepository) -> sessionRepository
.setDefaultMaxInactiveInterval(MAX_INACTIVE_INTERVAL_IN_SECONDS); .setDefaultMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
} }
} }
@@ -461,7 +461,7 @@ class HazelcastHttpSessionConfigurationTests {
@Bean @Bean
SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> sessionRepositoryCustomizer() { SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> sessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
} }
} }

View File

@@ -236,11 +236,7 @@ public class JdbcIndexedSessionRepository implements
private String deleteSessionsByExpiryTimeQuery; private String deleteSessionsByExpiryTimeQuery;
/** private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
* If non-null, this value is used to override the default value for
* {@link JdbcSession#setMaxInactiveInterval(Duration)}.
*/
private Integer defaultMaxInactiveInterval;
private IndexResolver<Session> indexResolver = new DelegatingIndexResolver<>(new PrincipalNameIndexResolver<>()); private IndexResolver<Session> indexResolver = new DelegatingIndexResolver<>(new PrincipalNameIndexResolver<>());
@@ -386,10 +382,11 @@ public class JdbcIndexedSessionRepository implements
/** /**
* Set the maximum inactive interval in seconds between requests before newly created * 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 * sessions will be invalidated. A negative time indicates that the session will never
* timeout. The default is 1800 (30 minutes). * time out. The default is 30 minutes.
* @param defaultMaxInactiveInterval the maximum inactive interval in seconds * @param defaultMaxInactiveInterval the default maxInactiveInterval
*/ */
public void setDefaultMaxInactiveInterval(Integer defaultMaxInactiveInterval) { public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
Assert.notNull(defaultMaxInactiveInterval, "defaultMaxInactiveInterval must not be null");
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval; this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
} }
@@ -452,9 +449,7 @@ public class JdbcIndexedSessionRepository implements
@Override @Override
public JdbcSession createSession() { public JdbcSession createSession() {
MapSession delegate = new MapSession(); MapSession delegate = new MapSession();
if (this.defaultMaxInactiveInterval != null) { delegate.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
delegate.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
JdbcSession session = new JdbcSession(delegate, UUID.randomUUID().toString(), true); JdbcSession session = new JdbcSession(delegate, UUID.randomUUID().toString(), true);
session.flushIfRequired(); session.flushIfRequired();
return session; return session;

View File

@@ -17,6 +17,7 @@
package org.springframework.session.jdbc.config.annotation.web.http; package org.springframework.session.jdbc.config.annotation.web.http;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
import java.time.Duration;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -77,7 +78,7 @@ import org.springframework.util.StringValueResolver;
public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware { implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware {
private Integer maxInactiveIntervalInSeconds = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS; private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL;
private String tableName = JdbcIndexedSessionRepository.DEFAULT_TABLE_NAME; private String tableName = JdbcIndexedSessionRepository.DEFAULT_TABLE_NAME;
@@ -118,7 +119,7 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
if (StringUtils.hasText(this.tableName)) { if (StringUtils.hasText(this.tableName)) {
sessionRepository.setTableName(this.tableName); sessionRepository.setTableName(this.tableName);
} }
sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds); sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveInterval);
sessionRepository.setFlushMode(this.flushMode); sessionRepository.setFlushMode(this.flushMode);
sessionRepository.setSaveMode(this.saveMode); sessionRepository.setSaveMode(this.saveMode);
sessionRepository.setCleanupCron(this.cleanupCron); sessionRepository.setCleanupCron(this.cleanupCron);
@@ -158,8 +159,13 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
} }
} }
public void setMaxInactiveInterval(Duration maxInactiveInterval) {
this.maxInactiveInterval = maxInactiveInterval;
}
@Deprecated
public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) { public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) {
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; setMaxInactiveInterval(Duration.ofSeconds(maxInactiveIntervalInSeconds));
} }
public void setTableName(String tableName) { public void setTableName(String tableName) {
@@ -246,7 +252,7 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
if (attributes == null) { if (attributes == null) {
return; return;
} }
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds"); this.maxInactiveInterval = Duration.ofSeconds(attributes.<Integer>getNumber("maxInactiveIntervalInSeconds"));
String tableNameValue = attributes.getString("tableName"); String tableNameValue = attributes.getString("tableName");
if (StringUtils.hasText(tableNameValue)) { if (StringUtils.hasText(tableNameValue)) {
this.tableName = this.embeddedValueResolver.resolveStringValue(tableNameValue); this.tableName = this.embeddedValueResolver.resolveStringValue(tableNameValue);

View File

@@ -274,13 +274,13 @@ class JdbcIndexedSessionRepositoryTests {
@Test @Test
void createSessionCustomMaxInactiveInterval() { void createSessionCustomMaxInactiveInterval() {
int interval = 1; Duration interval = Duration.ofSeconds(1);
this.repository.setDefaultMaxInactiveInterval(interval); this.repository.setDefaultMaxInactiveInterval(interval);
JdbcSession session = this.repository.createSession(); JdbcSession session = this.repository.createSession();
assertThat(session.isNew()).isTrue(); assertThat(session.isNew()).isTrue();
assertThat(session.getMaxInactiveInterval()).isEqualTo(Duration.ofSeconds(interval)); assertThat(session.getMaxInactiveInterval()).isEqualTo(interval);
verifyNoMoreInteractions(this.jdbcOperations); verifyNoMoreInteractions(this.jdbcOperations);
} }

View File

@@ -16,6 +16,8 @@
package org.springframework.session.jdbc.config.annotation.web.http; package org.springframework.session.jdbc.config.annotation.web.http;
import java.time.Duration;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
@@ -118,9 +120,8 @@ class JdbcHttpSessionConfigurationTests {
CustomMaxInactiveIntervalInSecondsAnnotationConfiguration.class); CustomMaxInactiveIntervalInSecondsAnnotationConfiguration.class);
JdbcIndexedSessionRepository repository = this.context.getBean(JdbcIndexedSessionRepository.class); JdbcIndexedSessionRepository repository = this.context.getBean(JdbcIndexedSessionRepository.class);
assertThat(repository).isNotNull(); assertThat(repository).extracting("defaultMaxInactiveInterval")
assertThat(ReflectionTestUtils.getField(repository, "defaultMaxInactiveInterval")) .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
.isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS);
} }
@Test @Test
@@ -128,9 +129,8 @@ class JdbcHttpSessionConfigurationTests {
registerAndRefresh(DataSourceConfiguration.class, CustomMaxInactiveIntervalInSecondsSetterConfiguration.class); registerAndRefresh(DataSourceConfiguration.class, CustomMaxInactiveIntervalInSecondsSetterConfiguration.class);
JdbcIndexedSessionRepository repository = this.context.getBean(JdbcIndexedSessionRepository.class); JdbcIndexedSessionRepository repository = this.context.getBean(JdbcIndexedSessionRepository.class);
assertThat(repository).isNotNull(); assertThat(repository).extracting("defaultMaxInactiveInterval")
assertThat(ReflectionTestUtils.getField(repository, "defaultMaxInactiveInterval")) .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
.isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS);
} }
@Test @Test
@@ -297,8 +297,8 @@ class JdbcHttpSessionConfigurationTests {
void sessionRepositoryCustomizer() { void sessionRepositoryCustomizer() {
registerAndRefresh(DataSourceConfiguration.class, SessionRepositoryCustomizerConfiguration.class); registerAndRefresh(DataSourceConfiguration.class, SessionRepositoryCustomizerConfiguration.class);
JdbcIndexedSessionRepository sessionRepository = this.context.getBean(JdbcIndexedSessionRepository.class); JdbcIndexedSessionRepository sessionRepository = this.context.getBean(JdbcIndexedSessionRepository.class);
assertThat(sessionRepository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", assertThat(sessionRepository).extracting("defaultMaxInactiveInterval")
MAX_INACTIVE_INTERVAL_IN_SECONDS); .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
} }
@Test @Test
@@ -315,7 +315,7 @@ class JdbcHttpSessionConfigurationTests {
void importConfigAndCustomize() { void importConfigAndCustomize() {
registerAndRefresh(DataSourceConfiguration.class, ImportConfigAndCustomizeConfiguration.class); registerAndRefresh(DataSourceConfiguration.class, ImportConfigAndCustomizeConfiguration.class);
JdbcIndexedSessionRepository sessionRepository = this.context.getBean(JdbcIndexedSessionRepository.class); JdbcIndexedSessionRepository sessionRepository = this.context.getBean(JdbcIndexedSessionRepository.class);
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(0); assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ZERO);
} }
private void registerAndRefresh(Class<?>... annotatedClasses) { private void registerAndRefresh(Class<?>... annotatedClasses) {
@@ -552,14 +552,14 @@ class JdbcHttpSessionConfigurationTests {
@Bean @Bean
@Order(0) @Order(0)
SessionRepositoryCustomizer<JdbcIndexedSessionRepository> sessionRepositoryCustomizerOne() { SessionRepositoryCustomizer<JdbcIndexedSessionRepository> sessionRepositoryCustomizerOne() {
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
} }
@Bean @Bean
@Order(1) @Order(1)
SessionRepositoryCustomizer<JdbcIndexedSessionRepository> sessionRepositoryCustomizerTwo() { SessionRepositoryCustomizer<JdbcIndexedSessionRepository> sessionRepositoryCustomizerTwo() {
return (sessionRepository) -> sessionRepository return (sessionRepository) -> sessionRepository
.setDefaultMaxInactiveInterval(MAX_INACTIVE_INTERVAL_IN_SECONDS); .setDefaultMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
} }
} }
@@ -570,7 +570,7 @@ class JdbcHttpSessionConfigurationTests {
@Bean @Bean
SessionRepositoryCustomizer<JdbcIndexedSessionRepository> sessionRepositoryCustomizer() { SessionRepositoryCustomizer<JdbcIndexedSessionRepository> sessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
} }
} }