getting access token from header

Viewed 79

i have created an oauth bearer token creator but while i check the validity i need authorization header to extract the token in this purpose i wrote below code but the auth header always gets null what is the problem?

@Component
public class JwtTokenFilter extends OncePerRequestFilter {


    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest,
                                    @NotNull HttpServletResponse httpServletResponse,
                                    @NotNull FilterChain filterChain) throws ServletException, IOException {

        /**
         * We are parsing our token in two pieces and we process the second
         * "Bearer hvs231asas2355"
         */
        final String authHeader = httpServletRequest.getHeader("Authorization");  // this part always gets null
        String afId = null;
        String token = null;



        if (authHeader != null && authHeader.contains("Bearer")) {
            token = authHeader.substring(7);

            afId = tokenManager.getUsernameToken(token);


        }

        if (afId != null && SecurityContextHolder.getContext().getAuthentication() == null) {
            if (tokenManager.tokenValidate(token)) {
                AnonymousAuthenticationToken anonymousAuthenticationToken=new AnonymousAuthenticationToken(token,null,new ArrayList<>());
                authenticationManager.authenticate(anonymousAuthenticationToken);
                anonymousAuthenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));

                SecurityContextHolder.getContext().setAuthentication(anonymousAuthenticationToken);


            }
        }

        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }
}

NOTE: I am sure that i send token in header from postman but somehow it can not be get from httpServlet.

0 Answers
Related