49
49
import org .springframework .security .core .SpringSecurityMessageSource ;
50
50
import org .springframework .security .core .context .SecurityContext ;
51
51
import org .springframework .security .core .context .SecurityContextHolder ;
52
+ import org .springframework .security .core .context .SecurityContextHolderStrategy ;
52
53
import org .springframework .security .core .userdetails .UserDetails ;
53
54
import org .springframework .security .core .userdetails .UserDetailsChecker ;
54
55
import org .springframework .security .core .userdetails .UserDetailsService ;
@@ -114,6 +115,9 @@ public class SwitchUserFilter extends GenericFilterBean implements ApplicationEv
114
115
115
116
public static final String ROLE_PREVIOUS_ADMINISTRATOR = "ROLE_PREVIOUS_ADMINISTRATOR" ;
116
117
118
+ private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
119
+ .getContextHolderStrategy ();
120
+
117
121
private ApplicationEventPublisher eventPublisher ;
118
122
119
123
private AuthenticationDetailsSource <HttpServletRequest , ?> authenticationDetailsSource = new WebAuthenticationDetailsSource ();
@@ -175,9 +179,9 @@ private void doFilter(HttpServletRequest request, HttpServletResponse response,
175
179
try {
176
180
Authentication targetUser = attemptSwitchUser (request );
177
181
// update the current context to the new target user
178
- SecurityContext context = SecurityContextHolder .createEmptyContext ();
182
+ SecurityContext context = this . securityContextHolderStrategy .createEmptyContext ();
179
183
context .setAuthentication (targetUser );
180
- SecurityContextHolder .setContext (context );
184
+ this . securityContextHolderStrategy .setContext (context );
181
185
this .logger .debug (LogMessage .format ("Set SecurityContextHolder to %s" , targetUser ));
182
186
// redirect to target url
183
187
this .successHandler .onAuthenticationSuccess (request , response , targetUser );
@@ -192,9 +196,9 @@ private void doFilter(HttpServletRequest request, HttpServletResponse response,
192
196
// get the original authentication object (if exists)
193
197
Authentication originalUser = attemptExitUser (request );
194
198
// update the current context back to the original user
195
- SecurityContext context = SecurityContextHolder .createEmptyContext ();
199
+ SecurityContext context = this . securityContextHolderStrategy .createEmptyContext ();
196
200
context .setAuthentication (originalUser );
197
- SecurityContextHolder .setContext (context );
201
+ this . securityContextHolderStrategy .setContext (context );
198
202
this .logger .debug (LogMessage .format ("Set SecurityContextHolder to %s" , originalUser ));
199
203
// redirect to target url
200
204
this .successHandler .onAuthenticationSuccess (request , response , originalUser );
@@ -228,7 +232,7 @@ protected Authentication attemptSwitchUser(HttpServletRequest request) throws Au
228
232
// publish event
229
233
if (this .eventPublisher != null ) {
230
234
this .eventPublisher .publishEvent (new AuthenticationSwitchUserEvent (
231
- SecurityContextHolder .getContext ().getAuthentication (), targetUser ));
235
+ this . securityContextHolderStrategy .getContext ().getAuthentication (), targetUser ));
232
236
}
233
237
return targetUserRequest ;
234
238
}
@@ -244,7 +248,7 @@ protected Authentication attemptSwitchUser(HttpServletRequest request) throws Au
244
248
protected Authentication attemptExitUser (HttpServletRequest request )
245
249
throws AuthenticationCredentialsNotFoundException {
246
250
// need to check to see if the current user has a SwitchUserGrantedAuthority
247
- Authentication current = SecurityContextHolder .getContext ().getAuthentication ();
251
+ Authentication current = this . securityContextHolderStrategy .getContext ().getAuthentication ();
248
252
if (current == null ) {
249
253
throw new AuthenticationCredentialsNotFoundException (this .messages
250
254
.getMessage ("SwitchUserFilter.noCurrentUser" , "No current user associated with this request" ));
@@ -310,7 +314,7 @@ private Authentication getCurrentAuthentication(HttpServletRequest request) {
310
314
return attemptExitUser (request );
311
315
}
312
316
catch (AuthenticationCredentialsNotFoundException ex ) {
313
- return SecurityContextHolder .getContext ().getAuthentication ();
317
+ return this . securityContextHolderStrategy .getContext ().getAuthentication ();
314
318
}
315
319
}
316
320
@@ -510,6 +514,17 @@ public void setSwitchAuthorityRole(String switchAuthorityRole) {
510
514
this .switchAuthorityRole = switchAuthorityRole ;
511
515
}
512
516
517
+ /**
518
+ * Sets the {@link SecurityContextHolderStrategy} to use. The default action is to use
519
+ * the {@link SecurityContextHolderStrategy} stored in {@link SecurityContextHolder}.
520
+ *
521
+ * @since 5.8
522
+ */
523
+ public void setSecurityContextHolderStrategy (SecurityContextHolderStrategy securityContextHolderStrategy ) {
524
+ Assert .notNull (securityContextHolderStrategy , "securityContextHolderStrategy cannot be null" );
525
+ this .securityContextHolderStrategy = securityContextHolderStrategy ;
526
+ }
527
+
513
528
private static RequestMatcher createMatcher (String pattern ) {
514
529
return new AntPathRequestMatcher (pattern , "POST" , true , new UrlPathHelper ());
515
530
}
0 commit comments