java.lang.ClassCastException:class java.lang.String cannot be cast to 一个实体类

报错信息:
在这里插入图片描述
点到出问题的文件,debug

是下面这一句出现的问题
在这里插入图片描述
携带token时,authentication.getPrincipal()打印出来是一个对象
在这里插入图片描述

不携带token时,只是一个字符串
在这里插入图片描述
自然 字符串 不能强转成一个 对象 了

要想不报错,以上这么写在post时必须携带token

不然就加个if的判断,给他放行(不建议)
在这里插入图片描述
知识扩展:

/**
 * 登录校验过滤器   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

JwtAuthenticationTokenFilter 这个登录校验过滤器,对于请求,他会先做有没有携带token判断,如果没有放行(后面还有校验),有的话解析token获取userid,验证是否正确后,正确将获取到的对象 存入SecurityContextHolder。
这也就是为什么上面利用 SecurityContextHolder.getContext().getAuthentication().getPrincipal()
能获取到对象LoginUser了

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值