Skip to content

Commit

Permalink
AccessLogFilter: clear MDC after logging
Browse files Browse the repository at this point in the history
  • Loading branch information
groldan authored and pmauduit committed Mar 26, 2024
1 parent ef3e6c9 commit 08d6b84
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class FiltersAutoConfiguration {
return new ResolveTargetGlobalFilter(config);
}


/**
* Custom gateway predicate factory to support matching by regular expressions
* on both name and value of query parameters
Expand All @@ -68,6 +67,7 @@ public class FiltersAutoConfiguration {
public @Bean StripBasePathGatewayFilterFactory stripBasePathGatewayFilterFactory() {
return new StripBasePathGatewayFilterFactory();
}

@Bean
RequestIdGlobalFilter requestIdGlobalFilter() {
return new RequestIdGlobalFilter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,34 @@ private void doLog(Principal principal, ServerWebExchange exchange) {
InetSocketAddress addr = request.getRemoteAddress();
String remoteAddress = addr == null ? "unknown" : addr.toString();

MDC.put("route-id", route.getId());
MDC.put("route-uri", String.valueOf(routeUri));
MDC.put(RequestIdGlobalFilter.REQUEST_ID_HEADER, requestId);
MDC.put("remoteAddress", remoteAddress);
mdcPut("route-id", route.getId());
mdcPut("route-uri", String.valueOf(routeUri));
mdcPut(RequestIdGlobalFilter.REQUEST_ID_HEADER, requestId);
mdcPut("remoteAddress", remoteAddress);

Optional<GeorchestraUser> user = GeorchestraUsers.resolve(exchange);
user.ifPresentOrElse(gsu -> {
MDC.put("auth-user", gsu.getUsername());
MDC.put("auth-roles", gsu.getRoles().stream().collect(Collectors.joining(", ")));
mdcPut("auth-user", gsu.getUsername());
mdcPut("auth-roles", gsu.getRoles().stream().collect(Collectors.joining(", ")));
}, () -> MDC.put("auth-user", "anonymous"));

if (principal instanceof Authentication && principal != ANNON) {
String roles = ((Authentication) principal).getAuthorities().stream().map(GrantedAuthority::getAuthority)
.collect(Collectors.joining(", "));

MDC.put("principal-name", principal.getName());
MDC.put("principal-authorities", roles);
mdcPut("principal-name", principal.getName());
mdcPut("principal-authorities", roles);
}

log.info("{} {} {} ", request.getMethodValue(), response.getRawStatusCode(), uri);
mdcClear();
}

void mdcPut(String key, String value) {
MDC.put(key, value);
}

void mdcClear() {
MDC.clear();
}
}
1 change: 0 additions & 1 deletion gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ logging:
'[org.springframework.cloud.gateway]': info
'[org.springframework.security]': info
'[org.springframework.security.oauth2]': debug
'[reactor.netty.http ]': debug
'[org.georchestra.gateway]': info
'[org.georchestra.gateway.accesslog]': info
'[org.georchestra.gateway.filter.headers]': debug
Expand Down

0 comments on commit 08d6b84

Please sign in to comment.