zalando/logbook

Logs only show header's name but not value if header is added using a Filter in Spring Boot

wrmichaelmak opened this issue · 2 comments

The logs is showing the request's header name but not the request's header value if the header is added using a custom Filter.

Description

It seems like the problem only exists when the header values are obtained using HttpServletRequestWrapper.getHeaders (link) instead of HttpServletRequestWrapper.getHeader. When I change the source code to use HttpServletRequestWrapper.getHeader instead, the logs was correctly showing the header value and name.

Expected Behavior

Logs should be showing header value and header name, if a header named Authorization with value of authorization_token is added using a custom filter, the logs should show Authorization: [XXX] or Authorization: [authorization_token]

Actual Behavior

Logs is only showing header name and empty header value, i.e it shows Authorization: []

Possible Fix

I have made a quick and dirty fix. Apologies in advance, I am still somewhat new to Java so the solution might not be elegant.

    @Override
    public HttpHeaders getHeaders() {
        HttpHeaders headers = HttpHeaders.empty();
        final Enumeration<String> names = getHeaderNames();

        while (names.hasMoreElements()) {
            final String name = names.nextElement();
            headers = headers.update(name,
                    List.of(new String[] {getHeader(name)}));
        }

        return headers;
    }

Steps to Reproduce

  1. Implement a custom filter that adds a header and header value. (example filter that you can copy)
  2. Register custom filter to the top of the chain or before logbook
  3. Run the application!

Context

I am not able to check if the headers I have injected are right.

Your Environment

  • Version used:
    Spring Boot 3.1.5
    Logbook 3.6.0

  • Link to your project:

Hi @wrmichaelmak could you help me reproduce the issue? I followed the steps that you described, but can see the custom header in the request log:

2023-11-13T12:19:35.544+01:00 TRACE 1705222 --- [nio-8080-exec-6] org.zalando.logbook.Logbook              : {"origin":"remote","type":"request","correlation":"d3e8c8c6f596721b","protocol":"HTTP/1.1","remote":"127.0.0.1","method":"GET","uri":"http://localhost:8080/test","host":"localhost","path":"/test","scheme":"http","port":"8080","headers":{"accept":["*/*"],"host":["localhost:8080"],"remote_addr":["127.0.0.1"],"user-agent":["curl/7.81.0"]}}

Hi @kasmarian , I reviewed my code and realised that I did not override the getHeaders method and that caused issues down the line. Apologies for the oversight. I will be closing the issue if that's ok.