Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 245d69a

Browse files
committed
Track change of MongoSession's id to properly delete.
When a session is made invalid and changed to a new one, the old one must be deleted from MongoDB at the next save(). Resolves #116.
1 parent d6206cc commit 245d69a

File tree

5 files changed

+286
-10
lines changed

5 files changed

+286
-10
lines changed

pom.xml

+39
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,24 @@
525525
<scope>test</scope>
526526
</dependency>
527527

528+
<dependency>
529+
<groupId>org.springframework</groupId>
530+
<artifactId>spring-webflux</artifactId>
531+
<scope>test</scope>
532+
</dependency>
533+
534+
<dependency>
535+
<groupId>org.springframework.security</groupId>
536+
<artifactId>spring-security-config</artifactId>
537+
<scope>test</scope>
538+
</dependency>
539+
540+
<dependency>
541+
<groupId>org.springframework.security</groupId>
542+
<artifactId>spring-security-web</artifactId>
543+
<scope>test</scope>
544+
</dependency>
545+
528546
<dependency>
529547
<groupId>de.flapdoodle.embed</groupId>
530548
<artifactId>de.flapdoodle.embed.mongo</artifactId>
@@ -575,6 +593,27 @@
575593
<scope>test</scope>
576594
</dependency>
577595

596+
<dependency>
597+
<groupId>org.hamcrest</groupId>
598+
<artifactId>hamcrest</artifactId>
599+
<version>2.1</version>
600+
<scope>test</scope>
601+
</dependency>
602+
603+
<dependency>
604+
<groupId>ch.qos.logback</groupId>
605+
<artifactId>logback-classic</artifactId>
606+
<version>1.2.3</version>
607+
<scope>test</scope>
608+
</dependency>
609+
610+
<dependency>
611+
<groupId>ch.qos.logback</groupId>
612+
<artifactId>logback-core</artifactId>
613+
<version>1.2.3</version>
614+
<scope>test</scope>
615+
</dependency>
616+
578617
<dependency>
579618
<groupId>org.mockito</groupId>
580619
<artifactId>mockito-core</artifactId>

