Skip to content

Commit c330bde

Browse files
committed
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.
1 parent da8e5fb commit c330bde

File tree

37 files changed

+270
-279
lines changed

37 files changed

+270
-279
lines changed

spring-session-core/src/main/java/org/springframework/session/MapSession.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,6 +53,11 @@ public final class MapSession implements Session, Serializable {
5353
*/
5454
public static final int DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS = 1800;
5555

56+
/**
57+
* Default {@link #setMaxInactiveInterval(Duration)} (30 minutes).
58+
*/
59+
public static final Duration DEFAULT_MAX_INACTIVE_INTERVAL = Duration.ofSeconds(DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
60+
5661
private String id;
5762

5863
private final String originalId;
@@ -66,7 +71,7 @@ public final class MapSession implements Session, Serializable {
6671
/**
6772
* Defaults to 30 minutes.
6873
*/
69-
private Duration maxInactiveInterval = Duration.ofSeconds(DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
74+
private Duration maxInactiveInterval = DEFAULT_MAX_INACTIVE_INTERVAL;
7075

7176
/**
7277
* Creates a new instance with a secure randomly generated identifier.

spring-session-core/src/main/java/org/springframework/session/MapSessionRepository.java

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.session.events.SessionDeletedEvent;
2323
import org.springframework.session.events.SessionExpiredEvent;
24+
import org.springframework.util.Assert;
2425

2526
/**
2627
* A {@link SessionRepository} backed by a {@link java.util.Map} and that uses a
@@ -38,11 +39,7 @@
3839
*/
3940
public class MapSessionRepository implements SessionRepository<MapSession> {
4041

41-
/**
42-
* If non-null, this value is used to override
43-
* {@link Session#setMaxInactiveInterval(Duration)}.
44-
*/
45-
private Integer defaultMaxInactiveInterval;
42+
private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
4643

4744
private final Map<String, Session> sessions;
4845

@@ -59,12 +56,13 @@ public MapSessionRepository(Map<String, Session> sessions) {
5956
}
6057

6158
/**
62-
* If non-null, this value is used to override
63-
* {@link Session#setMaxInactiveInterval(Duration)}.
64-
* @param defaultMaxInactiveInterval the number of seconds that the {@link Session}
65-
* should be kept alive between client requests.
59+
* Set the maximum inactive interval in seconds between requests before newly created
60+
* sessions will be invalidated. A negative time indicates that the session will never
61+
* time out. The default is 30 minutes.
62+
* @param defaultMaxInactiveInterval the default maxInactiveInterval
6663
*/
67-
public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) {
64+
public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
65+
Assert.notNull(defaultMaxInactiveInterval, "defaultMaxInactiveInterval must not be null");
6866
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
6967
}
7068

@@ -97,9 +95,7 @@ public void deleteById(String id) {
9795
@Override
9896
public MapSession createSession() {
9997
MapSession result = new MapSession();
100-
if (this.defaultMaxInactiveInterval != null) {
101-
result.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
102-
}
98+
result.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
10399
return result;
104100
}
105101

spring-session-core/src/main/java/org/springframework/session/ReactiveMapSessionRepository.java

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.session.events.SessionDeletedEvent;
2525
import org.springframework.session.events.SessionExpiredEvent;
26+
import org.springframework.util.Assert;
2627

2728
/**
2829
* A {@link ReactiveSessionRepository} backed by a {@link Map} and that uses a
@@ -40,11 +41,7 @@
4041
*/
4142
public class ReactiveMapSessionRepository implements ReactiveSessionRepository<MapSession> {
4243

43-
/**
44-
* If non-null, this value is used to override
45-
* {@link Session#setMaxInactiveInterval(Duration)}.
46-
*/
47-
private Integer defaultMaxInactiveInterval;
44+
private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
4845

4946
private final Map<String, Session> sessions;
5047

@@ -61,12 +58,13 @@ public ReactiveMapSessionRepository(Map<String, Session> sessions) {
6158
}
6259

6360
/**
64-
* If non-null, this value is used to override
65-
* {@link Session#setMaxInactiveInterval(Duration)}.
66-
* @param defaultMaxInactiveInterval the number of seconds that the {@link Session}
67-
* should be kept alive between client requests.
61+
* Set the maximum inactive interval in seconds between requests before newly created
62+
* sessions will be invalidated. A negative time indicates that the session will never
63+
* time out. The default is 30 minutes.
64+
* @param defaultMaxInactiveInterval the default maxInactiveInterval
6865
*/
69-
public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) {
66+
public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
67+
Assert.notNull(defaultMaxInactiveInterval, "defaultMaxInactiveInterval must not be null");
7068
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
7169
}
7270

@@ -99,9 +97,7 @@ public Mono<Void> deleteById(String id) {
9997
public Mono<MapSession> createSession() {
10098
return Mono.defer(() -> {
10199
MapSession result = new MapSession();
102-
if (this.defaultMaxInactiveInterval != null) {
103-
result.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
104-
}
100+
result.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
105101
return Mono.just(result);
106102
});
107103
}

spring-session-core/src/test/java/org/springframework/session/MapSessionRepositoryTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,8 +60,8 @@ void createSessionDefaultExpiration() {
6060

6161
@Test
6262
void createSessionCustomDefaultExpiration() {
63-
final Duration expectedMaxInterval = new MapSession().getMaxInactiveInterval().plusSeconds(10);
64-
this.repository.setDefaultMaxInactiveInterval((int) expectedMaxInterval.getSeconds());
63+
Duration expectedMaxInterval = new MapSession().getMaxInactiveInterval().plusSeconds(10);
64+
this.repository.setDefaultMaxInactiveInterval(expectedMaxInterval);
6565

6666
Session session = this.repository.createSession();
6767

spring-session-core/src/test/java/org/springframework/session/ReactiveMapSessionRepositoryTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -103,8 +103,8 @@ void createSessionWhenDefaultMaxInactiveIntervalThenDefaultMaxInactiveInterval()
103103

104104
@Test
105105
void createSessionWhenCustomMaxInactiveIntervalThenCustomMaxInactiveInterval() {
106-
final Duration expectedMaxInterval = new MapSession().getMaxInactiveInterval().plusSeconds(10);
107-
this.repository.setDefaultMaxInactiveInterval((int) expectedMaxInterval.getSeconds());
106+
Duration expectedMaxInterval = new MapSession().getMaxInactiveInterval().plusSeconds(10);
107+
this.repository.setDefaultMaxInactiveInterval(expectedMaxInterval);
108108

109109
Session session = this.repository.createSession().block();
110110

spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoIndexedSessionRepository.java

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
import org.springframework.data.mongodb.core.index.IndexOperations;
3535
import org.springframework.lang.Nullable;
3636
import org.springframework.session.FindByIndexNameSessionRepository;
37+
import org.springframework.session.MapSession;
3738
import org.springframework.session.events.SessionCreatedEvent;
3839
import org.springframework.session.events.SessionDeletedEvent;
3940
import org.springframework.session.events.SessionExpiredEvent;
@@ -46,15 +47,19 @@
4647
*
4748
* @author Jakub Kubrynski
4849
* @author Greg Turnquist
50+
* @author Vedran Pavic
4951
* @since 2.2.0
5052
*/
5153
public class MongoIndexedSessionRepository
5254
implements FindByIndexNameSessionRepository<MongoSession>, ApplicationEventPublisherAware, InitializingBean {
5355

5456
/**
5557
* The default time period in seconds in which a session will expire.
58+
* @deprecated since 3.0.0 in favor of
59+
* {@link MapSession#DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS}
5660
*/
57-
public static final int DEFAULT_INACTIVE_INTERVAL = 1800;
61+
@Deprecated
62+
public static final int DEFAULT_INACTIVE_INTERVAL = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
5863

5964
/**
6065
* the default collection name for storing session.
@@ -65,12 +70,12 @@ public class MongoIndexedSessionRepository
6570

6671
private final MongoOperations mongoOperations;
6772

68-
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
73+
private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
6974

7075
private String collectionName = DEFAULT_COLLECTION_NAME;
7176

7277
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
73-
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
78+
this.defaultMaxInactiveInterval);
7479

7580
private ApplicationEventPublisher eventPublisher;
7681

@@ -83,9 +88,7 @@ public MongoSession createSession() {
8388

8489
MongoSession session = new MongoSession();
8590

86-
if (this.maxInactiveIntervalInSeconds != null) {
87-
session.setMaxInactiveInterval(Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
88-
}
91+
session.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
8992

9093
publishEvent(new SessionCreatedEvent(this, session));
9194

@@ -178,8 +181,16 @@ private void publishEvent(ApplicationEvent event) {
178181
}
179182
}
180183

181-
public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) {
182-
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
184+
/**
185+
* Set the maximum inactive interval in seconds between requests before newly created
186+
* sessions will be invalidated. A negative time indicates that the session will never
187+
* time out. The default is 30 minutes.
188+
* @param defaultMaxInactiveInterval the default maxInactiveInterval
189+
*/
190+
public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
191+
org.springframework.util.Assert.notNull(defaultMaxInactiveInterval,
192+
"defaultMaxInactiveInterval must not be null");
193+
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
183194
}
184195

185196
public void setCollectionName(final String collectionName) {

spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoSession.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.stream.Collectors;
2828

2929
import org.springframework.lang.Nullable;
30+
import org.springframework.session.MapSession;
3031
import org.springframework.session.Session;
3132

3233
/**
@@ -66,7 +67,7 @@ public class MongoSession implements Session {
6667
private Map<String, Object> attrs = new HashMap<>();
6768

6869
public MongoSession() {
69-
this(MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL);
70+
this(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
7071
}
7172

7273
public MongoSession(long maxInactiveIntervalInSeconds) {

spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/ReactiveMongoSessionRepository.java

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,23 +32,29 @@
3232
import org.springframework.data.mongodb.core.index.IndexOperations;
3333
import org.springframework.data.mongodb.core.query.Criteria;
3434
import org.springframework.data.mongodb.core.query.Query;
35+
import org.springframework.session.MapSession;
3536
import org.springframework.session.ReactiveSessionRepository;
3637
import org.springframework.session.events.SessionCreatedEvent;
3738
import org.springframework.session.events.SessionDeletedEvent;
39+
import org.springframework.util.Assert;
3840

3941
/**
4042
* A {@link ReactiveSessionRepository} implementation that uses Spring Data MongoDB.
4143
*
4244
* @author Greg Turnquist
45+
* @author Vedran Pavic
4346
* @since 2.2.0
4447
*/
4548
public class ReactiveMongoSessionRepository
4649
implements ReactiveSessionRepository<MongoSession>, ApplicationEventPublisherAware, InitializingBean {
4750

4851
/**
4952
* The default time period in seconds in which a session will expire.
53+
* @deprecated since 3.0.0 in favor of
54+
* {@link MapSession#DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS}
5055
*/
51-
public static final int DEFAULT_INACTIVE_INTERVAL = 1800;
56+
@Deprecated
57+
public static final int DEFAULT_INACTIVE_INTERVAL = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
5258

5359
/**
5460
* The default collection name for storing session.
@@ -59,12 +65,12 @@ public class ReactiveMongoSessionRepository
5965

6066
private final ReactiveMongoOperations mongoOperations;
6167

62-
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
68+
private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
6369

6470
private String collectionName = DEFAULT_COLLECTION_NAME;
6571

6672
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
67-
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
73+
this.defaultMaxInactiveInterval);
6874

6975
private MongoOperations blockingMongoOperations;
7076

@@ -88,7 +94,7 @@ public ReactiveMongoSessionRepository(ReactiveMongoOperations mongoOperations) {
8894
@Override
8995
public Mono<MongoSession> createSession() {
9096

91-
return Mono.justOrEmpty(this.maxInactiveIntervalInSeconds) //
97+
return Mono.justOrEmpty(this.defaultMaxInactiveInterval.toSeconds()) //
9298
.map(MongoSession::new) //
9399
.doOnNext((mongoSession) -> publishEvent(new SessionCreatedEvent(this, mongoSession))) //
94100
.switchIfEmpty(Mono.just(new MongoSession()));
@@ -170,12 +176,15 @@ private void publishEvent(ApplicationEvent event) {
170176
}
171177
}
172178

173-
public Integer getMaxInactiveIntervalInSeconds() {
174-
return this.maxInactiveIntervalInSeconds;
175-
}
176-
177-
public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) {
178-
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
179+
/**
180+
* Set the maximum inactive interval in seconds between requests before newly created
181+
* sessions will be invalidated. A negative time indicates that the session will never
182+
* time out. The default is 30 minutes.
183+
* @param defaultMaxInactiveInterval the default maxInactiveInterval
184+
*/
185+
public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
186+
Assert.notNull(defaultMaxInactiveInterval, "defaultMaxInactiveInterval must not be null");
187+
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
179188
}
180189

181190
public String getCollectionName() {

spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/config/annotation/web/http/EnableMongoHttpSession.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.annotation.Target;
2424

2525
import org.springframework.context.annotation.Import;
26+
import org.springframework.session.MapSession;
2627
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
2728

2829
/**
@@ -58,7 +59,7 @@
5859
* The maximum time a session will be kept if it is inactive.
5960
* @return default max inactive interval in seconds
6061
*/
61-
int maxInactiveIntervalInSeconds() default MongoIndexedSessionRepository.DEFAULT_INACTIVE_INTERVAL;
62+
int maxInactiveIntervalInSeconds() default MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
6263

6364
/**
6465
* The collection name to use.

0 commit comments

Comments
 (0)