:class cannot be cast to 一个实体类
/**
* 登录校验过滤器 JwtAuthenticationTokenFilter
*/
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
@Autowired
private RedisCache redisCache;
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
//获取请求头中的token
String token = httpServletRequest.getHeader("token");
if (!StringUtils.hasText(token)){
//说明该接口不需要登录 直接放行
filterChain.doFilter(httpServletRequest,httpServletResponse);
return;
}
//解析获取userId(解密)
Claims claims = null;
try {
claims = JwtUtil.parseJWT(token);
} catch (Exception e) {
//token超时 token非法,
e.printStackTrace();
//响应告诉前端需要重新登录
ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
WebUtils.renderString(httpServletResponse, JSON.toJSONString(result));
return;
}
String userId = claims.getSubject();
//从redis中获取用户信息
LoginUser loginUser = redisCache.getCacheObject("bloglogin:" + userId);
//如果获取不到
//说明登录过期,要重新登录
if (Objects.isNull(loginUser))
{
ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
WebUtils.renderString(httpServletResponse, JSON.toJSONString(result));
return;
}
//存入SecurityContextHolder
//三个参数是认证后状态
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser,null,null);
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
filterChain.doFilter(httpServletRequest,httpServletResponse);
}
}
```java