src/main/java/org/springframework/session/data/mongo/MongoSession.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class MongoSession implements Session {
4343
private static final char DOT_COVER_CHAR = '';
4444

4545
private String id;
46+
private String originalSessionId;
4647
private long createdMillis = System.currentTimeMillis();
4748
private long accessedMillis;
4849
private long intervalSeconds;
@@ -60,6 +61,7 @@ public MongoSession(long maxInactiveIntervalInSeconds) {
6061
public MongoSession(String id, long maxInactiveIntervalInSeconds) {
6162

6263
this.id = id;
64+
this.originalSessionId = id;
6365
this.intervalSeconds = maxInactiveIntervalInSeconds;
6466
setLastAccessedTime(Instant.ofEpochMilli(this.createdMillis));
6567
}
@@ -72,6 +74,7 @@ static String uncoverDot(String attributeName) {
7274
return attributeName.replace(DOT_COVER_CHAR, '.');
7375
}
7476

77+
@Override
7578
public String changeSessionId() {
7679

7780
String changedId = UUID.randomUUID().toString();
@@ -85,10 +88,12 @@ public <T> T getAttribute(String attributeName) {
8588
return (T) this.attrs.get(coverDot(attributeName));
8689
}
8790

91+
@Override
8892
public Set<String> getAttributeNames() {
8993
return this.attrs.keySet().stream().map(MongoSession::uncoverDot).collect(Collectors.toSet());
9094
}
9195

96+
@Override
9297
public void setAttribute(String attributeName, Object attributeValue) {
9398

9499
if (attributeValue == null) {
@@ -98,10 +103,12 @@ public void setAttribute(String attributeName, Object attributeValue) {
98103
}
99104
}
100105

106+
@Override
101107
public void removeAttribute(String attributeName) {
102108
this.attrs.remove(coverDot(attributeName));
103109
}
104110

111+
@Override
105112
public Instant getCreationTime() {
106113
return Instant.ofEpochMilli(this.createdMillis);
107114
}
@@ -110,24 +117,29 @@ public void setCreationTime(long created) {
110117
this.createdMillis = created;
111118
}
112119

120+
@Override
113121
public Instant getLastAccessedTime() {
114122
return Instant.ofEpochMilli(this.accessedMillis);
115123
}
116124

125+
@Override
117126
public void setLastAccessedTime(Instant lastAccessedTime) {
118127

119128
this.accessedMillis = lastAccessedTime.toEpochMilli();
120129
this.expireAt = Date.from(lastAccessedTime.plus(Duration.ofSeconds(this.intervalSeconds)));
121130
}
122131

132+
@Override
123133
public Duration getMaxInactiveInterval() {
124134
return Duration.ofSeconds(this.intervalSeconds);
125135
}
126136

137+
@Override
127138
public void setMaxInactiveInterval(Duration interval) {
128139
this.intervalSeconds = interval.getSeconds();
129140
}
130141

142+
@Override
131143
public boolean isExpired() {
132144
return this.intervalSeconds >= 0 && new Date().after(this.expireAt);
133145
}
@@ -140,14 +152,15 @@ public boolean equals(Object o) {
140152
if (o == null || getClass() != o.getClass())
141153
return false;
142154
MongoSession that = (MongoSession) o;
143-
return Objects.equals(id, that.id);
155+
return Objects.equals(this.id, that.id);
144156
}
145157

146158
@Override
147159
public int hashCode() {
148-
return Objects.hash(id);
160+
return Objects.hash(this.id);
149161
}
150162

163+
@Override
151164
public String getId() {
152165
return this.id;
153166
}
@@ -159,4 +172,12 @@ public Date getExpireAt() {
159172
public void setExpireAt(final Date expireAt) {
160173
this.expireAt = expireAt;
161174
}
175+
176+
boolean hasChangedSessionId() {
177+
return !getId().equals(this.originalSessionId);
178+
}
179+
180+
String getOriginalSessionId() {
181+
return this.originalSessionId;
182+
}
162183
}

src/main/java/org/springframework/session/data/mongo/ReactiveMongoSessionRepository.java

+16-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.session.data.mongo;
1717

18+
import static org.springframework.data.mongodb.core.query.Criteria.*;
19+
import static org.springframework.data.mongodb.core.query.Query.*;
1820
import static org.springframework.session.data.mongo.MongoSessionUtils.*;
1921

2022
import java.time.Duration;
@@ -34,8 +36,6 @@
3436
import org.springframework.session.events.SessionCreatedEvent;
3537
import org.springframework.session.events.SessionDeletedEvent;
3638

37-
import com.mongodb.DBObject;
38-
3939
/**
4040
* A {@link ReactiveSessionRepository} implementation that uses Spring Data MongoDB.
4141
*
@@ -92,12 +92,20 @@ public Mono<MongoSession> createSession() {
9292
@Override
9393
public Mono<Void> save(MongoSession session) {
9494

95-
DBObject dbObject = convertToDBObject(this.mongoSessionConverter, session);
96-
if (dbObject != null) {
97-
return this.mongoOperations.save(dbObject, this.collectionName).then();
98-
} else {
99-
return Mono.empty();
100-
}
95+
return Mono //
96+
.justOrEmpty(convertToDBObject(this.mongoSessionConverter, session)) //
97+
.flatMap(dbObject -> {
98+
if (session.hasChangedSessionId()) {
99+
100+
return this.mongoOperations
101+
.remove(query(where("_id").is(session.getOriginalSessionId())), this.collectionName) //
102+
.then(this.mongoOperations.save(dbObject, this.collectionName));
103+
} else {
104+
105+
return this.mongoOperations.save(dbObject, this.collectionName);
106+
}
107+
}) //
108+
.then();
101109
}
102110

103111
@Override

0 commit comments

Comments
 (0)