4949import org .springframework .security .core .SpringSecurityMessageSource ;
5050import org .springframework .security .core .context .SecurityContext ;
5151import org .springframework .security .core .context .SecurityContextHolder ;
52+ import org .springframework .security .core .context .SecurityContextHolderStrategy ;
5253import org .springframework .security .core .userdetails .UserDetails ;
5354import org .springframework .security .core .userdetails .UserDetailsChecker ;
5455import org .springframework .security .core .userdetails .UserDetailsService ;
@@ -114,6 +115,9 @@ public class SwitchUserFilter extends GenericFilterBean implements ApplicationEv
114115
115116 public static final String ROLE_PREVIOUS_ADMINISTRATOR = "ROLE_PREVIOUS_ADMINISTRATOR" ;
116117
118+ private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
119+ .getContextHolderStrategy ();
120+
117121 private ApplicationEventPublisher eventPublisher ;
118122
119123 private AuthenticationDetailsSource <HttpServletRequest , ?> authenticationDetailsSource = new WebAuthenticationDetailsSource ();
@@ -175,9 +179,9 @@ private void doFilter(HttpServletRequest request, HttpServletResponse response,
175179 try {
176180 Authentication targetUser = attemptSwitchUser (request );
177181 // update the current context to the new target user
178- SecurityContext context = SecurityContextHolder .createEmptyContext ();
182+ SecurityContext context = this . securityContextHolderStrategy .createEmptyContext ();
179183 context .setAuthentication (targetUser );
180- SecurityContextHolder .setContext (context );
184+ this . securityContextHolderStrategy .setContext (context );
181185 this .logger .debug (LogMessage .format ("Set SecurityContextHolder to %s" , targetUser ));
182186 // redirect to target url
183187 this .successHandler .onAuthenticationSuccess (request , response , targetUser );
@@ -192,9 +196,9 @@ private void doFilter(HttpServletRequest request, HttpServletResponse response,
192196 // get the original authentication object (if exists)
193197 Authentication originalUser = attemptExitUser (request );
194198 // update the current context back to the original user
195- SecurityContext context = SecurityContextHolder .createEmptyContext ();
199+ SecurityContext context = this . securityContextHolderStrategy .createEmptyContext ();
196200 context .setAuthentication (originalUser );
197- SecurityContextHolder .setContext (context );
201+ this . securityContextHolderStrategy .setContext (context );
198202 this .logger .debug (LogMessage .format ("Set SecurityContextHolder to %s" , originalUser ));
199203 // redirect to target url
200204 this .successHandler .onAuthenticationSuccess (request , response , originalUser );
@@ -228,7 +232,7 @@ protected Authentication attemptSwitchUser(HttpServletRequest request) throws Au
228232 // publish event
229233 if (this .eventPublisher != null ) {
230234 this .eventPublisher .publishEvent (new AuthenticationSwitchUserEvent (
231- SecurityContextHolder .getContext ().getAuthentication (), targetUser ));
235+ this . securityContextHolderStrategy .getContext ().getAuthentication (), targetUser ));
232236 }
233237 return targetUserRequest ;
234238 }
@@ -244,7 +248,7 @@ protected Authentication attemptSwitchUser(HttpServletRequest request) throws Au
244248 protected Authentication attemptExitUser (HttpServletRequest request )
245249 throws AuthenticationCredentialsNotFoundException {
246250 // 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 ();
248252 if (current == null ) {
249253 throw new AuthenticationCredentialsNotFoundException (this .messages
250254 .getMessage ("SwitchUserFilter.noCurrentUser" , "No current user associated with this request" ));
@@ -310,7 +314,7 @@ private Authentication getCurrentAuthentication(HttpServletRequest request) {
310314 return attemptExitUser (request );
311315 }
312316 catch (AuthenticationCredentialsNotFoundException ex ) {
313- return SecurityContextHolder .getContext ().getAuthentication ();
317+ return this . securityContextHolderStrategy .getContext ().getAuthentication ();
314318 }
315319 }
316320
@@ -510,6 +514,17 @@ public void setSwitchAuthorityRole(String switchAuthorityRole) {
510514 this .switchAuthorityRole = switchAuthorityRole ;
511515 }
512516
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+
513528 private static RequestMatcher createMatcher (String pattern ) {
514529 return new AntPathRequestMatcher (pattern , "POST" , true , new UrlPathHelper ());
515530 }
0 commit comments