@@ -142,13 +142,7 @@ public void saveContext(SecurityContext context, HttpServletRequest request,
142
142
+ response
143
143
+ ". You must use the HttpRequestResponseHolder.response after invoking loadContext" );
144
144
}
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 );
152
146
}
153
147
154
148
public boolean containsContext (HttpServletRequest request ) {
@@ -305,6 +299,7 @@ final class SaveToSessionResponseWrapper extends
305
299
private final boolean httpSessionExistedAtStartOfRequest ;
306
300
private final SecurityContext contextBeforeExecution ;
307
301
private final Authentication authBeforeExecution ;
302
+ private boolean isSaveContextInvoked ;
308
303
309
304
/**
310
305
* Takes the parameters required to call <code>saveContext()</code> successfully
@@ -355,6 +350,7 @@ protected void saveContext(SecurityContext context) {
355
350
// SEC-1587 A non-anonymous context may still be in the session
356
351
// SEC-1735 remove if the contextBeforeExecution was not anonymous
357
352
httpSession .removeAttribute (springSecurityContextKey );
353
+ this .isSaveContextInvoked = true ;
358
354
}
359
355
return ;
360
356
}
@@ -371,7 +367,7 @@ protected void saveContext(SecurityContext context) {
371
367
if (contextChanged (context )
372
368
|| httpSession .getAttribute (springSecurityContextKey ) == null ) {
373
369
httpSession .setAttribute (springSecurityContextKey , context );
374
-
370
+ this . isSaveContextInvoked = true ;
375
371
if (logger .isDebugEnabled ()) {
376
372
logger .debug ("SecurityContext '" + context
377
373
+ "' stored to HttpSession: '" + httpSession );
@@ -381,7 +377,7 @@ protected void saveContext(SecurityContext context) {
381
377
}
382
378
383
379
private boolean contextChanged (SecurityContext context ) {
384
- return context != contextBeforeExecution
380
+ return this . isSaveContextInvoked || context != contextBeforeExecution
385
381
|| context .getAuthentication () != authBeforeExecution ;
386
382
}
387
383
0 commit comments