diff --git a/src/main/java/cn/chjyj/szwh/Interceptor/ChInterceptor.java b/src/main/java/cn/chjyj/szwh/Interceptor/ChInterceptor.java index 58ae5bc..f0a1d3d 100644 --- a/src/main/java/cn/chjyj/szwh/Interceptor/ChInterceptor.java +++ b/src/main/java/cn/chjyj/szwh/Interceptor/ChInterceptor.java @@ -2,7 +2,10 @@ package cn.chjyj.szwh.Interceptor; import cn.chjyj.szwh.annotation.PassToken; +import cn.chjyj.szwh.bean.Admin; import cn.chjyj.szwh.exception.ChException; +import cn.chjyj.szwh.service.AdminService; +import cn.chjyj.szwh.service.impl.AdminServiceImpl; import cn.chjyj.szwh.utils.JwtUtils; import com.auth0.jwt.JWT; import com.auth0.jwt.interfaces.DecodedJWT; @@ -45,29 +48,26 @@ public class ChInterceptor implements HandlerInterceptor { } DecodedJWT decode = JWT.decode(token); - //获取到里面存在的用户id + // todo 1 验证是否过期 + + //2 验证是否 String userId = decode.getClaim("userid").asString(); +// //获取载荷内容 +// String userName = JwtUtils.getClaimByName(token, "userName").asString(); +// String realName = JwtUtils.getClaimByName(token, "realName").asString(); //找找看是否有这个user 因为我们需要检查用户是否存在,读者可以自行修改逻辑 - // todo -// AccountDTO user = accountService.getByUserName(userId); -// -// if (user == null) { -// //这个错误也是我自定义的 -// throw new ChException("用户不存在"); -// } - - // 验证 token - JwtUtils.verifyToken(token, userId); - - //获取载荷内容 - String userName = JwtUtils.getClaimByName(token, "userName").asString(); - String realName = JwtUtils.getClaimByName(token, "realName").asString(); - - //放入attribute以便后面调用 - request.setAttribute("userName", userName); - request.setAttribute("realName", realName); + AdminService adminService = new AdminServiceImpl(); + Admin admin = adminService.getAdminByAccountId(userId); + if (admin == null) { + //这个错误也是我自定义的 + throw new ChException("用户不存在"); + }else{ + //放入attribute以便后面调用 + request.setAttribute("uid", admin.getId()); + request.setAttribute("acount_name",admin.getAccountName()); //登录账号 + } return true; } return true;