Skip to content

Commit 7b98c2e

Browse files
committed
Restructure SwitchUserFilter Logs
Issue gh-6311
1 parent 77399ee commit 7b98c2e

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

web/src/main/java/org/springframework/security/web/authentication/switchuser/SwitchUserFilter.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ private void doFilter(HttpServletRequest request, HttpServletResponse response,
178178
SecurityContext context = SecurityContextHolder.createEmptyContext();
179179
context.setAuthentication(targetUser);
180180
SecurityContextHolder.setContext(context);
181+
this.logger.debug(LogMessage.format("Set SecurityContextHolder to %s", targetUser));
181182
// redirect to target url
182183
this.successHandler.onAuthenticationSuccess(request, response, targetUser);
183184
}
@@ -194,10 +195,13 @@ private void doFilter(HttpServletRequest request, HttpServletResponse response,
194195
SecurityContext context = SecurityContextHolder.createEmptyContext();
195196
context.setAuthentication(originalUser);
196197
SecurityContextHolder.setContext(context);
198+
this.logger.debug(LogMessage.format("Set SecurityContextHolder to %s", originalUser));
197199
// redirect to target url
198200
this.successHandler.onAuthenticationSuccess(request, response, originalUser);
199201
return;
200202
}
203+
this.logger.trace(LogMessage.format("Did not attempt to switch user since request did not match [%s] or [%s]",
204+
this.switchUserMatcher, this.exitUserMatcher));
201205
chain.doFilter(request, response);
202206
}
203207

@@ -216,12 +220,11 @@ protected Authentication attemptSwitchUser(HttpServletRequest request) throws Au
216220
UsernamePasswordAuthenticationToken targetUserRequest;
217221
String username = request.getParameter(this.usernameParameter);
218222
username = (username != null) ? username : "";
219-
this.logger.debug(LogMessage.format("Attempt to switch to user [%s]", username));
223+
this.logger.debug(LogMessage.format("Attempting to switch to user [%s]", username));
220224
UserDetails targetUser = this.userDetailsService.loadUserByUsername(username);
221225
this.userDetailsChecker.check(targetUser);
222226
// OK, create the switch user token
223227
targetUserRequest = createSwitchUserToken(request, targetUser);
224-
this.logger.debug(LogMessage.format("Switch User Token [%s]", targetUserRequest));
225228
// publish event
226229
if (this.eventPublisher != null) {
227230
this.eventPublisher.publishEvent(new AuthenticationSwitchUserEvent(
@@ -250,9 +253,9 @@ protected Authentication attemptExitUser(HttpServletRequest request)
250253
// if so, get the original source user so we can switch back
251254
Authentication original = getSourceAuthentication(current);
252255
if (original == null) {
253-
this.logger.debug("Could not find original user Authentication object!");
254-
throw new AuthenticationCredentialsNotFoundException(this.messages.getMessage(
255-
"SwitchUserFilter.noOriginalAuthentication", "Could not find original Authentication object"));
256+
this.logger.debug("Failed to find original user");
257+
throw new AuthenticationCredentialsNotFoundException(this.messages
258+
.getMessage("SwitchUserFilter.noOriginalAuthentication", "Failed to find original user"));
256259
}
257260
// get the source user details
258261
UserDetails originalUser = null;
@@ -327,7 +330,7 @@ private Authentication getSourceAuthentication(Authentication current) {
327330
// check for switch user type of authority
328331
if (auth instanceof SwitchUserGrantedAuthority) {
329332
original = ((SwitchUserGrantedAuthority) auth).getSource();
330-
this.logger.debug("Found original switch user granted authority [" + original + "]");
333+
this.logger.debug(LogMessage.format("Found original switch user granted authority [%s]", original));
331334
}
332335
}
333336
return original;

web/src/main/java/org/springframework/security/web/server/authentication/SwitchUserWebFilter.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)