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
......
}
}