SpringBoot自定义拦截器

执行顺序

前置Filter---->前置Interceptor---->controller---->后置Interceptor---->后置Filter

实现步骤:

1、创建自己的拦截器类并实现 HandlerInterceptor 接口。

2、

  • 创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法。(SpringBoot 2.0之前)
  • SpringBoot 2.0 后,WebMvcConfigurerAdapter被标记为@Deprecated。官方推荐直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport,方式一:实现WebMvcConfigurer接口(推荐),方式二:继承WebMvcConfigurationSupport类

3、实例化自定义的拦截器

4、将拦截器对像手动添加到拦截器链中(在addInterceptors方法中添加)

代码

自定义拦截器

实现 HandlerInterceptor 接口

preHandle() 方法:进入controller层之前拦截请求

postHandle() 方法:处理请求完成后视图渲染之前的处理操作 请求之后,控制器中抛出了异常的话就不会执行。

afterCompletion() 方法:视图渲染之后的操作,请求之后调用,不管抛不抛出异常都会被调用.参数中异常如果被异常处理器调用的话就不会传入到参数中。

@Component
//1、创建自己的拦截器类并实现 HandlerInterceptor 接口。
public class MyInterceptor implements HandlerInterceptor {
 
    /**
     * 进入controller层之前拦截请求
     * */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor preHandle");
        return true;
    }
 
    /**
     * 处理请求完成后视图渲染之前的处理操作
     *
     * 请求之后,控制器中抛出了异常的话就不会执行
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyInterceptor postHandle");
    }
 
    /**
     * 视图渲染之后的操作
     *
     * 请求之后调用,不管抛不抛出异常都会被调用.参数中异常如果被异常处理器调用的话就不会传入到参数中.
     * */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor afterCompletion");
    }
}
注册拦截器

spring boot 1.X是继承WebMvcConfigurerAdapter

重写addInterceptors方法,注册拦截器

@Configuration
//2、创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法。
public class MyInterceptorConfiguration extends WebMvcConfigurerAdapter {
 
    //3、实例化自定义的拦截器
    @Resource
    private MyInterceptor myInterceptor;
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //如果interceptor中不注入redis或其他项目可以直接new
        //4、将拦截器对像手动添加到拦截器链中(在addInterceptors方法中添加)
        registry.addInterceptor(myInterceptor).addPathPatterns("/**");
    }
}

SpringBoot 2.0后:

方式一:实现WebMvcConfigurer接口(推荐)

@Configuration
//2、创建一个Java类实现WebMvcConfigurer接口,并重写 addInterceptors 方法。
public class MyInterceptorConfiguration implements WebMvcConfigurer {
 
    //3、实例化自定义的拦截器
    @Resource
    private MyInterceptor myInterceptor;
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //如果interceptor中不注入redis或其他项目可以直接new
        //4、将拦截器对像手动添加到拦截器链中(在addInterceptors方法中添加)
        registry.addInterceptor(myInterceptor).addPathPatterns("/**");
    }
}

方式二:继承WebMvcConfigurationSupport类

@Configuration
//2、创建一个Java类继承WebMvcConfigurationSupport,并重写 addInterceptors 方法。
public class MyInterceptorConfiguration extends WebMvcConfigurationSupport {
 
    //3、实例化自定义的拦截器
    @Resource
    private MyInterceptor myInterceptor;
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //如果interceptor中不注入redis或其他项目可以直接new
        //4、将拦截器对像手动添加到拦截器链中(在addInterceptors方法中添加)
        registry.addInterceptor(myInterceptor).addPathPatterns("/**");
    }
}

原文参考:https://blog.csdn.net/jy02268879/article/details/84302520

参考文章:

补充:

实践出真知
继承WebMvcConfigurationSupport类时,如果加了@EnableWebMvc,拦截器不能正常执行。不加@EnableWebMvc注解,拦截器正常执行
继承WebMvcConfigurerAdapter 与 实现WebMvcConfigurer接口时,无论是否加 @EnableWebMvc注解,都能正常执行
原因可能如下:
https://blog.csdn.net/weixin_42479595/article/details/104562174

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值