x-forwarded-for header missing in http request while it going through AWS LoadBalancer to spring boot application

303 views Asked by At

I have spring boot application. Running this spring boot application behind proxy i.e AWS Loadbalancer.

In that there is one endpoint

@GetMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Object getTestResponse(@RequestHeader("id") String id, @RequestHeader(required = true, value = "x-forwarded-for") String clientIpAddress,) throws IOException {
//Other logic goes here
        
}

Application running without any error in aws cloud.

If I try to access the endpoint "\test" with required request payload and header.

curl -v -H 'id:12345678' -H 'x-forwarded-for:192.168.0.2' http://example.com/context/services/test

I am getting the x-forwarded-for missing error, even though it passed while I am calling the endpoint.

.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingRequestHeaderException: Required request header 'x-forwarded-for' for method parameter type String is not present

After the research, I have done changes in spring boot application and AWS configuration

  1. Spring boot application change

    To receive this x-forwarded-for header from ALB to servlet(Tomcat) container, Added below property in application.yml

    server:
    
     forward-headers-strategy: NATIVE
    

    As metioned spring boot documentation - Doc link

  2. AWS Load Balancer

    There is a X-Forwarded-For header atrribute in load balancer. I change it value to Preserve . Because in my cause I need to preserve the x-forwarded-for header from client request.

    AWS Document for your reference regarding X-Forwarded-For header atrribute values. Such as Append, Preserve, Remove.

I don't know what I am missing here, after above change also I am getting x-forwarded-for header missing error in spring boot application log

Kindly check and help me to solve this issue , Feel free to share your valuable thoughts.

Thanks,

0

There are 0 answers