我已经创建了一个oauth承载令牌创建者,但是在我检查有效性的同时,我需要授权标头来提取令牌,为此我在下面的代码中编写了代码,但是auth标头始终为null,这是什么问题?
@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);
}
}
注意:我确定我是在邮递员的标头中发送令牌的,但是以某种方式无法从httpServlet中获取令牌。