Skip to content

Commit 0ecde06

Browse files
author
Laurent SCHOELENS
committed
[GH-2284] fix commitSession() called multiple times in requestDispatcher.include
1 parent ef7ef2a commit 0ecde06

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ private final class SessionRepositoryRequestWrapper extends HttpServletRequestWr
205205

206206
private boolean requestedSessionInvalidated;
207207

208+
private boolean hasCommitedInInclude;
209+
208210
private SessionRepositoryRequestWrapper(HttpServletRequest request, HttpServletResponse response) {
209211
super(request);
210212
this.response = response;
@@ -338,7 +340,7 @@ public String getRequestedSessionId() {
338340
@Override
339341
public RequestDispatcher getRequestDispatcher(String path) {
340342
RequestDispatcher requestDispatcher = super.getRequestDispatcher(path);
341-
return new SessionCommittingRequestDispatcher(requestDispatcher);
343+
return new SessionCommittingRequestDispatcher(requestDispatcher, this);
342344
}
343345

344346
private S getRequestedSession() {
@@ -397,8 +399,11 @@ private final class SessionCommittingRequestDispatcher implements RequestDispatc
397399

398400
private final RequestDispatcher delegate;
399401

400-
SessionCommittingRequestDispatcher(RequestDispatcher delegate) {
402+
private final SessionRepositoryRequestWrapper wrapper;
403+
404+
SessionCommittingRequestDispatcher(RequestDispatcher delegate, SessionRepositoryRequestWrapper wrapper) {
401405
this.delegate = delegate;
406+
this.wrapper = wrapper;
402407
}
403408

404409
@Override
@@ -408,7 +413,10 @@ public void forward(ServletRequest request, ServletResponse response) throws Ser
408413

409414
@Override
410415
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException {
411-
SessionRepositoryRequestWrapper.this.commitSession();
416+
if (!this.wrapper.hasCommitedInInclude) {
417+
SessionRepositoryRequestWrapper.this.commitSession();
418+
this.wrapper.hasCommitedInInclude = true;
419+
}
412420
this.delegate.include(request, response);
413421
}
414422

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

+26
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,32 @@ public void doFilter(HttpServletRequest wrappedRequest) {
13201320
assertThat(bindingListener.getCounter()).isEqualTo(1);
13211321
}
13221322

1323+
@Test // gh-2284
1324+
void doFilterIncludeCommitSessionOnce() throws Exception {
1325+
MapSession session = this.sessionRepository.createSession();
1326+
this.sessionRepository.save(session);
1327+
SessionRepository<MapSession> sessionRepository = spy(this.sessionRepository);
1328+
setSessionCookie(session.getId());
1329+
1330+
given(sessionRepository.findById(session.getId())).willReturn(session);
1331+
1332+
this.filter = new SessionRepositoryFilter<>(sessionRepository);
1333+
1334+
doFilter(new DoInFilter() {
1335+
@Override
1336+
public void doFilter(HttpServletRequest wrappedRequest, HttpServletResponse wrappedResponse)
1337+
throws IOException, ServletException {
1338+
String id = wrappedRequest.getSession().getId();
1339+
wrappedRequest.getRequestDispatcher("/").include(wrappedRequest, wrappedResponse);
1340+
assertThat(SessionRepositoryFilterTests.this.sessionRepository.findById(id)).isNotNull();
1341+
wrappedRequest.getRequestDispatcher("/").include(wrappedRequest, wrappedResponse);
1342+
verify(sessionRepository).findById(session.getId());
1343+
verify(sessionRepository).save(session);
1344+
verifyNoMoreInteractions(sessionRepository);
1345+
}
1346+
});
1347+
}
1348+
13231349
// --- helper methods
13241350

13251351
private void assertNewSession() {

0 commit comments

Comments
 (0)