Desired behaviour:
There is a Spring Boot Application that has a Controller which hosts an endpoint which will retrieve data (via HTTP call) through a separate service. However, it will need to pass along the Bearer token from the original headers in the original request.
Current Implementation:
A. There is a GenericFilter which will save the "Authorization" header into a bean instance.
@Component
public class exampleFilter extends OncePerRequestFilter {
@Autowired
private POJO pojo;
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
FilterChain filterChain) throws ServletException, IOException {
pojo.setAuthorization(httpServletRequest.getHeader("Authorization"));
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
}
Issue:
Since the Authorization value is stored in a bean, it is not thread safe.
From online research, the pattern is to use a method variable in an HTTPServlet class' "doGet()" or "service()" method; however, this has not worked correctly for this issue.
Is that pattern still the right solution for this problem ? any suggestions on what should be ?
Thanks in advance!