1

We are making a spring boot endpoint call from axios. When the call is received in the filter, we are not able to fetch the header information such as "Authorization" from request. Please let us know where we are going wrong. Appreciate your help.

This is the call from VueJS -

axios({
    method: 'post',
    url: <url>,
    data: {},
    headers: {
    'Authorization': 'JWT fefege...',
    'Accept': 'application/json',
    'Content-Type': 'application/json'
    }
})
.then(function (response) {
    console.log("Response:" + JSON.stringify(response));
})
.catch(function (error) {
    alert(error);
});

......... This is the code in Web API filter -

@Component
public class JwtRequestFilter extends OncePerRequestFilter {

    @Autowired
    private JwtUserDetailsService jwtUserDetailsService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

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

    //Prints fine
        System.out.println("Request URI:" + request.getRequestURI());
        System.out.println("CORSFilter HTTP Request: " + request.getMethod());

        String prefix = null;
        Collections.list(request.getHeaderNames()).forEach(headerName ->
                Collections.list(request.getHeaders(headerName)).forEach(headerValue ->
                        System.out.println(prefix + ":" + headerName + ":" + headerValue)));
    
    //Below is printed
        null:host:localhost:8080
    null:user-agent:Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0
    null:accept:*/*
    null:accept-language:en-US,en;q=0.5
    null:accept-encoding:gzip, deflate
    null:access-control-request-method:POST
    null:access-control-request-headers:access-control-allow-origin,authorization,content-type
    null:referer:http://localhost:1337/
    null:origin:http://localhost:1337
    null:connection:keep-alive

                        

        final String requestTestHeader = request.getHeader("access-control-request-headers");
        System.out.println("requestTestHeader:" + requestTestHeader);
        //Prints access-control-allow-origin,authorization,content-type

        final String requestTokenHeader = request.getHeader("Authorization");
        System.out.println("TokenHeader:" + requestTokenHeader);
        //Above prints NULL
        
        ......
        
    }

}
Krishnan V S
  • 1,124
  • 1
  • 13
  • 31
  • Please see if this solves your problem: https://stackoverflow.com/a/52292612/14072498. You should also test with Postman when facing issues like this. BR – Roar S. Feb 21 '21 at 12:03
  • Thanks for your reply. I tried that but not working. When I call the same endpoint from an Android app, it works fine, so I am sure that it will work with Postman too. Most likely something to do with the way we are setting the headers in axios but not able to figure out. – Krishnan V S Feb 21 '21 at 14:32

0 Answers0