joel
joel

Reputation: 103

Modify Log Request Payload in Spring Boot

How can we modify and log request payload in Spring-boot?

I have already implemented CommonsRequestLoggingFilter and am able to log the payload but want to modify the request before being logged so I can hide some sensitive data in the log.

Upvotes: 5

Views: 2734

Answers (1)

daltonfury42
daltonfury42

Reputation: 3762

CommonsRequestLoggingFilter inherits from AbstractRequestLoggingFilter which allows you to supply a predicate that should return true in order to log the error.

In my case, I wanted to log all except the authorization header:

loggingFilter.setHeaderPredicate(header -> !header.toLowerCase().equals("authorization"));

This will result in the logger masking the auth header:

... headers=[authorization:"masked", ...]

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/filter/AbstractRequestLoggingFilter.html#setHeaderPredicate-java.util.function.Predicate-

Upvotes: 7

Related Questions