Skip to content

Commit 6d02790

Browse files
committed
Fix caching of requested session in SessionRepositoryFilter
Closes gh-1076
1 parent 42818a1 commit 6d02790

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

spring-session-core/src/main/java/org/springframework/session/web/http/SessionRepositoryFilter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ private void commitSession() {
230230
}
231231
else {
232232
S session = wrappedSession.getSession();
233-
saveSession(session);
233+
clearRequestedSessionCache();
234+
SessionRepositoryFilter.this.sessionRepository.save(session);
234235
String sessionId = session.getId();
235236
if (!isRequestedSessionIdValid()
236237
|| !sessionId.equals(getRequestedSessionId())) {
@@ -371,10 +372,9 @@ private S getRequestedSession() {
371372
return this.requestedSession;
372373
}
373374

374-
private void saveSession(S session) {
375+
private void clearRequestedSessionCache() {
375376
this.requestedSessionCached = false;
376377
this.requestedSession = null;
377-
SessionRepositoryFilter.this.sessionRepository.save(session);
378378
}
379379

380380
/**
@@ -394,6 +394,7 @@ public void invalidate() {
394394
super.invalidate();
395395
SessionRepositoryRequestWrapper.this.requestedSessionInvalidated = true;
396396
setCurrentSession(null);
397+
clearRequestedSessionCache();
397398
SessionRepositoryFilter.this.sessionRepository.deleteById(getId());
398399
}
399400
}

spring-session-core/src/test/java/org/springframework/session/web/http/SessionRepositoryFilterTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import static org.mockito.Mockito.never;
6868
import static org.mockito.Mockito.reset;
6969
import static org.mockito.Mockito.spy;
70+
import static org.mockito.Mockito.times;
7071
import static org.mockito.Mockito.verify;
7172
import static org.mockito.Mockito.verifyZeroInteractions;
7273

@@ -1335,6 +1336,32 @@ public void doFilter(HttpServletRequest wrappedRequest,
13351336
verifyZeroInteractions(sessionRepository);
13361337
}
13371338

1339+
@Test
1340+
public void doFilterSessionRetrievalIsCached() throws Exception {
1341+
MapSession session = this.sessionRepository.createSession();
1342+
this.sessionRepository.save(session);
1343+
SessionRepository<MapSession> sessionRepository = spy(this.sessionRepository);
1344+
setSessionCookie(session.getId());
1345+
1346+
this.filter = new SessionRepositoryFilter<>(sessionRepository);
1347+
1348+
doFilter(new DoInFilter() {
1349+
@Override
1350+
public void doFilter(HttpServletRequest wrappedRequest,
1351+
HttpServletResponse wrappedResponse) {
1352+
wrappedRequest.getSession().invalidate();
1353+
wrappedRequest.getSession();
1354+
}
1355+
});
1356+
1357+
// 3 invocations expected: initial resolution, after invalidation, after commit
1358+
verify(sessionRepository, times(3)).findById(eq(session.getId()));
1359+
verify(sessionRepository).deleteById(eq(session.getId()));
1360+
verify(sessionRepository).createSession();
1361+
verify(sessionRepository).save(any());
1362+
verifyZeroInteractions(sessionRepository);
1363+
}
1364+
13381365
// --- order
13391366

13401367
@Test

0 commit comments

Comments
 (0)