@@ -136,12 +136,7 @@ public void saveContext(SecurityContext context, HttpServletRequest request, Htt
136
136
SaveContextOnUpdateOrErrorResponseWrapper .class );
137
137
Assert .state (responseWrapper != null , () -> "Cannot invoke saveContext on response " + response
138
138
+ ". You must use the HttpRequestResponseHolder.response after invoking loadContext" );
139
- // saveContext() might already be called by the response wrapper if something in
140
- // the chain called sendError() or sendRedirect(). This ensures we only call it
141
- // once per request.
142
- if (!responseWrapper .isContextSaved ()) {
143
- responseWrapper .saveContext (context );
144
- }
139
+ responseWrapper .saveContext (context );
145
140
}
146
141
147
142
@ Override
@@ -296,6 +291,8 @@ final class SaveToSessionResponseWrapper extends SaveContextOnUpdateOrErrorRespo
296
291
297
292
private final Authentication authBeforeExecution ;
298
293
294
+ private boolean isSaveContextInvoked ;
295
+
299
296
/**
300
297
* Takes the parameters required to call <code>saveContext()</code> successfully
301
298
* in addition to the request and the response object we are wrapping.
@@ -339,6 +336,7 @@ protected void saveContext(SecurityContext context) {
339
336
// SEC-1587 A non-anonymous context may still be in the session
340
337
// SEC-1735 remove if the contextBeforeExecution was not anonymous
341
338
httpSession .removeAttribute (springSecurityContextKey );
339
+ this .isSaveContextInvoked = true ;
342
340
}
343
341
if (this .logger .isDebugEnabled ()) {
344
342
if (authentication == null ) {
@@ -358,6 +356,7 @@ protected void saveContext(SecurityContext context) {
358
356
// is set SEC-1561
359
357
if (contextChanged (context ) || httpSession .getAttribute (springSecurityContextKey ) == null ) {
360
358
httpSession .setAttribute (springSecurityContextKey , context );
359
+ this .isSaveContextInvoked = true ;
361
360
if (this .logger .isDebugEnabled ()) {
362
361
this .logger .debug (LogMessage .format ("Stored %s to HttpSession [%s]" , context , httpSession ));
363
362
}
@@ -366,7 +365,8 @@ protected void saveContext(SecurityContext context) {
366
365
}
367
366
368
367
private boolean contextChanged (SecurityContext context ) {
369
- return context != this .contextBeforeExecution || context .getAuthentication () != this .authBeforeExecution ;
368
+ return this .isSaveContextInvoked || context != this .contextBeforeExecution
369
+ || context .getAuthentication () != this .authBeforeExecution ;
370
370
}
371
371
372
372
private HttpSession createNewSessionIfAllowed (SecurityContext context ) {
0 commit comments