Home > Back-end >  Httpclient wiretap logging hide Authorization in log
Httpclient wiretap logging hide Authorization in log

Time:10-03

At the moment I am using

HttpClient httpClient = HttpClient
  .create()
  .wiretap("reactor.netty.http.client.HttpClient", 
    LogLevel.INFO, AdvancedByteBufFormat.TEXTUAL);

to log back the response and request but I would like to hide the header Authorization: from log is it passable?

CodePudding user response:

For logging you can add custom exchange filter to webClient

    public WebClient.Builder webClientBuilder() {
        return WebClient.builder()
                .filter(logResponse())
    }

    private ExchangeFilterFunction logResponse() {
        return ExchangeFilterFunction.ofResponseProcessor(response -> {
                logHeaders(response);
                logBody(response);
        });
    }

    private void logHeaders(ClientResponse response) {
        //here you can decide what kind of header do you need append to logs
        response.headers().asHttpHeaders().forEach((name, values) -> values.forEach(value -> log.debug("{}={}", name, value)));
    }

    private static Mono<ClientResponse> logBody(ClientResponse response) {
       // log body
    }
  • Related