diff --git a/spring-session-core/src/main/java/org/springframework/session/MapSession.java b/spring-session-core/src/main/java/org/springframework/session/MapSession.java index 485883f1..bd241357 100644 --- a/spring-session-core/src/main/java/org/springframework/session/MapSession.java +++ b/spring-session-core/src/main/java/org/springframework/session/MapSession.java @@ -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"); * 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 { /** - * Default {@link #setMaxInactiveInterval(Duration)} (30 minutes). + * Default {@link #setMaxInactiveInterval(Duration)} (30 minutes) in seconds. */ 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 final String originalId; @@ -66,7 +72,7 @@ public final class MapSession implements Session, Serializable { /** * 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. diff --git a/spring-session-core/src/main/java/org/springframework/session/MapSessionRepository.java b/spring-session-core/src/main/java/org/springframework/session/MapSessionRepository.java index 373c0182..bff80f36 100644 --- a/spring-session-core/src/main/java/org/springframework/session/MapSessionRepository.java +++ b/spring-session-core/src/main/java/org/springframework/session/MapSessionRepository.java @@ -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"); * 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.SessionExpiredEvent; +import org.springframework.util.Assert; /** * 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 { - /** - * If non-null, this value is used to override - * {@link Session#setMaxInactiveInterval(Duration)}. - */ - private Integer defaultMaxInactiveInterval; + private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS); private final Map sessions; @@ -59,12 +56,13 @@ public class MapSessionRepository implements SessionRepository { } /** - * If non-null, this value is used to override - * {@link Session#setMaxInactiveInterval(Duration)}. - * @param defaultMaxInactiveInterval the number of seconds that the {@link Session} - * should be kept alive between client requests. + * 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(int defaultMaxInactiveInterval) { + public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) { + Assert.notNull(defaultMaxInactiveInterval, "defaultMaxInactiveInterval must not be null"); this.defaultMaxInactiveInterval = defaultMaxInactiveInterval; } @@ -97,9 +95,7 @@ public class MapSessionRepository implements SessionRepository { @Override public MapSession createSession() { MapSession result = new MapSession(); - if (this.defaultMaxInactiveInterval != null) { - result.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval)); - } + result.setMaxInactiveInterval(this.defaultMaxInactiveInterval); return result; } diff --git a/spring-session-core/src/main/java/org/springframework/session/ReactiveMapSessionRepository.java b/spring-session-core/src/main/java/org/springframework/session/ReactiveMapSessionRepository.java index ed3d843f..3bd1cb03 100644 --- a/spring-session-core/src/main/java/org/springframework/session/ReactiveMapSessionRepository.java +++ b/spring-session-core/src/main/java/org/springframework/session/ReactiveMapSessionRepository.java @@ -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"); * 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.SessionExpiredEvent; +import org.springframework.util.Assert; /** * 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 { - /** - * If non-null, this value is used to override - * {@link Session#setMaxInactiveInterval(Duration)}. - */ - private Integer defaultMaxInactiveInterval; + private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS); private final Map sessions; @@ -61,12 +58,13 @@ public class ReactiveMapSessionRepository implements ReactiveSessionRepository createSession() { return Mono.defer(() -> { MapSession result = new MapSession(); - if (this.defaultMaxInactiveInterval != null) { - result.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval)); - } + result.setMaxInactiveInterval(this.defaultMaxInactiveInterval); return Mono.just(result); }); } diff --git a/spring-session-core/src/test/java/org/springframework/session/MapSessionRepositoryTests.java b/spring-session-core/src/test/java/org/springframework/session/MapSessionRepositoryTests.java index ede43f20..208e26e5 100644 --- a/spring-session-core/src/test/java/org/springframework/session/MapSessionRepositoryTests.java +++ b/spring-session-core/src/test/java/org/springframework/session/MapSessionRepositoryTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -60,8 +60,8 @@ class MapSessionRepositoryTests { @Test void createSessionCustomDefaultExpiration() { - final Duration expectedMaxInterval = new MapSession().getMaxInactiveInterval().plusSeconds(10); - this.repository.setDefaultMaxInactiveInterval((int) expectedMaxInterval.getSeconds()); + Duration expectedMaxInterval = new MapSession().getMaxInactiveInterval().plusSeconds(10); + this.repository.setDefaultMaxInactiveInterval(expectedMaxInterval); Session session = this.repository.createSession(); diff --git a/spring-session-core/src/test/java/org/springframework/session/ReactiveMapSessionRepositoryTests.java b/spring-session-core/src/test/java/org/springframework/session/ReactiveMapSessionRepositoryTests.java index 23044fe2..ee1478f2 100644 --- a/spring-session-core/src/test/java/org/springframework/session/ReactiveMapSessionRepositoryTests.java +++ b/spring-session-core/src/test/java/org/springframework/session/ReactiveMapSessionRepositoryTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -103,8 +103,8 @@ class ReactiveMapSessionRepositoryTests { @Test void createSessionWhenCustomMaxInactiveIntervalThenCustomMaxInactiveInterval() { - final Duration expectedMaxInterval = new MapSession().getMaxInactiveInterval().plusSeconds(10); - this.repository.setDefaultMaxInactiveInterval((int) expectedMaxInterval.getSeconds()); + Duration expectedMaxInterval = new MapSession().getMaxInactiveInterval().plusSeconds(10); + this.repository.setDefaultMaxInactiveInterval(expectedMaxInterval); Session session = this.repository.createSession().block(); diff --git a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoIndexedSessionRepository.java b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoIndexedSessionRepository.java index 7ecc699a..8a7b4ec4 100644 --- a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoIndexedSessionRepository.java +++ b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoIndexedSessionRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.index.IndexOperations; import org.springframework.lang.Nullable; import org.springframework.session.FindByIndexNameSessionRepository; +import org.springframework.session.MapSession; import org.springframework.session.events.SessionCreatedEvent; import org.springframework.session.events.SessionDeletedEvent; import org.springframework.session.events.SessionExpiredEvent; @@ -46,6 +47,7 @@ import org.springframework.session.events.SessionExpiredEvent; * * @author Jakub Kubrynski * @author Greg Turnquist + * @author Vedran Pavic * @since 2.2.0 */ public class MongoIndexedSessionRepository @@ -53,8 +55,11 @@ public class MongoIndexedSessionRepository /** * The default time period in seconds in which a session will expire. + * @deprecated since 3.0.0 in favor of + * {@link MapSession#DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS} */ - public static final int DEFAULT_INACTIVE_INTERVAL = 1800; + @Deprecated + public static final int DEFAULT_INACTIVE_INTERVAL = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS; /** * the default collection name for storing session. @@ -65,12 +70,12 @@ public class MongoIndexedSessionRepository private final MongoOperations mongoOperations; - private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL; + private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS); private String collectionName = DEFAULT_COLLECTION_NAME; private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter( - Duration.ofSeconds(this.maxInactiveIntervalInSeconds)); + this.defaultMaxInactiveInterval); private ApplicationEventPublisher eventPublisher; @@ -83,9 +88,7 @@ public class MongoIndexedSessionRepository MongoSession session = new MongoSession(); - if (this.maxInactiveIntervalInSeconds != null) { - session.setMaxInactiveInterval(Duration.ofSeconds(this.maxInactiveIntervalInSeconds)); - } + session.setMaxInactiveInterval(this.defaultMaxInactiveInterval); publishEvent(new SessionCreatedEvent(this, session)); @@ -178,8 +181,16 @@ public class MongoIndexedSessionRepository } } - public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) { - this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; + /** + * Set the maximum inactive interval in seconds between requests before newly created + * sessions will be invalidated. A negative time indicates that the session will never + * time out. The default is 30 minutes. + * @param defaultMaxInactiveInterval the default maxInactiveInterval + */ + public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) { + org.springframework.util.Assert.notNull(defaultMaxInactiveInterval, + "defaultMaxInactiveInterval must not be null"); + this.defaultMaxInactiveInterval = defaultMaxInactiveInterval; } public void setCollectionName(final String collectionName) { diff --git a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoSession.java b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoSession.java index 2741f383..47a506b1 100644 --- a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoSession.java +++ b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoSession.java @@ -27,6 +27,7 @@ import java.util.UUID; import java.util.stream.Collectors; import org.springframework.lang.Nullable; +import org.springframework.session.MapSession; import org.springframework.session.Session; /** @@ -66,7 +67,7 @@ public class MongoSession implements Session { private Map attrs = new HashMap<>(); public MongoSession() { - this(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL); + this(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS); } public MongoSession(long maxInactiveIntervalInSeconds) { diff --git a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/ReactiveMongoSessionRepository.java b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/ReactiveMongoSessionRepository.java index 13382745..fc6bf62f 100644 --- a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/ReactiveMongoSessionRepository.java +++ b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/ReactiveMongoSessionRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,14 +32,17 @@ import org.springframework.data.mongodb.core.ReactiveMongoOperations; import org.springframework.data.mongodb.core.index.IndexOperations; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; +import org.springframework.session.MapSession; import org.springframework.session.ReactiveSessionRepository; import org.springframework.session.events.SessionCreatedEvent; import org.springframework.session.events.SessionDeletedEvent; +import org.springframework.util.Assert; /** * A {@link ReactiveSessionRepository} implementation that uses Spring Data MongoDB. * * @author Greg Turnquist + * @author Vedran Pavic * @since 2.2.0 */ public class ReactiveMongoSessionRepository @@ -47,8 +50,11 @@ public class ReactiveMongoSessionRepository /** * The default time period in seconds in which a session will expire. + * @deprecated since 3.0.0 in favor of + * {@link MapSession#DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS} */ - public static final int DEFAULT_INACTIVE_INTERVAL = 1800; + @Deprecated + public static final int DEFAULT_INACTIVE_INTERVAL = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS; /** * The default collection name for storing session. @@ -59,12 +65,12 @@ public class ReactiveMongoSessionRepository private final ReactiveMongoOperations mongoOperations; - private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL; + private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS); private String collectionName = DEFAULT_COLLECTION_NAME; private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter( - Duration.ofSeconds(this.maxInactiveIntervalInSeconds)); + this.defaultMaxInactiveInterval); private MongoOperations blockingMongoOperations; @@ -88,7 +94,7 @@ public class ReactiveMongoSessionRepository @Override public Mono createSession() { - return Mono.justOrEmpty(this.maxInactiveIntervalInSeconds) // + return Mono.justOrEmpty(this.defaultMaxInactiveInterval.toSeconds()) // .map(MongoSession::new) // .doOnNext((mongoSession) -> publishEvent(new SessionCreatedEvent(this, mongoSession))) // .switchIfEmpty(Mono.just(new MongoSession())); @@ -170,12 +176,15 @@ public class ReactiveMongoSessionRepository } } - public Integer getMaxInactiveIntervalInSeconds() { - return this.maxInactiveIntervalInSeconds; - } - - public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) { - this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; + /** + * Set the maximum inactive interval in seconds between requests before newly created + * sessions will be invalidated. A negative time indicates that the session will never + * time out. The default is 30 minutes. + * @param defaultMaxInactiveInterval the default maxInactiveInterval + */ + public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) { + Assert.notNull(defaultMaxInactiveInterval, "defaultMaxInactiveInterval must not be null"); + this.defaultMaxInactiveInterval = defaultMaxInactiveInterval; } public String getCollectionName() { diff --git a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/http/EnableMongoHttpSession.java b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/http/EnableMongoHttpSession.java index d337f4f3..73a6d640 100644 --- a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/http/EnableMongoHttpSession.java +++ b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/http/EnableMongoHttpSession.java @@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.context.annotation.Import; +import org.springframework.session.MapSession; import org.springframework.session.data.mongo.MongoIndexedSessionRepository; /** @@ -58,7 +59,7 @@ public @interface EnableMongoHttpSession { * The maximum time a session will be kept if it is inactive. * @return default max inactive interval in seconds */ - int maxInactiveIntervalInSeconds() default MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL; + int maxInactiveIntervalInSeconds() default MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS; /** * The collection name to use. diff --git a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfiguration.java b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfiguration.java index 74cff9dc..eb1014d8 100644 --- a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfiguration.java +++ b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,7 @@ import org.springframework.core.serializer.support.SerializingConverter; import org.springframework.core.type.AnnotationMetadata; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.session.IndexResolver; +import org.springframework.session.MapSession; import org.springframework.session.config.SessionRepositoryCustomizer; import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration; import org.springframework.session.data.mongo.AbstractMongoSessionConverter; @@ -56,7 +57,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio private AbstractMongoSessionConverter mongoSessionConverter; - private Integer maxInactiveIntervalInSeconds; + private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL; private String collectionName; @@ -72,7 +73,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio public MongoIndexedSessionRepository mongoSessionRepository(MongoOperations mongoOperations) { MongoIndexedSessionRepository repository = new MongoIndexedSessionRepository(mongoOperations); - repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds); + repository.setDefaultMaxInactiveInterval(this.maxInactiveInterval); if (this.mongoSessionConverter != null) { repository.setMongoSessionConverter(this.mongoSessionConverter); @@ -84,7 +85,7 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio else { JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(), new DeserializingConverter(this.classLoader), - Duration.ofSeconds(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL)); + Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS)); if (this.indexResolver != null) { mongoSessionConverter.setIndexResolver(this.indexResolver); @@ -107,8 +108,13 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio this.collectionName = collectionName; } + public void setMaxInactiveInterval(Duration maxInactiveInterval) { + this.maxInactiveInterval = maxInactiveInterval; + } + + @Deprecated public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) { - this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; + setMaxInactiveInterval(Duration.ofSeconds(maxInactiveIntervalInSeconds)); } public void setImportMetadata(AnnotationMetadata importMetadata) { @@ -117,10 +123,8 @@ public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguratio .fromMap(importMetadata.getAnnotationAttributes(EnableMongoHttpSession.class.getName())); if (attributes != null) { - this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds"); - } - else { - this.maxInactiveIntervalInSeconds = MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL; + this.maxInactiveInterval = Duration + .ofSeconds(attributes.getNumber("maxInactiveIntervalInSeconds")); } String collectionNameValue = (attributes != null) ? attributes.getString("collectionName") : ""; diff --git a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/reactive/EnableMongoWebSession.java b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/reactive/EnableMongoWebSession.java index ae567bb7..87704475 100644 --- a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/reactive/EnableMongoWebSession.java +++ b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/reactive/EnableMongoWebSession.java @@ -21,6 +21,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.Target; import org.springframework.context.annotation.Import; +import org.springframework.session.MapSession; import org.springframework.session.data.mongo.ReactiveMongoSessionRepository; /** @@ -44,7 +45,7 @@ import org.springframework.session.data.mongo.ReactiveMongoSessionRepository; * * * @author Greg Turnquist - * @author Vedran Pavić + * @author Vedran Pavic * @since 2.0 */ @Retention(java.lang.annotation.RetentionPolicy.RUNTIME) @@ -57,7 +58,7 @@ public @interface EnableMongoWebSession { * The maximum time a session will be kept if it is inactive. * @return default max inactive interval in seconds */ - int maxInactiveIntervalInSeconds() default ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL; + int maxInactiveIntervalInSeconds() default MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS; /** * The collection name to use. diff --git a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/reactive/ReactiveMongoWebSessionConfiguration.java b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/reactive/ReactiveMongoWebSessionConfiguration.java index c6acdd87..f15fdb64 100644 --- a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/reactive/ReactiveMongoWebSessionConfiguration.java +++ b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/reactive/ReactiveMongoWebSessionConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 the original author or authors. + * Copyright 2016-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ import org.springframework.core.type.AnnotationMetadata; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.ReactiveMongoOperations; import org.springframework.session.IndexResolver; +import org.springframework.session.MapSession; import org.springframework.session.config.ReactiveSessionRepositoryCustomizer; import org.springframework.session.config.annotation.web.server.SpringWebSessionConfiguration; import org.springframework.session.data.mongo.AbstractMongoSessionConverter; @@ -56,7 +57,7 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig private AbstractMongoSessionConverter mongoSessionConverter; - private Integer maxInactiveIntervalInSeconds; + private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL; private String collectionName; @@ -87,7 +88,7 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig else { JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(), new DeserializingConverter(this.classLoader), - Duration.ofSeconds(ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL)); + Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS)); if (this.indexResolver != null) { mongoSessionConverter.setIndexResolver(this.indexResolver); @@ -96,9 +97,7 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig repository.setMongoSessionConverter(mongoSessionConverter); } - if (this.maxInactiveIntervalInSeconds != null) { - repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds); - } + repository.setDefaultMaxInactiveInterval(this.maxInactiveInterval); if (this.collectionName != null) { repository.setCollectionName(this.collectionName); @@ -126,10 +125,8 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig .fromMap(importMetadata.getAnnotationAttributes(EnableMongoWebSession.class.getName())); if (attributes != null) { - this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds"); - } - else { - this.maxInactiveIntervalInSeconds = ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL; + this.maxInactiveInterval = Duration + .ofSeconds(attributes.getNumber("maxInactiveIntervalInSeconds")); } String collectionNameValue = (attributes != null) ? attributes.getString("collectionName") : ""; @@ -149,12 +146,17 @@ public class ReactiveMongoWebSessionConfiguration extends SpringWebSessionConfig this.embeddedValueResolver = embeddedValueResolver; } - public Integer getMaxInactiveIntervalInSeconds() { - return this.maxInactiveIntervalInSeconds; + public Duration getMaxInactiveInterval() { + return this.maxInactiveInterval; } + public void setMaxInactiveInterval(Duration maxInactiveInterval) { + this.maxInactiveInterval = maxInactiveInterval; + } + + @Deprecated public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) { - this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; + setMaxInactiveInterval(Duration.ofSeconds(maxInactiveIntervalInSeconds)); } public String getCollectionName() { diff --git a/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/MongoIndexedSessionRepositoryTest.java b/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/MongoIndexedSessionRepositoryTest.java index 0061744a..c33ce79d 100644 --- a/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/MongoIndexedSessionRepositoryTest.java +++ b/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/MongoIndexedSessionRepositoryTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,7 @@ import org.springframework.core.convert.TypeDescriptor; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.query.Query; import org.springframework.session.FindByIndexNameSessionRepository; +import org.springframework.session.MapSession; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; @@ -76,20 +77,7 @@ public class MongoIndexedSessionRepositoryTest { // then assertThat(session.getId()).isNotEmpty(); assertThat(session.getMaxInactiveInterval().getSeconds()) - .isEqualTo(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL); - } - - @Test - void shouldCreateSessionWhenMaxInactiveIntervalNotDefined() { - - // when - this.repository.setMaxInactiveIntervalInSeconds(null); - MongoSession session = this.repository.createSession(); - - // then - assertThat(session.getId()).isNotEmpty(); - assertThat(session.getMaxInactiveInterval().getSeconds()) - .isEqualTo(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL); + .isEqualTo(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS); } @Test @@ -164,8 +152,7 @@ public class MongoIndexedSessionRepositoryTest { Document sessionDocument = new Document(); sessionDocument.put("id", sessionId); - MongoSession mongoSession = new MongoSession(sessionId, - MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL); + MongoSession mongoSession = new MongoSession(sessionId, MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS); given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class), TypeDescriptor.valueOf(MongoSession.class))).willReturn(mongoSession); diff --git a/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/ReactiveMongoSessionRepositoryTest.java b/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/ReactiveMongoSessionRepositoryTest.java index 3dbfb6c2..ed6966a7 100644 --- a/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/ReactiveMongoSessionRepositoryTest.java +++ b/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/ReactiveMongoSessionRepositoryTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.session.data.mongo; +import java.time.Duration; import java.util.UUID; import com.mongodb.BasicDBObject; @@ -35,6 +36,7 @@ import org.springframework.core.convert.TypeDescriptor; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.ReactiveMongoOperations; import org.springframework.data.mongodb.core.index.IndexOperations; +import org.springframework.session.MapSession; import org.springframework.session.events.SessionDeletedEvent; import static org.assertj.core.api.Assertions.assertThat; @@ -84,26 +86,8 @@ public class ReactiveMongoSessionRepositoryTest { .as(StepVerifier::create) // .expectNextMatches((mongoSession) -> { assertThat(mongoSession.getId()).isNotEmpty(); - assertThat(mongoSession.getMaxInactiveInterval().getSeconds()) - .isEqualTo(ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL); - return true; - }) // - .verifyComplete(); - } - - @Test - void shouldCreateSessionWhenMaxInactiveIntervalNotDefined() { - - // when - this.repository.setMaxInactiveIntervalInSeconds(null); - - // then - this.repository.createSession() // - .as(StepVerifier::create) // - .expectNextMatches((mongoSession) -> { - assertThat(mongoSession.getId()).isNotEmpty(); - assertThat(mongoSession.getMaxInactiveInterval().getSeconds()) - .isEqualTo(ReactiveMongoSessionRepository.DEFAULT_INACTIVE_INTERVAL); + assertThat(mongoSession.getMaxInactiveInterval()) + .isEqualTo(Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS)); return true; }) // .verifyComplete(); diff --git a/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTest.java b/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTest.java index 5837bb0a..3f807fe5 100644 --- a/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTest.java +++ b/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTest.java @@ -17,6 +17,7 @@ package org.springframework.session.data.mongo.config.annotation.web.http; import java.net.UnknownHostException; +import java.time.Duration; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Order; @@ -112,9 +113,8 @@ public class MongoHttpSessionConfigurationTest { MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class); - assertThat(repository).isNotNull(); - assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds")) - .isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS); + assertThat(repository).extracting("defaultMaxInactiveInterval") + .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } @Test @@ -124,9 +124,8 @@ public class MongoHttpSessionConfigurationTest { MongoIndexedSessionRepository repository = this.context.getBean(MongoIndexedSessionRepository.class); - assertThat(repository).isNotNull(); - assertThat(ReflectionTestUtils.getField(repository, "maxInactiveIntervalInSeconds")) - .isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS); + assertThat(repository).extracting("defaultMaxInactiveInterval") + .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } @Test @@ -161,7 +160,7 @@ public class MongoHttpSessionConfigurationTest { MongoIndexedSessionRepository sessionRepository = this.context.getBean(MongoIndexedSessionRepository.class); - assertThat(sessionRepository).hasFieldOrPropertyWithValue("maxInactiveIntervalInSeconds", 10000); + assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ofSeconds(10000)); } @Test @@ -198,7 +197,7 @@ public class MongoHttpSessionConfigurationTest { void importConfigAndCustomize() { registerAndRefresh(ImportConfigAndCustomizeConfiguration.class); MongoIndexedSessionRepository sessionRepository = this.context.getBean(MongoIndexedSessionRepository.class); - assertThat(sessionRepository).extracting("maxInactiveIntervalInSeconds").isEqualTo(0); + assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ZERO); } private void registerAndRefresh(Class... annotatedClasses) { @@ -266,7 +265,7 @@ public class MongoHttpSessionConfigurationTest { static class CustomMaxInactiveIntervalInSecondsSetConfiguration extends MongoHttpSessionConfiguration { CustomMaxInactiveIntervalInSecondsSetConfiguration() { - setMaxInactiveIntervalInSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS); + setMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } } @@ -300,13 +299,13 @@ public class MongoHttpSessionConfigurationTest { @Bean @Order(0) SessionRepositoryCustomizer sessionRepositoryCustomizerOne() { - return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO); } @Bean @Order(1) SessionRepositoryCustomizer sessionRepositoryCustomizerTwo() { - return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(10000); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ofSeconds(10000)); } } @@ -346,7 +345,7 @@ public class MongoHttpSessionConfigurationTest { @Bean SessionRepositoryCustomizer sessionRepositoryCustomizer() { - return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO); } } diff --git a/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/config/annotation/web/reactive/ReactiveMongoWebSessionConfigurationTest.java b/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/config/annotation/web/reactive/ReactiveMongoWebSessionConfigurationTest.java index 8aebf9cf..e20d0805 100644 --- a/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/config/annotation/web/reactive/ReactiveMongoWebSessionConfigurationTest.java +++ b/spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/config/annotation/web/reactive/ReactiveMongoWebSessionConfigurationTest.java @@ -17,6 +17,7 @@ package org.springframework.session.data.mongo.config.annotation.web.reactive; import java.lang.reflect.Field; +import java.time.Duration; import java.util.Collections; import org.junit.jupiter.api.AfterEach; @@ -56,7 +57,7 @@ import static org.mockito.BDDMockito.verify; * Verify various configurations through {@link EnableSpringWebSession}. * * @author Greg Turnquist - * @author Vedran Pavić + * @author Vedran Pavic */ public class ReactiveMongoWebSessionConfigurationTest { @@ -128,7 +129,7 @@ public class ReactiveMongoWebSessionConfigurationTest { } @Test - void overridingIntervalAndCollectionNameThroughAnnotationShouldWork() throws IllegalAccessException { + void overridingIntervalAndCollectionNameThroughAnnotationShouldWork() { this.context = new AnnotationConfigApplicationContext(); this.context.register(OverrideMongoParametersConfig.class); @@ -136,17 +137,8 @@ public class ReactiveMongoWebSessionConfigurationTest { ReactiveMongoSessionRepository repository = this.context.getBean(ReactiveMongoSessionRepository.class); - Field inactiveField = ReflectionUtils.findField(ReactiveMongoSessionRepository.class, - "maxInactiveIntervalInSeconds"); - ReflectionUtils.makeAccessible(inactiveField); - Integer inactiveSeconds = (Integer) inactiveField.get(repository); - - Field collectionNameField = ReflectionUtils.findField(ReactiveMongoSessionRepository.class, "collectionName"); - ReflectionUtils.makeAccessible(collectionNameField); - String collectionName = (String) collectionNameField.get(repository); - - assertThat(inactiveSeconds).isEqualTo(123); - assertThat(collectionName).isEqualTo("test-case"); + assertThat(repository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ofSeconds(123)); + assertThat(repository).extracting("collectionName").isEqualTo("test-case"); } @Test @@ -174,7 +166,7 @@ public class ReactiveMongoWebSessionConfigurationTest { ReactiveMongoSessionRepository repository = this.context.getBean(ReactiveMongoSessionRepository.class); assertThat(repository.getCollectionName()).isEqualTo("custom-collection"); - assertThat(repository.getMaxInactiveIntervalInSeconds()).isEqualTo(123); + assertThat(repository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ofSeconds(123)); } @Test @@ -186,7 +178,7 @@ public class ReactiveMongoWebSessionConfigurationTest { ReactiveMongoSessionRepository repository = this.context.getBean(ReactiveMongoSessionRepository.class); - assertThat(repository).hasFieldOrPropertyWithValue("maxInactiveIntervalInSeconds", 10000); + assertThat(repository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ofSeconds(10000)); } @Test @@ -227,7 +219,7 @@ public class ReactiveMongoWebSessionConfigurationTest { this.context.register(ImportConfigAndCustomizeConfiguration.class); this.context.refresh(); ReactiveMongoSessionRepository sessionRepository = this.context.getBean(ReactiveMongoSessionRepository.class); - assertThat(sessionRepository).extracting("maxInactiveIntervalInSeconds").isEqualTo(0); + assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ZERO); } /** @@ -331,7 +323,7 @@ public class ReactiveMongoWebSessionConfigurationTest { CustomizedReactiveConfiguration() { this.setCollectionName("custom-collection"); - this.setMaxInactiveIntervalInSeconds(123); + this.setMaxInactiveInterval(Duration.ofSeconds(123)); } @Bean @@ -353,13 +345,13 @@ public class ReactiveMongoWebSessionConfigurationTest { @Bean @Order(0) ReactiveSessionRepositoryCustomizer sessionRepositoryCustomizerOne() { - return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO); } @Bean @Order(1) ReactiveSessionRepositoryCustomizer sessionRepositoryCustomizerTwo() { - return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(10000); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ofSeconds(10000)); } } @@ -414,7 +406,7 @@ public class ReactiveMongoWebSessionConfigurationTest { @Bean ReactiveSessionRepositoryCustomizer sessionRepositoryCustomizer() { - return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO); } } diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisSessionRepository.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisSessionRepository.java index a987359e..f91015ca 100644 --- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisSessionRepository.java +++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisSessionRepository.java @@ -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"); * you may not use this file except in compliance with the License. @@ -55,11 +55,7 @@ public class ReactiveRedisSessionRepository */ private String namespace = DEFAULT_NAMESPACE + ":"; - /** - * If non-null, this value is used to override the default value for - * {@link RedisSession#setMaxInactiveInterval(Duration)}. - */ - private Integer defaultMaxInactiveInterval; + private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS); 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 - * timeout. The default is 1800 (30 minutes). - * @param defaultMaxInactiveInterval the number of seconds that the {@link Session} - * should be kept alive between client requests. + * time out. The default is 30 minutes. + * @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; } @@ -110,9 +106,7 @@ public class ReactiveRedisSessionRepository public Mono createSession() { return Mono.defer(() -> { MapSession cached = new MapSession(); - if (this.defaultMaxInactiveInterval != null) { - cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval)); - } + cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval); RedisSession session = new RedisSession(cached, true); return Mono.just(session); }); diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java index 4b4e9b83..f0a00b25 100644 --- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java +++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java @@ -307,11 +307,7 @@ public class RedisIndexedSessionRepository private ApplicationEventPublisher eventPublisher = (event) -> { }; - /** - * If non-null, this value is used to override the default value for - * {@link RedisSession#setMaxInactiveInterval(Duration)}. - */ - private Integer defaultMaxInactiveInterval; + private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS); private IndexResolver 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 - * timeout. The default is 1800 (30 minutes). - * @param defaultMaxInactiveInterval the number of seconds that the {@link Session} - * should be kept alive between client requests. + * time out. The default is 30 minutes. + * @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; } @@ -560,9 +556,7 @@ public class RedisIndexedSessionRepository @Override public RedisSession createSession() { MapSession cached = new MapSession(); - if (this.defaultMaxInactiveInterval != null) { - cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval)); - } + cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval); RedisSession session = new RedisSession(cached, true); session.flushImmediateIfNecessary(); return session; diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionRepository.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionRepository.java index 4ef4b229..d2408a70 100644 --- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionRepository.java +++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionRepository.java @@ -18,7 +18,6 @@ package org.springframework.session.data.redis; import java.time.Duration; import java.time.Instant; -import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -68,7 +67,9 @@ public class RedisSessionRepository implements SessionRepository(this.delta)); RedisSessionRepository.this.sessionRedisOperations.expireAt(key, - Date.from(Instant.ofEpochMilli(getLastAccessedTime().toEpochMilli()) - .plusSeconds(getMaxInactiveInterval().getSeconds()))); + Instant.ofEpochMilli(getLastAccessedTime().toEpochMilli()) + .plusSeconds(getMaxInactiveInterval().getSeconds())); this.delta.clear(); } diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/AbstractRedisHttpSessionConfiguration.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/AbstractRedisHttpSessionConfiguration.java index 34a953c3..b2b495c1 100644 --- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/AbstractRedisHttpSessionConfiguration.java +++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/AbstractRedisHttpSessionConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.session.data.redis.config.annotation.web.http; +import java.time.Duration; import java.util.List; import java.util.stream.Collectors; @@ -53,7 +54,7 @@ import org.springframework.util.Assert; public abstract class AbstractRedisHttpSessionConfiguration> 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; @@ -71,12 +72,17 @@ public abstract class AbstractRedisHttpSessionConfiguration redisTemplate = createRedisTemplate(); RedisSessionRepository sessionRepository = new RedisSessionRepository(redisTemplate); - sessionRepository.setDefaultMaxInactiveInterval(Duration.ofSeconds(getMaxInactiveIntervalInSeconds())); + sessionRepository.setDefaultMaxInactiveInterval(getMaxInactiveInterval()); if (StringUtils.hasText(getRedisNamespace())) { sessionRepository.setRedisKeyNamespace(getRedisNamespace()); } @@ -78,7 +78,7 @@ public class RedisHttpSessionConfiguration extends AbstractRedisHttpSessionConfi if (attributes == null) { return; } - setMaxInactiveIntervalInSeconds(attributes.getNumber("maxInactiveIntervalInSeconds")); + setMaxInactiveInterval(Duration.ofSeconds(attributes.getNumber("maxInactiveIntervalInSeconds"))); String redisNamespaceValue = attributes.getString("redisNamespace"); if (StringUtils.hasText(redisNamespaceValue)) { setRedisNamespace(this.embeddedValueResolver.resolveStringValue(redisNamespaceValue)); diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/RedisIndexedHttpSessionConfiguration.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/RedisIndexedHttpSessionConfiguration.java index e8a65bca..c6c4243d 100644 --- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/RedisIndexedHttpSessionConfiguration.java +++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/http/RedisIndexedHttpSessionConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.session.data.redis.config.annotation.web.http; +import java.time.Duration; import java.util.Arrays; import java.util.Collections; import java.util.Map; @@ -91,7 +92,7 @@ public class RedisIndexedHttpSessionConfiguration if (getDefaultRedisSerializer() != null) { sessionRepository.setDefaultSerializer(getDefaultRedisSerializer()); } - sessionRepository.setDefaultMaxInactiveInterval(getMaxInactiveIntervalInSeconds()); + sessionRepository.setDefaultMaxInactiveInterval(getMaxInactiveInterval()); if (StringUtils.hasText(getRedisNamespace())) { sessionRepository.setRedisKeyNamespace(getRedisNamespace()); } @@ -178,7 +179,7 @@ public class RedisIndexedHttpSessionConfiguration if (attributes == null) { return; } - setMaxInactiveIntervalInSeconds(attributes.getNumber("maxInactiveIntervalInSeconds")); + setMaxInactiveInterval(Duration.ofSeconds(attributes.getNumber("maxInactiveIntervalInSeconds"))); String redisNamespaceValue = attributes.getString("redisNamespace"); if (StringUtils.hasText(redisNamespaceValue)) { setRedisNamespace(this.embeddedValueResolver.resolveStringValue(redisNamespaceValue)); diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfiguration.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfiguration.java index a1e0a831..e9ba50a6 100644 --- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfiguration.java +++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.session.data.redis.config.annotation.web.server; +import java.time.Duration; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -59,7 +60,7 @@ import org.springframework.web.server.session.WebSessionManager; public class RedisWebSessionConfiguration extends SpringWebSessionConfiguration 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; @@ -79,7 +80,7 @@ public class RedisWebSessionConfiguration extends SpringWebSessionConfiguration public ReactiveRedisSessionRepository sessionRepository() { ReactiveRedisTemplate reactiveRedisTemplate = createReactiveRedisTemplate(); ReactiveRedisSessionRepository sessionRepository = new ReactiveRedisSessionRepository(reactiveRedisTemplate); - sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds); + sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveInterval); if (StringUtils.hasText(this.redisNamespace)) { sessionRepository.setRedisKeyNamespace(this.redisNamespace); } @@ -89,8 +90,13 @@ public class RedisWebSessionConfiguration extends SpringWebSessionConfiguration return sessionRepository; } + public void setMaxInactiveInterval(Duration maxInactiveInterval) { + this.maxInactiveInterval = maxInactiveInterval; + } + + @Deprecated public void setMaxInactiveIntervalInSeconds(int maxInactiveIntervalInSeconds) { - this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; + setMaxInactiveInterval(Duration.ofSeconds(maxInactiveIntervalInSeconds)); } public void setRedisNamespace(String namespace) { @@ -143,7 +149,7 @@ public class RedisWebSessionConfiguration extends SpringWebSessionConfiguration if (attributes == null) { return; } - this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds"); + this.maxInactiveInterval = Duration.ofSeconds(attributes.getNumber("maxInactiveIntervalInSeconds")); String redisNamespaceValue = attributes.getString("redisNamespace"); if (StringUtils.hasText(redisNamespaceValue)) { this.redisNamespace = this.embeddedValueResolver.resolveStringValue(redisNamespaceValue); diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisSessionRepositoryTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisSessionRepositoryTests.java index efdc81f9..a85b9d36 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisSessionRepositoryTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisSessionRepositoryTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -103,9 +103,10 @@ class ReactiveRedisSessionRepositoryTests { @Test 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 @@ -118,11 +119,11 @@ class ReactiveRedisSessionRepositoryTests { @Test void createSessionCustomMaxInactiveInterval() { - this.repository.setDefaultMaxInactiveInterval(600); + Duration interval = Duration.ofMinutes(10); + this.repository.setDefaultMaxInactiveInterval(interval); StepVerifier.create(this.repository.createSession()) - .consumeNextWith( - (session) -> assertThat(session.getMaxInactiveInterval()).isEqualTo(Duration.ofSeconds(600))) + .consumeNextWith((session) -> assertThat(session.getMaxInactiveInterval()).isEqualTo(interval)) .verifyComplete(); } diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryTests.java index 825f1ca2..c1403d3f 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryTests.java @@ -158,10 +158,10 @@ class RedisIndexedSessionRepositoryTests { @Test void createSessionCustomMaxInactiveInterval() { - int interval = 1; + Duration interval = Duration.ofSeconds(1); this.redisRepository.setDefaultMaxInactiveInterval(interval); Session session = this.redisRepository.createSession(); - assertThat(session.getMaxInactiveInterval()).isEqualTo(Duration.ofSeconds(interval)); + assertThat(session.getMaxInactiveInterval()).isEqualTo(interval); } @Test @@ -663,7 +663,7 @@ class RedisIndexedSessionRepositoryTests { given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations); 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.createSession(); Map delta = getDelta(); diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisSessionRepositoryTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisSessionRepositoryTests.java index 8e5c2a02..0092e217 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisSessionRepositoryTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisSessionRepositoryTests.java @@ -20,7 +20,6 @@ import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Collections; -import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -378,9 +377,9 @@ class RedisSessionRepositoryTests { return "spring:session:sessions:" + sessionId; } - private static Date getExpiry(RedisSession session) { - return Date.from(Instant.ofEpochMilli(session.getLastAccessedTime().toEpochMilli()) - .plusSeconds(session.getMaxInactiveInterval().getSeconds())); + private static Instant getExpiry(RedisSession session) { + return Instant.ofEpochMilli(session.getLastAccessedTime().toEpochMilli()) + .plusSeconds(session.getMaxInactiveInterval().getSeconds()); } private static Map mapOf(Object... objects) { diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/RedisIndexedHttpSessionConfigurationTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/RedisIndexedHttpSessionConfigurationTests.java index 10acac89..f89dc8b4 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/RedisIndexedHttpSessionConfigurationTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/RedisIndexedHttpSessionConfigurationTests.java @@ -16,6 +16,7 @@ package org.springframework.session.data.redis.config.annotation.web.http; +import java.time.Duration; import java.util.Map; import java.util.Properties; @@ -228,15 +229,15 @@ class RedisIndexedHttpSessionConfigurationTests { void sessionRepositoryCustomizer() { registerAndRefresh(RedisConfig.class, SessionRepositoryCustomizerConfiguration.class); RedisIndexedSessionRepository sessionRepository = this.context.getBean(RedisIndexedSessionRepository.class); - assertThat(sessionRepository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", - MAX_INACTIVE_INTERVAL_IN_SECONDS); + assertThat(sessionRepository).extracting("defaultMaxInactiveInterval") + .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } @Test void importConfigAndCustomize() { registerAndRefresh(RedisConfig.class, ImportConfigAndCustomizeConfiguration.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) { @@ -420,14 +421,14 @@ class RedisIndexedHttpSessionConfigurationTests { @Bean @Order(0) SessionRepositoryCustomizer sessionRepositoryCustomizerOne() { - return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO); } @Bean @Order(1) SessionRepositoryCustomizer sessionRepositoryCustomizerTwo() { return (sessionRepository) -> sessionRepository - .setDefaultMaxInactiveInterval(MAX_INACTIVE_INTERVAL_IN_SECONDS); + .setDefaultMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } } @@ -438,7 +439,7 @@ class RedisIndexedHttpSessionConfigurationTests { @Bean SessionRepositoryCustomizer sessionRepositoryCustomizer() { - return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO); } } diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/gh109/Gh109Tests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/gh109/Gh109Tests.java index aba58ee8..5ecc4daf 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/gh109/Gh109Tests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/gh109/Gh109Tests.java @@ -16,6 +16,7 @@ package org.springframework.session.data.redis.config.annotation.web.http.gh109; +import java.time.Duration; import java.util.Properties; import org.junit.jupiter.api.Test; @@ -60,7 +61,7 @@ class Gh109Tests { @Configuration static class Config extends RedisHttpSessionConfiguration { - int sessionTimeout = 100; + Duration sessionTimeout = Duration.ofSeconds(100); /** * override sessionRepository construction to set the custom session-timeout diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfigurationTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfigurationTests.java index edb44a14..091c4c2b 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfigurationTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfigurationTests.java @@ -16,6 +16,8 @@ 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.BeforeEach; import org.junit.jupiter.api.Test; @@ -103,9 +105,8 @@ class RedisWebSessionConfigurationTests { registerAndRefresh(RedisConfig.class, CustomMaxInactiveIntervalConfig.class); ReactiveRedisSessionRepository repository = this.context.getBean(ReactiveRedisSessionRepository.class); - assertThat(repository).isNotNull(); - assertThat(ReflectionTestUtils.getField(repository, "defaultMaxInactiveInterval")) - .isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS); + assertThat(repository).extracting("defaultMaxInactiveInterval") + .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } @Test @@ -222,15 +223,15 @@ class RedisWebSessionConfigurationTests { void sessionRepositoryCustomizer() { registerAndRefresh(RedisConfig.class, SessionRepositoryCustomizerConfiguration.class); ReactiveRedisSessionRepository sessionRepository = this.context.getBean(ReactiveRedisSessionRepository.class); - assertThat(sessionRepository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", - MAX_INACTIVE_INTERVAL_IN_SECONDS); + assertThat(sessionRepository).extracting("defaultMaxInactiveInterval") + .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } @Test void importConfigAndCustomize() { registerAndRefresh(RedisConfig.class, ImportConfigAndCustomizeConfiguration.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) { @@ -377,14 +378,14 @@ class RedisWebSessionConfigurationTests { @Bean @Order(0) ReactiveSessionRepositoryCustomizer sessionRepositoryCustomizerOne() { - return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO); } @Bean @Order(1) ReactiveSessionRepositoryCustomizer sessionRepositoryCustomizerTwo() { return (sessionRepository) -> sessionRepository - .setDefaultMaxInactiveInterval(MAX_INACTIVE_INTERVAL_IN_SECONDS); + .setDefaultMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } } @@ -395,7 +396,7 @@ class RedisWebSessionConfigurationTests { @Bean ReactiveSessionRepositoryCustomizer sessionRepositoryCustomizer() { - return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO); } } diff --git a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java index f120eb41..851197d3 100644 --- a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java +++ b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java @@ -137,11 +137,7 @@ public class HazelcastIndexedSessionRepository private ApplicationEventPublisher eventPublisher = (event) -> { }; - /** - * If non-null, this value is used to override - * {@link MapSession#setMaxInactiveInterval(Duration)}. - */ - private Integer defaultMaxInactiveInterval; + private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS); private IndexResolver indexResolver = new DelegatingIndexResolver<>(new PrincipalNameIndexResolver<>()); @@ -190,10 +186,11 @@ public class HazelcastIndexedSessionRepository /** * 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 - * timeout. The default is 1800 (30 minutes). - * @param defaultMaxInactiveInterval the maximum inactive interval in seconds + * time out. The default is 30 minutes. + * @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; } @@ -236,9 +233,7 @@ public class HazelcastIndexedSessionRepository @Override public HazelcastSession createSession() { MapSession cached = new MapSession(); - if (this.defaultMaxInactiveInterval != null) { - cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval)); - } + cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval); HazelcastSession session = new HazelcastSession(cached, true); session.flushImmediateIfNecessary(); return session; diff --git a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfiguration.java b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfiguration.java index 5dfe6e31..d1077ed9 100644 --- a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfiguration.java +++ b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.session.hazelcast.config.annotation.web.http; +import java.time.Duration; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -56,7 +57,7 @@ import org.springframework.util.StringUtils; @Configuration(proxyBeanMethods = false) 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; @@ -77,8 +78,13 @@ public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfigur return createHazelcastIndexedSessionRepository(); } + public void setMaxInactiveInterval(Duration maxInactiveInterval) { + this.maxInactiveInterval = maxInactiveInterval; + } + + @Deprecated public void setMaxInactiveIntervalInSeconds(int maxInactiveIntervalInSeconds) { - this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; + setMaxInactiveInterval(Duration.ofSeconds(maxInactiveIntervalInSeconds)); } public void setSessionMapName(String sessionMapName) { @@ -128,7 +134,7 @@ public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfigur if (attributes == null) { return; } - this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds"); + this.maxInactiveInterval = Duration.ofSeconds(attributes.getNumber("maxInactiveIntervalInSeconds")); String sessionMapNameValue = attributes.getString("sessionMapName"); if (StringUtils.hasText(sessionMapNameValue)) { this.sessionMapName = sessionMapNameValue; @@ -147,7 +153,7 @@ public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfigur if (StringUtils.hasText(this.sessionMapName)) { sessionRepository.setSessionMapName(this.sessionMapName); } - sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds); + sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveInterval); sessionRepository.setFlushMode(this.flushMode); sessionRepository.setSaveMode(this.saveMode); this.sessionRepositoryCustomizers diff --git a/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepositoryTests.java b/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepositoryTests.java index 4c71046a..abe728e3 100644 --- a/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepositoryTests.java +++ b/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepositoryTests.java @@ -105,12 +105,12 @@ class HazelcastIndexedSessionRepositoryTests { void createSessionCustomMaxInactiveInterval() { verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean()); - int interval = 1; + Duration interval = Duration.ofSeconds(1); this.repository.setDefaultMaxInactiveInterval(interval); HazelcastSession session = this.repository.createSession(); - assertThat(session.getMaxInactiveInterval()).isEqualTo(Duration.ofSeconds(interval)); + assertThat(session.getMaxInactiveInterval()).isEqualTo(interval); verifyNoMoreInteractions(this.sessions); } diff --git a/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfigurationTests.java b/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfigurationTests.java index 97566536..ca708c03 100644 --- a/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfigurationTests.java +++ b/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfigurationTests.java @@ -16,6 +16,8 @@ package org.springframework.session.hazelcast.config.annotation.web.http; +import java.time.Duration; + import com.hazelcast.core.HazelcastInstance; import com.hazelcast.map.IMap; import org.junit.jupiter.api.AfterEach; @@ -106,9 +108,8 @@ class HazelcastHttpSessionConfigurationTests { registerAndRefresh(BaseConfiguration.class, CustomMaxInactiveIntervalInSecondsSetConfiguration.class); HazelcastIndexedSessionRepository repository = this.context.getBean(HazelcastIndexedSessionRepository.class); - assertThat(repository).isNotNull(); - assertThat(ReflectionTestUtils.getField(repository, "defaultMaxInactiveInterval")) - .isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS); + assertThat(repository).extracting("defaultMaxInactiveInterval") + .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } @Test @@ -116,9 +117,8 @@ class HazelcastHttpSessionConfigurationTests { registerAndRefresh(CustomMaxInactiveIntervalInSecondsConfiguration.class); HazelcastIndexedSessionRepository repository = this.context.getBean(HazelcastIndexedSessionRepository.class); - assertThat(repository).isNotNull(); - assertThat(ReflectionTestUtils.getField(repository, "defaultMaxInactiveInterval")) - .isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS); + assertThat(repository).extracting("defaultMaxInactiveInterval") + .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } @Test @@ -227,8 +227,8 @@ class HazelcastHttpSessionConfigurationTests { registerAndRefresh(SessionRepositoryCustomizerConfiguration.class); HazelcastIndexedSessionRepository sessionRepository = this.context .getBean(HazelcastIndexedSessionRepository.class); - assertThat(sessionRepository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", - MAX_INACTIVE_INTERVAL_IN_SECONDS); + assertThat(sessionRepository).extracting("defaultMaxInactiveInterval") + .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } @Test @@ -236,7 +236,7 @@ class HazelcastHttpSessionConfigurationTests { registerAndRefresh(ImportConfigAndCustomizeConfiguration.class); HazelcastIndexedSessionRepository sessionRepository = this.context .getBean(HazelcastIndexedSessionRepository.class); - assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(0); + assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ZERO); } private void registerAndRefresh(Class... annotatedClasses) { @@ -289,7 +289,7 @@ class HazelcastHttpSessionConfigurationTests { static class CustomMaxInactiveIntervalInSecondsSetConfiguration extends HazelcastHttpSessionConfiguration { CustomMaxInactiveIntervalInSecondsSetConfiguration() { - setMaxInactiveIntervalInSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS); + setMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } } @@ -443,14 +443,14 @@ class HazelcastHttpSessionConfigurationTests { @Bean @Order(0) SessionRepositoryCustomizer sessionRepositoryCustomizerOne() { - return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO); } @Bean @Order(1) SessionRepositoryCustomizer sessionRepositoryCustomizerTwo() { return (sessionRepository) -> sessionRepository - .setDefaultMaxInactiveInterval(MAX_INACTIVE_INTERVAL_IN_SECONDS); + .setDefaultMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } } @@ -461,7 +461,7 @@ class HazelcastHttpSessionConfigurationTests { @Bean SessionRepositoryCustomizer sessionRepositoryCustomizer() { - return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO); } } diff --git a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java index 16f9eab2..3a2e929e 100644 --- a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java +++ b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java @@ -236,11 +236,7 @@ public class JdbcIndexedSessionRepository implements private String deleteSessionsByExpiryTimeQuery; - /** - * If non-null, this value is used to override the default value for - * {@link JdbcSession#setMaxInactiveInterval(Duration)}. - */ - private Integer defaultMaxInactiveInterval; + private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS); private IndexResolver 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 * sessions will be invalidated. A negative time indicates that the session will never - * timeout. The default is 1800 (30 minutes). - * @param defaultMaxInactiveInterval the maximum inactive interval in seconds + * time out. The default is 30 minutes. + * @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; } @@ -452,9 +449,7 @@ public class JdbcIndexedSessionRepository implements @Override public JdbcSession createSession() { MapSession delegate = new MapSession(); - if (this.defaultMaxInactiveInterval != null) { - delegate.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval)); - } + delegate.setMaxInactiveInterval(this.defaultMaxInactiveInterval); JdbcSession session = new JdbcSession(delegate, UUID.randomUUID().toString(), true); session.flushIfRequired(); return session; diff --git a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfiguration.java b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfiguration.java index 078caf66..fa628acc 100644 --- a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfiguration.java +++ b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfiguration.java @@ -17,6 +17,7 @@ package org.springframework.session.jdbc.config.annotation.web.http; import java.sql.DatabaseMetaData; +import java.time.Duration; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -77,7 +78,7 @@ import org.springframework.util.StringValueResolver; public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration 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; @@ -118,7 +119,7 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration if (StringUtils.hasText(this.tableName)) { sessionRepository.setTableName(this.tableName); } - sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds); + sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveInterval); sessionRepository.setFlushMode(this.flushMode); sessionRepository.setSaveMode(this.saveMode); 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) { - this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; + setMaxInactiveInterval(Duration.ofSeconds(maxInactiveIntervalInSeconds)); } public void setTableName(String tableName) { @@ -246,7 +252,7 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration if (attributes == null) { return; } - this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds"); + this.maxInactiveInterval = Duration.ofSeconds(attributes.getNumber("maxInactiveIntervalInSeconds")); String tableNameValue = attributes.getString("tableName"); if (StringUtils.hasText(tableNameValue)) { this.tableName = this.embeddedValueResolver.resolveStringValue(tableNameValue); diff --git a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcIndexedSessionRepositoryTests.java b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcIndexedSessionRepositoryTests.java index e9392d03..1d3283ea 100644 --- a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcIndexedSessionRepositoryTests.java +++ b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcIndexedSessionRepositoryTests.java @@ -274,13 +274,13 @@ class JdbcIndexedSessionRepositoryTests { @Test void createSessionCustomMaxInactiveInterval() { - int interval = 1; + Duration interval = Duration.ofSeconds(1); this.repository.setDefaultMaxInactiveInterval(interval); JdbcSession session = this.repository.createSession(); assertThat(session.isNew()).isTrue(); - assertThat(session.getMaxInactiveInterval()).isEqualTo(Duration.ofSeconds(interval)); + assertThat(session.getMaxInactiveInterval()).isEqualTo(interval); verifyNoMoreInteractions(this.jdbcOperations); } diff --git a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java index 9ecca089..dcf5c2c7 100644 --- a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java +++ b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java @@ -16,6 +16,8 @@ package org.springframework.session.jdbc.config.annotation.web.http; +import java.time.Duration; + import javax.sql.DataSource; import org.junit.jupiter.api.AfterEach; @@ -118,9 +120,8 @@ class JdbcHttpSessionConfigurationTests { CustomMaxInactiveIntervalInSecondsAnnotationConfiguration.class); JdbcIndexedSessionRepository repository = this.context.getBean(JdbcIndexedSessionRepository.class); - assertThat(repository).isNotNull(); - assertThat(ReflectionTestUtils.getField(repository, "defaultMaxInactiveInterval")) - .isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS); + assertThat(repository).extracting("defaultMaxInactiveInterval") + .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } @Test @@ -128,9 +129,8 @@ class JdbcHttpSessionConfigurationTests { registerAndRefresh(DataSourceConfiguration.class, CustomMaxInactiveIntervalInSecondsSetterConfiguration.class); JdbcIndexedSessionRepository repository = this.context.getBean(JdbcIndexedSessionRepository.class); - assertThat(repository).isNotNull(); - assertThat(ReflectionTestUtils.getField(repository, "defaultMaxInactiveInterval")) - .isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS); + assertThat(repository).extracting("defaultMaxInactiveInterval") + .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } @Test @@ -297,8 +297,8 @@ class JdbcHttpSessionConfigurationTests { void sessionRepositoryCustomizer() { registerAndRefresh(DataSourceConfiguration.class, SessionRepositoryCustomizerConfiguration.class); JdbcIndexedSessionRepository sessionRepository = this.context.getBean(JdbcIndexedSessionRepository.class); - assertThat(sessionRepository).hasFieldOrPropertyWithValue("defaultMaxInactiveInterval", - MAX_INACTIVE_INTERVAL_IN_SECONDS); + assertThat(sessionRepository).extracting("defaultMaxInactiveInterval") + .isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } @Test @@ -315,7 +315,7 @@ class JdbcHttpSessionConfigurationTests { void importConfigAndCustomize() { registerAndRefresh(DataSourceConfiguration.class, ImportConfigAndCustomizeConfiguration.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) { @@ -552,14 +552,14 @@ class JdbcHttpSessionConfigurationTests { @Bean @Order(0) SessionRepositoryCustomizer sessionRepositoryCustomizerOne() { - return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO); } @Bean @Order(1) SessionRepositoryCustomizer sessionRepositoryCustomizerTwo() { return (sessionRepository) -> sessionRepository - .setDefaultMaxInactiveInterval(MAX_INACTIVE_INTERVAL_IN_SECONDS); + .setDefaultMaxInactiveInterval(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS)); } } @@ -570,7 +570,7 @@ class JdbcHttpSessionConfigurationTests { @Bean SessionRepositoryCustomizer sessionRepositoryCustomizer() { - return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0); + return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO); } }