IntelliJ IDEA 2017版 spring-boot搭建拦截器

1、建立一个springboot-web项目

         https://www.cnblogs.com/liuyangfirst/p/8298588.html

2、加入过滤接口

 1 public class LoginInterceptor implements HandlerInterceptor {
 2 
 3 
 4     @Override
 5     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
 6 
 7         System.out.println("已经进入了登录拦截器......");
 8 
 9         // 逻辑代码按照之前的方式去编写即可
10 
11 
12         return true;
13     }
14 
15     @Override
16     public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
17 
18     }
19 
20     @Override
21     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
22 
23     }
24 }
View Code

3、配置识别类

 1 // 当前类变成配置拦截器类
 2 @Configuration
 3 public class WebConfig extends WebMvcConfigurerAdapter {
 4 
 5     @Override
 6     public void addInterceptors(InterceptorRegistry registry) {
 7 
 8         // 需要拦截的路径
 9         String[] addPathPatterns = {"/test/**"};
10 
11         //不拦截的路径
12         String[] excludePathPatterns = {"/index", "/myjsp"};
13 
14 
15 //        注册登录拦截器(此拦截器注册多行就是,添加多个拦截器)
16         registry.addInterceptor(new LoginInterceptor())
17                 .addPathPatterns(addPathPatterns).
18                 excludePathPatterns(excludePathPatterns);
19     }
20 }
View Code

4、源码位置

https://github.com/liushaoye/05-filter/tree/master

猜你喜欢

转载自www.cnblogs.com/liuyangfirst/p/9316557.html