Skip to content

Commit

Permalink
Optimize HttpSessionSecurityContextRepository
Browse files Browse the repository at this point in the history
Closes gh-9387
  • Loading branch information
rwinch committed Feb 11, 2021
1 parent 996ccc0 commit 38e9e8c
Showing 1 changed file with 5 additions and 9 deletions.
Expand Up @@ -142,13 +142,7 @@ public void saveContext(SecurityContext context, HttpServletRequest request,
+ response
+ ". You must use the HttpRequestResponseHolder.response after invoking loadContext");
}
// saveContext() might already be called by the response wrapper
// if something in the chain called sendError() or sendRedirect(). This ensures we
// only call it
// once per request.
if (!responseWrapper.isContextSaved()) {
responseWrapper.saveContext(context);
}
responseWrapper.saveContext(context);
}

public boolean containsContext(HttpServletRequest request) {
Expand Down Expand Up @@ -305,6 +299,7 @@ final class SaveToSessionResponseWrapper extends
private final boolean httpSessionExistedAtStartOfRequest;
private final SecurityContext contextBeforeExecution;
private final Authentication authBeforeExecution;
private boolean isSaveContextInvoked;

/**
* Takes the parameters required to call <code>saveContext()</code> successfully
Expand Down Expand Up @@ -355,6 +350,7 @@ protected void saveContext(SecurityContext context) {
// SEC-1587 A non-anonymous context may still be in the session
// SEC-1735 remove if the contextBeforeExecution was not anonymous
httpSession.removeAttribute(springSecurityContextKey);
this.isSaveContextInvoked = true;
}
return;
}
Expand All @@ -371,7 +367,7 @@ protected void saveContext(SecurityContext context) {
if (contextChanged(context)
|| httpSession.getAttribute(springSecurityContextKey) == null) {
httpSession.setAttribute(springSecurityContextKey, context);

this.isSaveContextInvoked = true;
if (logger.isDebugEnabled()) {
logger.debug("SecurityContext '" + context
+ "' stored to HttpSession: '" + httpSession);
Expand All @@ -381,7 +377,7 @@ protected void saveContext(SecurityContext context) {
}

private boolean contextChanged(SecurityContext context) {
return context != contextBeforeExecution
return this.isSaveContextInvoked || context != contextBeforeExecution
|| context.getAuthentication() != authBeforeExecution;
}

Expand Down

0 comments on commit 38e9e8c

Please sign in to comment.