'How to add customer header to Request?
I'm caught in a situation where I need to manually add a header(Authorization
) to the request.
The catch is that I only need to add that header to requests coming to a specific API(Controller
). Not to all.
I'm using Java 1.8 with Spring Boot.
Is there anyway to achieve this?
I want to achieve this : Image description
Solution 1:[1]
This might work for you..
public class MyCustomFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
if(httpServletRequest.getRequestURI().equals("Your custom URI")){
httpServletResponse.setHeader("Authorization","Your value");
}
filterChain.doFilter(httpServletRequest,httpServletResponse);
}
}
For every request, checks the URI and if matches,adds the Authorization header.Also, you will probably need to add @Component annotation.
Solution 2:[2]
I guess that this is the answer that you are looking for:
@RequestMapping(value = "/users", method = RequestMethod.GET)
public List<AppUser> getUsers(OAuth2Authentication auth, @RequestHeader (name="Authorization") String token)
More info here is the reference of this code: https://stackoverflow.com/a/54911059/13842927
I hope that this helped you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tsimpragakis Vasilis |
Solution 2 | Sanady_ |