// 判断是否存在令牌信息,如果存在,则允许登录
String accessToken = request.getHeader(“Authorization”);
if (null == accessToken) {
throw new CommonException(401, “无token,请重新登录”);
} else {
// 从Redis 中查看 token 是否过期
Claims claims;
try{
claims = TokenUtils.parseJWT(accessToken);
}catch (ExpiredJwtException e){
response.setStatus(401);
throw new CommonException(401, “token失效,请重新登录”);
}catch (SignatureException se){
response.setStatus(401);
throw new CommonException(401, “token令牌错误”);
}
String userName = claims.getId();
UserBase user = userBaseService.findUserByAccount(userName);
if (user == null) {
response.setStatus(401);
throw new CommonException(401, “用户不存在,请重新登录”);
}
// 当前登录用户@CurrentUser
request.setAttribute(CurrentUserConstants.CURRENT_USER, user);
return true;
}
} else {//不需要登录可请求
return true;
}
}
// 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
// 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
/**
-
@BelongsProject:
-
@BelongsPackage: com.jdtaste.jdtastesso.web.intercepter.auth
-
@Author:
-
@CreateTime: 2018-