@@ -158,8 +158,12 @@ public SwitchUserWebFilter(ReactiveUserDetailsService userDetailsService, String
158
158
public Mono <Void > filter (ServerWebExchange exchange , WebFilterChain chain ) {
159
159
final WebFilterExchange webFilterExchange = new WebFilterExchange (exchange , chain );
160
160
return switchUser (webFilterExchange ).switchIfEmpty (Mono .defer (() -> exitSwitchUser (webFilterExchange )))
161
- .switchIfEmpty (Mono .defer (() -> chain .filter (exchange ).then (Mono .empty ())))
162
- .flatMap ((authentication ) -> onAuthenticationSuccess (authentication , webFilterExchange ))
161
+ .switchIfEmpty (Mono .defer (() -> {
162
+ this .logger .trace (
163
+ LogMessage .format ("Did not attempt to switch user since request did not match [%s] or [%s]" ,
164
+ this .switchUserMatcher , this .exitUserMatcher ));
165
+ return chain .filter (exchange ).then (Mono .empty ());
166
+ })).flatMap ((authentication ) -> onAuthenticationSuccess (authentication , webFilterExchange ))
163
167
.onErrorResume (SwitchUserAuthenticationException .class , (exception ) -> Mono .empty ());
164
168
}
165
169
@@ -211,7 +215,7 @@ protected String getUsername(ServerWebExchange exchange) {
211
215
@ NonNull
212
216
private Mono <Authentication > attemptSwitchUser (Authentication currentAuthentication , String userName ) {
213
217
Assert .notNull (userName , "The userName can not be null." );
214
- this .logger .debug (LogMessage .format ("Attempt to switch to user [%s]" , userName ));
218
+ this .logger .debug (LogMessage .format ("Attempting to switch to user [%s]" , userName ));
215
219
return this .userDetailsService .findByUsername (userName )
216
220
.switchIfEmpty (Mono .error (this ::noTargetAuthenticationException ))
217
221
.doOnNext (this .userDetailsChecker ::check )
@@ -222,7 +226,7 @@ private Mono<Authentication> attemptSwitchUser(Authentication currentAuthenticat
222
226
private Authentication attemptExitUser (Authentication currentAuthentication ) {
223
227
Optional <Authentication > sourceAuthentication = extractSourceAuthentication (currentAuthentication );
224
228
if (!sourceAuthentication .isPresent ()) {
225
- this .logger .debug ("Could not find original user Authentication object! " );
229
+ this .logger .debug ("Failed to find original user" );
226
230
throw noOriginalAuthenticationException ();
227
231
}
228
232
return sourceAuthentication .get ();
@@ -232,13 +236,14 @@ private Mono<Void> onAuthenticationSuccess(Authentication authentication, WebFil
232
236
ServerWebExchange exchange = webFilterExchange .getExchange ();
233
237
SecurityContextImpl securityContext = new SecurityContextImpl (authentication );
234
238
return this .securityContextRepository .save (exchange , securityContext )
239
+ .doOnSuccess ((v ) -> this .logger .debug (LogMessage .format ("Switched user to %s" , authentication )))
235
240
.then (this .successHandler .onAuthenticationSuccess (webFilterExchange , authentication ))
236
241
.subscriberContext (ReactiveSecurityContextHolder .withSecurityContext (Mono .just (securityContext )));
237
242
}
238
243
239
244
private Mono <Void > onAuthenticationFailure (AuthenticationException exception , WebFilterExchange webFilterExchange ) {
240
245
return Mono .justOrEmpty (this .failureHandler ).switchIfEmpty (Mono .defer (() -> {
241
- this .logger .error ( "Switch User failed " , exception );
246
+ this .logger .debug ( "Failed to switch user " , exception );
242
247
return Mono .error (exception );
243
248
})).flatMap ((failureHandler ) -> failureHandler .onAuthenticationFailure (webFilterExchange , exception ));
244
249
}
@@ -247,7 +252,7 @@ private Authentication createSwitchUserToken(UserDetails targetUser, Authenticat
247
252
Optional <Authentication > sourceAuthentication = extractSourceAuthentication (currentAuthentication );
248
253
if (sourceAuthentication .isPresent ()) {
249
254
// SEC-1763. Check first if we are already switched.
250
- this .logger .info (
255
+ this .logger .debug (
251
256
LogMessage .format ("Found original switch user granted authority [%s]" , sourceAuthentication .get ()));
252
257
currentAuthentication = sourceAuthentication .get ();
253
258
}
0 commit comments