|
|
|
@ -9,7 +9,10 @@ import cn.chjyj.szwh.service.impl.AdminServiceImpl; |
|
|
|
import cn.chjyj.szwh.utils.JwtUtils; |
|
|
|
import com.auth0.jwt.JWT; |
|
|
|
import com.auth0.jwt.interfaces.DecodedJWT; |
|
|
|
import jdk.internal.instrumentation.Logger; |
|
|
|
import org.apache.commons.lang.StringUtils; |
|
|
|
import org.apache.commons.logging.Log; |
|
|
|
import org.apache.commons.logging.LogFactory; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.web.method.HandlerMethod; |
|
|
|
import org.springframework.web.servlet.HandlerInterceptor; |
|
|
|
@ -21,8 +24,7 @@ import java.lang.reflect.Method; |
|
|
|
|
|
|
|
@Component |
|
|
|
public class ChInterceptor implements HandlerInterceptor { |
|
|
|
|
|
|
|
|
|
|
|
private static Log log = LogFactory.getLog(ChInterceptor.class); |
|
|
|
@Override |
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
|
|
|
// 从请求头中获取token
|
|
|
|
@ -44,30 +46,33 @@ public class ChInterceptor implements HandlerInterceptor { |
|
|
|
else { |
|
|
|
// 执行认证
|
|
|
|
if (token == null) { |
|
|
|
throw new ChException("请登录重试"); |
|
|
|
} |
|
|
|
throw new ChException("token为空,token为必须参数"); |
|
|
|
}else{ |
|
|
|
|
|
|
|
} |
|
|
|
//log.info("token:"+token);
|
|
|
|
DecodedJWT decode = JWT.decode(token); |
|
|
|
// todo 1 验证是否过期
|
|
|
|
|
|
|
|
//2 验证是否
|
|
|
|
String userId = decode.getClaim("userid").asString(); |
|
|
|
String payload=decode.getPayload(); |
|
|
|
request.setAttribute("pstr",payload); |
|
|
|
// String userId = decode.getClaim("userid").asString();
|
|
|
|
// //获取载荷内容
|
|
|
|
// String userName = JwtUtils.getClaimByName(token, "userName").asString();
|
|
|
|
// String realName = JwtUtils.getClaimByName(token, "realName").asString();
|
|
|
|
|
|
|
|
//找找看是否有这个user 因为我们需要检查用户是否存在,读者可以自行修改逻辑
|
|
|
|
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()); //登录账号
|
|
|
|
} |
|
|
|
// 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; |
|
|
|
|