SessionRepositoryRequestWrapper overrides isRequestedSessionIdValid

Fixes gh-142, gh-153
This commit is contained in:
Rob Winch
2015-02-19 16:40:23 -06:00
parent 29ad238307
commit 369e98c7ef
2 changed files with 67 additions and 4 deletions

View File

@@ -150,7 +150,7 @@ public class SessionRepositoryFilter<S extends ExpiringSession> extends OncePerR
*/
private final class SessionRepositoryRequestWrapper extends HttpServletRequestWrapper {
private HttpSessionWrapper currentSession;
private boolean requestedValidSession;
private Boolean requestedSessionIdValid;
private final HttpServletResponse response;
private SessionRepositoryRequestWrapper(HttpServletRequest request, HttpServletResponse response) {
@@ -170,14 +170,31 @@ public class SessionRepositoryFilter<S extends ExpiringSession> extends OncePerR
} else {
S session = wrappedSession.session;
sessionRepository.save(session);
if(!requestedValidSession || !session.getId().equals(getRequestedSessionId())) {
if(!isRequestedSessionIdValid() || !session.getId().equals(getRequestedSessionId())) {
httpSessionStrategy.onNewSession(session, this, response);
}
}
}
public boolean isRequestedSessionIdValid() {
if(requestedSessionIdValid == null) {
String sessionId = getRequestedSessionId();
S session = sessionId == null ? null : sessionRepository.getSession(sessionId);
return isRequestedSessionIdValid(session);
}
return requestedSessionIdValid;
}
private boolean isRequestedSessionIdValid(S session) {
if(requestedSessionIdValid == null) {
requestedSessionIdValid = session != null;
}
return requestedSessionIdValid;
}
private boolean isInvalidateClientSession() {
return currentSession == null && requestedValidSession;
return currentSession == null && isRequestedSessionIdValid();
}
@Override
@@ -189,7 +206,7 @@ public class SessionRepositoryFilter<S extends ExpiringSession> extends OncePerR
if(requestedSessionId != null) {
S session = sessionRepository.getSession(requestedSessionId);
if(session != null) {
this.requestedValidSession = true;
this.requestedSessionIdValid = true;
currentSession = new HttpSessionWrapper(session, getServletContext());
currentSession.setNew(false);
return currentSession;

View File

@@ -393,6 +393,52 @@ public class SessionRepositoryFilterTests<S extends ExpiringSession> {
assertNoSession();
}
@Test
public void doFilterIsRequestedValidSessionTrue() throws Exception {
doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
wrappedRequest.getSession();
}
});
setupSession();
request.setRequestedSessionIdValid(false); // ensure we are using wrapped request
doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
assertThat(wrappedRequest.isRequestedSessionIdValid()).isTrue();
}
});
}
// gh-142, gh-153
@Test
public void doFilterIsRequestedValidSessionFalseInvalidId() throws Exception {
setSessionCookie("invalid");
request.setRequestedSessionIdValid(true); // ensure we are using wrapped request
doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
assertThat(wrappedRequest.isRequestedSessionIdValid()).isFalse();
}
});
}
@Test
public void doFilterIsRequestedValidSessionFalse() throws Exception {
request.setRequestedSessionIdValid(true); // ensure we are using wrapped request
doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest) {
assertThat(wrappedRequest.isRequestedSessionIdValid()).isFalse();
}
});
}
@Test
public void doFilterGetSessionGetSessionFalse() throws Exception {
doFilter(new DoInFilter() {