SessionRepositoryFilter caches null session lookup

If a session cannot be found by id, we will cache that result for any
subsequent calls to getSession(false) for the duration of this request.

Fixes gh-423
This commit is contained in:
Øyvind Horneland
2016-03-14 15:08:19 +01:00
committed by Rob Winch
parent 4f7728f5b5
commit 831d2f4152
2 changed files with 42 additions and 1 deletions

View File

@@ -87,6 +87,11 @@ public class SessionRepositoryFilter<S extends ExpiringSession>
public static final String SESSION_REPOSITORY_ATTR = SessionRepository.class
.getName();
/**
* Invalid session id (not backed by the session repository) request attribute name.
*/
public static final String INVALID_SESSION_ID_ATTR = SESSION_REPOSITORY_ATTR + ".invalidSessionId";
/**
* The default filter order.
*/
@@ -330,7 +335,7 @@ public class SessionRepositoryFilter<S extends ExpiringSession>
return currentSession;
}
String requestedSessionId = getRequestedSessionId();
if (requestedSessionId != null) {
if (requestedSessionId != null && getAttribute(INVALID_SESSION_ID_ATTR) == null) {
S session = getSession(requestedSessionId);
if (session != null) {
this.requestedSessionIdValid = true;
@@ -338,6 +343,12 @@ public class SessionRepositoryFilter<S extends ExpiringSession>
currentSession.setNew(false);
setCurrentSession(currentSession);
return currentSession;
} else {
// This is an invalid session id. No need to ask again if request.getSession is invoked for the duration of this request
if (SESSION_LOGGER.isDebugEnabled()) {
SESSION_LOGGER.debug("No session found by id: Caching result for getSession(false) for this HttpServletRequest.");
}
setAttribute(INVALID_SESSION_ID_ATTR, "true");
}
}
if (!create) {