@@ -142,13 +142,7 @@ public void saveContext(SecurityContext context, HttpServletRequest request,
142142 + response
143143 + ". You must use the HttpRequestResponseHolder.response after invoking loadContext" );
144144 }
145- // saveContext() might already be called by the response wrapper
146- // if something in the chain called sendError() or sendRedirect(). This ensures we
147- // only call it
148- // once per request.
149- if (!responseWrapper .isContextSaved ()) {
150- responseWrapper .saveContext (context );
151- }
145+ responseWrapper .saveContext (context );
152146 }
153147
154148 public boolean containsContext (HttpServletRequest request ) {
@@ -305,6 +299,7 @@ final class SaveToSessionResponseWrapper extends
305299 private final boolean httpSessionExistedAtStartOfRequest ;
306300 private final SecurityContext contextBeforeExecution ;
307301 private final Authentication authBeforeExecution ;
302+ private boolean isSaveContextInvoked ;
308303
309304 /**
310305 * Takes the parameters required to call <code>saveContext()</code> successfully
@@ -355,6 +350,7 @@ protected void saveContext(SecurityContext context) {
355350 // SEC-1587 A non-anonymous context may still be in the session
356351 // SEC-1735 remove if the contextBeforeExecution was not anonymous
357352 httpSession .removeAttribute (springSecurityContextKey );
353+ this .isSaveContextInvoked = true ;
358354 }
359355 return ;
360356 }
@@ -371,7 +367,7 @@ protected void saveContext(SecurityContext context) {
371367 if (contextChanged (context )
372368 || httpSession .getAttribute (springSecurityContextKey ) == null ) {
373369 httpSession .setAttribute (springSecurityContextKey , context );
374-
370+ this . isSaveContextInvoked = true ;
375371 if (logger .isDebugEnabled ()) {
376372 logger .debug ("SecurityContext '" + context
377373 + "' stored to HttpSession: '" + httpSession );
@@ -381,7 +377,7 @@ protected void saveContext(SecurityContext context) {
381377 }
382378
383379 private boolean contextChanged (SecurityContext context ) {
384- return context != contextBeforeExecution
380+ return this . isSaveContextInvoked || context != contextBeforeExecution
385381 || context .getAuthentication () != authBeforeExecution ;
386382 }
387383
0 commit comments