Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure the session is committed only once per request #1783

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
filterChain.doFilter(wrappedRequest, wrappedResponse);
}
finally {
wrappedRequest.commitSession();
wrappedRequest.commitSession(true);
}
}

Expand Down Expand Up @@ -176,7 +176,7 @@ private final class SessionRepositoryResponseWrapper extends OnCommittedResponse

@Override
protected void onResponseCommitted() {
this.request.commitSession();
this.request.commitSession(true);
}

}
Expand All @@ -203,6 +203,8 @@ private final class SessionRepositoryRequestWrapper extends HttpServletRequestWr

private boolean requestedSessionInvalidated;

private boolean sessionCommitted;

private SessionRepositoryRequestWrapper(HttpServletRequest request, HttpServletResponse response) {
super(request);
this.response = response;
Expand All @@ -211,8 +213,13 @@ private SessionRepositoryRequestWrapper(HttpServletRequest request, HttpServletR
/**
* Uses the {@link HttpSessionIdResolver} to write the session id to the response
* and persist the Session.
* @param clearCache clear the cache in case used when committing before the end
* of the request processing
*/
private void commitSession() {
private void commitSession(boolean clearCache) {
if (this.sessionCommitted) {
return;
}
HttpSessionWrapper wrappedSession = getCurrentSession();
if (wrappedSession == null) {
if (isInvalidateClientSession()) {
Expand All @@ -221,13 +228,16 @@ private void commitSession() {
}
else {
S session = wrappedSession.getSession();
clearRequestedSessionCache();
if (clearCache) {
clearRequestedSessionCache();
}
SessionRepositoryFilter.this.sessionRepository.save(session);
String sessionId = session.getId();
if (!isRequestedSessionIdValid() || !sessionId.equals(getRequestedSessionId())) {
SessionRepositoryFilter.this.httpSessionIdResolver.setSessionId(this, this.response, sessionId);
}
}
this.sessionCommitted = true;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -409,7 +419,7 @@ public void forward(ServletRequest request, ServletResponse response) throws Ser

@Override
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException {
SessionRepositoryRequestWrapper.this.commitSession();
SessionRepositoryRequestWrapper.this.commitSession(false);
this.delegate.include(request, response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,30 @@ public void doFilter(HttpServletRequest wrappedRequest, HttpServletResponse wrap
verifyNoMoreInteractions(sessionRepository);
}

@Test
void committingTwiceHasNoEffect() throws Exception {
MapSession session = this.sessionRepository.createSession();
this.sessionRepository.save(session);
SessionRepository<MapSession> sessionRepository = spy(this.sessionRepository);
setSessionCookie(session.getId());

this.filter = new SessionRepositoryFilter<>(sessionRepository);

doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest, HttpServletResponse wrappedResponse)
throws ServletException, IOException {
wrappedRequest.getSession(false);
wrappedRequest.getRequestDispatcher("/").include(wrappedRequest, wrappedResponse);
wrappedRequest.getRequestDispatcher("/").include(wrappedRequest, wrappedResponse);
}
});

verify(sessionRepository, times(1)).findById(eq(session.getId()));
verify(sessionRepository).save(any());
verifyNoMoreInteractions(sessionRepository);
}

// --- order

@Test
Expand Down