2.定义拦截规则
@Componentpublic class TokenInterceptor implements HandlerInterceptor {
@Autowired
private RedisUtil redisUtil;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String token = Optional.ofNullable(request.getHeader("Authorization")).orElse("");
String client = Optional.ofNullable(request.getHeader("XLClinet")).orElse("");
System.out.println(client + " " + request.getRequestURI());
if ("".equals(token)) {
return true;
}
//验证token
if (redisUtil.verifyTokenRedis(token, request)) {
return true;
} else {
JsonResponse.jsonWriteApp(response, Integer.toString(HttpStatusEnum.UNAUTHORIZED.value()),
BaseConstant.MESSAGE_ERROR_TOKEN_MISSING, StrUtil.toString(request.getHeader("deviceFlag")));
return false;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
}