@@ -158,8 +158,12 @@ public SwitchUserWebFilter(ReactiveUserDetailsService userDetailsService, String
158158 public Mono <Void > filter (ServerWebExchange exchange , WebFilterChain chain ) {
159159 final WebFilterExchange webFilterExchange = new WebFilterExchange (exchange , chain );
160160 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 ))
163167 .onErrorResume (SwitchUserAuthenticationException .class , (exception ) -> Mono .empty ());
164168 }
165169
@@ -211,7 +215,7 @@ protected String getUsername(ServerWebExchange exchange) {
211215 @ NonNull
212216 private Mono <Authentication > attemptSwitchUser (Authentication currentAuthentication , String userName ) {
213217 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 ));
215219 return this .userDetailsService .findByUsername (userName )
216220 .switchIfEmpty (Mono .error (this ::noTargetAuthenticationException ))
217221 .doOnNext (this .userDetailsChecker ::check )
@@ -222,7 +226,7 @@ private Mono<Authentication> attemptSwitchUser(Authentication currentAuthenticat
222226 private Authentication attemptExitUser (Authentication currentAuthentication ) {
223227 Optional <Authentication > sourceAuthentication = extractSourceAuthentication (currentAuthentication );
224228 if (!sourceAuthentication .isPresent ()) {
225- this .logger .debug ("Could not find original user Authentication object! " );
229+ this .logger .debug ("Failed to find original user" );
226230 throw noOriginalAuthenticationException ();
227231 }
228232 return sourceAuthentication .get ();
@@ -232,13 +236,14 @@ private Mono<Void> onAuthenticationSuccess(Authentication authentication, WebFil
232236 ServerWebExchange exchange = webFilterExchange .getExchange ();
233237 SecurityContextImpl securityContext = new SecurityContextImpl (authentication );
234238 return this .securityContextRepository .save (exchange , securityContext )
239+ .doOnSuccess ((v ) -> this .logger .debug (LogMessage .format ("Switched user to %s" , authentication )))
235240 .then (this .successHandler .onAuthenticationSuccess (webFilterExchange , authentication ))
236241 .subscriberContext (ReactiveSecurityContextHolder .withSecurityContext (Mono .just (securityContext )));
237242 }
238243
239244 private Mono <Void > onAuthenticationFailure (AuthenticationException exception , WebFilterExchange webFilterExchange ) {
240245 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 );
242247 return Mono .error (exception );
243248 })).flatMap ((failureHandler ) -> failureHandler .onAuthenticationFailure (webFilterExchange , exception ));
244249 }
@@ -247,7 +252,7 @@ private Authentication createSwitchUserToken(UserDetails targetUser, Authenticat
247252 Optional <Authentication > sourceAuthentication = extractSourceAuthentication (currentAuthentication );
248253 if (sourceAuthentication .isPresent ()) {
249254 // SEC-1763. Check first if we are already switched.
250- this .logger .info (
255+ this .logger .debug (
251256 LogMessage .format ("Found original switch user granted authority [%s]" , sourceAuthentication .get ()));
252257 currentAuthentication = sourceAuthentication .get ();
253258 }
0 commit comments