/**
* token过滤器 验证token有效性
*
*/
@Component
@Slf4j
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
{
@Autowired
private TokenService tokenService;
@SneakyThrows
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException
{
LoginUser loginUser = tokenService.getLoginUser(request);
String msg = StringUtils.format("请求路径:{}", request.getRequestURI());
log.info(msg);
if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication()))
{
tokenService.verifyToken(loginUser);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
chain.doFilter(request, response);
}
}
Token过滤器,验证Token
最新推荐文章于 2024-10-16 21:43:36 发布
该博客介绍了一个用于验证JWT(JSON Web Tokens)有效性的过滤器组件。该过滤器在每次请求时检查请求头中的token,通过调用TokenService获取LoginUser对象。如果token有效并且用户尚未认证,过滤器会进行认证并设置SecurityContextHolder上下文。这确保了只有经过验证的用户可以访问受保护的资源。
3万+

被折叠的 条评论
为什么被折叠?



