spring boot 拦截器

Spring boot 拦截器 配置很简单,比Spring Mvc 更加方便配置。

1、实现HandlerInterceptor

所有的拦截器需要实现HandlerInterceptor,可重写preHandle,postHandle,afterCompletion

我这边写了自定义的注解,用来匿名接口。

拦截Controller接口,进行验证出来,false则拦截

 

 1 public class HttpInterceptor implements HandlerInterceptor
 2 {
 3     @Override
 4     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
 5     {
 6         if (handler instanceof HandlerMethod)
 7         {
 8             HandlerMethod handlerMethod = (HandlerMethod) handler;
 9             // 获取方法上的注解
10             AllowAnonymous allowAnonymous = handlerMethod.getMethod().getAnnotation(AllowAnonymous.class);
11             // 如果方法上的注解为空 则获取类的注解
12             if (allowAnonymous == null)
13             {
14                 allowAnonymous = handlerMethod.getMethod().getDeclaringClass().getAnnotation(AllowAnonymous.class);
15             }
16             if (allowAnonymous != null)
17             {
18                 boolean value = allowAnonymous.value();
19                 if (value)
20                 {
21                     System.out.println(String.format("Method:%s,匿名", handlerMethod.getMethod()));
22                     return true;
23                 }
24             }
25             String token = request.getHeader("token");
26             if (token != "123456")
27             {
28                 System.out.println(String.format("Method:%s,验证不成功", handlerMethod.getMethod()));
29                 return false;
30             }
31 
32         }
33         return true;
34     }
35 
36     /**
37      * 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
38      *
39      * @param request
40      * @param response
41      * @param handler
42      * @param modelAndView
43      * @throws Exception
44      */
45     @Override
46     public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
47     {
48         //System.out.println("执行了TestInterceptor的postHandle方法");
49     }
50 
51     /**
52      * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
53      *
54      * @param request
55      * @param response
56      * @param handler
57      * @param ex
58      * @throws Exception
59      */
60     @Override
61     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception
62     {
63         //System.out.println("执行了TestInterceptor的afterCompletion方法");
64     }
65 }

 

2、配置启用

很简单,只有继承WebMvcConfigurer,重写addInterceptors,将自定义的拦截器加入即可,可配置不拦截哪些路径,比如静态文件等。

 1 @Configuration
 2 public class HttpInterceptorConfig implements WebMvcConfigurer
 3 {
 4     @Override
 5     public void addInterceptors(InterceptorRegistry registry)
 6     {
 7         InterceptorRegistration registration = registry.addInterceptor(new HttpInterceptor());
 8         //所有路径都被拦截
 9         registration.addPathPatterns("/**");
10         //添加不拦截路径
11         registration.excludePathPatterns(
12                 //登录
13                 "/login",
14                 //html静态资源
15                 "/**/*.html",
16                 //js静态资源
17                 "/**/*.js",
18                 //css静态资源
19                 "/**/*.css",
20                 "/**/*.woff",
21                 "/**/*.ttf"
22         );
23     }
24 }
posted @ 2020-01-19 13:39  归去如风  阅读(294)  评论(0编辑  收藏  举报