34
34
import org .springframework .context .MessageSource ;
35
35
import org .springframework .context .MessageSourceAware ;
36
36
import org .springframework .context .support .MessageSourceAccessor ;
37
+ import org .springframework .core .log .LogMessage ;
37
38
import org .springframework .security .authentication .AccountExpiredException ;
38
39
import org .springframework .security .authentication .AccountStatusUserDetailsChecker ;
39
40
import org .springframework .security .authentication .AuthenticationCredentialsNotFoundException ;
46
47
import org .springframework .security .core .AuthenticationException ;
47
48
import org .springframework .security .core .GrantedAuthority ;
48
49
import org .springframework .security .core .SpringSecurityMessageSource ;
50
+ import org .springframework .security .core .context .SecurityContext ;
49
51
import org .springframework .security .core .context .SecurityContextHolder ;
50
52
import org .springframework .security .core .userdetails .UserDetails ;
51
53
import org .springframework .security .core .userdetails .UserDetailsChecker ;
@@ -171,8 +173,10 @@ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
171
173
Authentication targetUser = attemptSwitchUser (request );
172
174
173
175
// update the current context to the new target user
174
- SecurityContextHolder .getContext ().setAuthentication (targetUser );
175
-
176
+ SecurityContext context = SecurityContextHolder .createEmptyContext ();
177
+ context .setAuthentication (targetUser );
178
+ SecurityContextHolder .setContext (context );
179
+ this .logger .debug (LogMessage .format ("Set SecurityContextHolder to %s" , targetUser ));
176
180
// redirect to target url
177
181
this .successHandler .onAuthenticationSuccess (request , response ,
178
182
targetUser );
@@ -189,14 +193,17 @@ else if (requiresExitUser(request)) {
189
193
Authentication originalUser = attemptExitUser (request );
190
194
191
195
// update the current context back to the original user
192
- SecurityContextHolder .getContext ().setAuthentication (originalUser );
193
-
196
+ SecurityContext context = SecurityContextHolder .createEmptyContext ();
197
+ context .setAuthentication (originalUser );
198
+ SecurityContextHolder .setContext (context );
199
+ this .logger .debug (LogMessage .format ("Set SecurityContextHolder to %s" , originalUser ));
194
200
// redirect to target url
195
201
this .successHandler .onAuthenticationSuccess (request , response , originalUser );
196
202
197
203
return ;
198
204
}
199
-
205
+ this .logger .trace (LogMessage .format ("Did not attempt to switch user since request did not match [%s] or [%s]" ,
206
+ this .switchUserMatcher , this .exitUserMatcher ));
200
207
chain .doFilter (request , response );
201
208
}
202
209
@@ -218,25 +225,13 @@ protected Authentication attemptSwitchUser(HttpServletRequest request)
218
225
UsernamePasswordAuthenticationToken targetUserRequest ;
219
226
220
227
String username = request .getParameter (this .usernameParameter );
221
-
222
- if (username == null ) {
223
- username = "" ;
224
- }
225
-
226
- if (this .logger .isDebugEnabled ()) {
227
- this .logger .debug ("Attempt to switch to user [" + username + "]" );
228
- }
229
-
228
+ username = (username != null ) ? username : "" ;
229
+ this .logger .debug (LogMessage .format ("Attempting to switch to user [%s]" , username ));
230
230
UserDetails targetUser = this .userDetailsService .loadUserByUsername (username );
231
231
this .userDetailsChecker .check (targetUser );
232
232
233
233
// OK, create the switch user token
234
234
targetUserRequest = createSwitchUserToken (request , targetUser );
235
-
236
- if (this .logger .isDebugEnabled ()) {
237
- this .logger .debug ("Switch User Token [" + targetUserRequest + "]" );
238
- }
239
-
240
235
// publish event
241
236
if (this .eventPublisher != null ) {
242
237
this .eventPublisher .publishEvent (new AuthenticationSwitchUserEvent (
@@ -273,10 +268,9 @@ protected Authentication attemptExitUser(HttpServletRequest request)
273
268
Authentication original = getSourceAuthentication (current );
274
269
275
270
if (original == null ) {
276
- this .logger .debug ("Could not find original user Authentication object!" );
277
- throw new AuthenticationCredentialsNotFoundException (
278
- this .messages .getMessage ("SwitchUserFilter.noOriginalAuthentication" ,
279
- "Could not find original Authentication object" ));
271
+ this .logger .debug ("Failed to find original user" );
272
+ throw new AuthenticationCredentialsNotFoundException (this .messages
273
+ .getMessage ("SwitchUserFilter.noOriginalAuthentication" , "Failed to find original user" ));
280
274
}
281
275
282
276
// get the source user details
@@ -373,8 +367,7 @@ private Authentication getSourceAuthentication(Authentication current) {
373
367
// check for switch user type of authority
374
368
if (auth instanceof SwitchUserGrantedAuthority ) {
375
369
original = ((SwitchUserGrantedAuthority ) auth ).getSource ();
376
- this .logger .debug ("Found original switch user granted authority ["
377
- + original + "]" );
370
+ this .logger .debug (LogMessage .format ("Found original switch user granted authority [%s]" , original ));
378
371
}
379
372
}
380
373
0 commit comments