SpringBoot2.X自定义拦截器实战及新旧配置对比(核心知识)

简介: 讲解拦截器使用,Spingboot2.x新版本配置拦截拦截器和旧版本SpringBoot配置拦截器区别讲解

1、@Configuration
  继承WebMvcConfigurationAdapter(SpringBoot2.X之前旧版本)

  SpringBoot2.X 新版本配置拦截器 implements WebMvcConfigurer

2、自定义拦截器 HandlerInterceptor
  preHandle:调用Controller某个方法之前
  postHandle:Controller之后调用,视图渲染之前,如果控制器Controller出现了异常,则不会执行此方法
  afterCompletion:不管有没有异常,这个afterCompletion都会被调用,用于资源清理

3、按照注册顺序进行拦截,先注册,先被拦截

  拦截器不生效常见问题:
    1)是否有加@Configuration
    2)拦截路径是否有问题 ** 和 *
    3)拦截器最后路径一定要 “/**”, 如果是目录的话则是 /*/

  Filter
    是基于函数回调 doFilter(),而Interceptor则是基于AOP思想
    Filter在只在Servlet前后起作用,而Interceptor够深入到方法前后、异常抛出前后等

    依赖于Servlet容器即web应用中,而Interceptor不依赖于Servlet容器所以可以运行在多种环境。

    在接口调用的生命周期里,Interceptor可以被多次调用,而Filter只能在容器初始化时调用一次。

    Filter和Interceptor的执行顺序

    过滤前->拦截前->action执行->拦截后->过滤后

代码示例:

  CustomWebMvcConfigurer.java

 1 package net.xdclass.demo.intecpter;
 2 
 3 import org.springframework.context.annotation.Configuration;
 4 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 5 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 6 
 7 
 8 @Configuration
 9 public class CustomWebMvcConfigurer implements WebMvcConfigurer  {
10 
11     @Override
12     public void addInterceptors(InterceptorRegistry registry) {
13 
14         //'/*'匹配一个文件夹
15         registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/api2/*/**");
16         registry.addInterceptor(new TwoIntercepter()).addPathPatterns("/api2/*/**");
17         
18         //.excludePathPatterns("/api2/xxx/**"); //拦截全部 /*/*/**
19         
20         WebMvcConfigurer.super.addInterceptors(registry);
21     }
22 
23     
24     
25     
26 
27 
28 }

LoginIntercepter.java:

 1 package net.xdclass.demo.intecpter;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 
 6 import org.springframework.web.servlet.HandlerInterceptor;
 7 import org.springframework.web.servlet.ModelAndView;
 8 
 9 public class LoginIntercepter implements HandlerInterceptor{
10 
11     /**
12      * 进入controller方法之前
13      */
14     @Override
15     public boolean preHandle(HttpServletRequest request,
16             HttpServletResponse response, Object handler) throws Exception {
17         System.out.println("LoginIntercepter------->preHandle");
18 
19 //        String token = request.getParameter("access_token");
20         
21 //        response.getWriter().print("fail");
22         
23         return HandlerInterceptor.super.preHandle(request, response, handler);
24     }
25 
26     /**
27      * 调用完controller之后,视图渲染之前
28      */
29     @Override
30     public void postHandle(HttpServletRequest request,
31             HttpServletResponse response, Object handler,
32             ModelAndView modelAndView) throws Exception {
33         
34         System.out.println("LoginIntercepter------->postHandle");
35         
36         HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
37     }
38 
39     /**
40      * 整个完成之后,通常用于资源清理
41      */
42     @Override
43     public void afterCompletion(HttpServletRequest request,
44             HttpServletResponse response, Object handler, Exception ex)
45             throws Exception {
46         System.out.println("LoginIntercepter------->afterCompletion");
47         
48         HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
49     }
50 
51     
52     
53 }

TwoIntercepter.java:

 1 package net.xdclass.demo.intecpter;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 
 6 import org.springframework.web.servlet.HandlerInterceptor;
 7 import org.springframework.web.servlet.ModelAndView;
 8 
 9 public class TwoIntercepter implements HandlerInterceptor{
10 
11     /**
12      * 进入对应的controller方法之前
13      */
14     @Override
15     public boolean preHandle(HttpServletRequest request,
16             HttpServletResponse response, Object handler) throws Exception {
17 
18         System.out.println("TwoIntercepter------>preHandle");
19         return HandlerInterceptor.super.preHandle(request, response, handler);
20     }
21 
22     /**
23      * controller处理之后,返回对应的视图之前
24      */
25     @Override
26     public void postHandle(HttpServletRequest request,
27             HttpServletResponse response, Object handler,
28             ModelAndView modelAndView) throws Exception {
29         System.out.println("TwoIntercepter------>postHandle");
30         HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
31     }
32 
33     /**
34      * 整个请求结束后调用,视图渲染后,主要用于资源的清理
35      */
36     @Override
37     public void afterCompletion(HttpServletRequest request,
38             HttpServletResponse response, Object handler, Exception ex)
39             throws Exception {
40         System.out.println("TwoIntercepter------>afterCompletion");
41         HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
42     }
43 
44     
45 }

测试代码:

浏览器访问:http://localhost:8083/api2/v1/account

控制台查看:

 

转载于:https://www.cnblogs.com/116970u/p/10251388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值