java - 从标头获取访问令牌
问题描述
我已经创建了一个 oauth Bearer 令牌创建者,但是当我检查有效性时,我需要授权标头来提取令牌,我在下面编写了代码,但是 auth 标头总是为空,这是什么问题?
@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 获取。
解决方案
推荐阅读
- wpf - 如何删除 WPF 功能区组上的鼠标悬停效果?
- python - 彩色日志,但不基于日志级别
- scala - scala - 如何从另一个类访问案例类方法?
- python - Python SSH 作为 sudoer 连接
- javascript - Javascript 过滤器在 IE 上不起作用
- python - 从列表中将值添加到数据库中,该列表的值对应于 python 中的另一个数据库
- neo4j - 如何使用 gremlin 控制台或服务器获取 neo4j 3.4.5 的插件?
- javascript - 为什么当我使用 AJAX 重新加载页面时数据消失了
- magento2 - $product->save() 在 magento 2.1.3 中保存一个简单的产品需要 4 秒
- javascript - Javascript:计算三角形、圆形或矩形面积的 3 个函数返回未定义