简体   繁体   中英

Spring Boot request header return null value

I have a spring boot project which has some Rest APIs in it. I have two custom headers named request_date and tenant respectively.

I am trying to read the value of these headers in an interceptor, but it reads the value for only tenant and returns null for request_date .

Important

  • I use a filter to wrap the request object because I want to read the request body later.
  • There is a filter to add CORS headers.

When I run my project on localhost and debug the code, I am successfully able to read both the headers' values.

However, when I deploy my application in production and make the request using postman or some other client, the request_date header's value is always read as null.

I am not sure what seems to be the problem with this. I am using Spring boot v1.5.10.RELEASE and JDK 1.8

Note:

  • I have tried to rename the header to something like input_date . However, it still reads null.

The following is the relevant code

TestInterceptor

public class TestInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String requestDate = request.getHeader("request_date");
        String tenant = request.getHeader("Tenant");

        /*Perform some checks*/

        return super.preHandle(request, response, handler);
    }
}

CorsFilter

public class ApiCorsFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, OPTIONS, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers," +
                " X-Requested-With, Origin, X-Auth-Token, Tenant, request_date");
        response.addHeader("Access-Control-Expose-Headers", "X-Auth-Token, Content-Disposition");
        chain.doFilter(request, response);
    }
}

RequestCacheFilter

public class RequestCacheFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                                    FilterChain filterChain) throws ServletException, IOException {

        HttpServletRequest req = new RequestWrapper(request);
        String body = ((RequestWrapper) req).getBody();

        /*Do some operation*/

        filterChain.doFilter(req, response);
    }
}

Some network tools can drop headers that contain underscore in it's name. As per this answer underscore is a legal character but it's uncommon and sometimes tools require additional configuration to support it.

Rename your header to requestDate or request-date and see if it helps. If it works without underscore than inspect network route between client and server eg maybe there is a proxy that drops them?

The issue was with the nginx configuration.

I set the underscores_in_headers on; for the server and now it doesn't drop the headers with underscore in their names.

The solution suggested by @Karol Dowbecki works as well. When I renamed my header to requestDate , I was able to read the value successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM