1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
@Slf4j @Configuration public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) { System.out.println("OPTIONS请求,放行");return true; } String token = request.getHeader("Authorization"); Map<String,Object> map = new HashMap<>(); try { JWTUtils.verify(token); return true; } catch (TokenExpiredException e) { map.put("state", false); map.put("msg", "Token已经过期!!!"); } catch (SignatureVerificationException e){ map.put("state", false); map.put("msg", "签名错误!!!"); } catch (AlgorithmMismatchException e){ map.put("state", false); map.put("msg", "加密算法不匹配!!!"); } catch (Exception e) { e.printStackTrace(); map.put("state", false); map.put("msg", "无效token~~"); } String json = new ObjectMapper().writeValueAsString(map); response.setContentType("application/json;charset=UTF-8"); response.getWriter().println(json); return false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
|