栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 拦截器实现注意要点

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot 拦截器实现注意要点

一、Interceptor 实现方式
  1. 实现HandlerInterceptor
  2. 继承WebRequestInterceptor
    两者的区别不大,针对于 preHandle 这个方法的时候,继承WebRequestInterceptor类的不需要返回值,但是实现HandlerInterceptor接口的,需要一个Boolean类型的返回值,true 的才会往下走,false的话就会中断请求,常用的是第一种。
  • 继承WebRequestInterceptor
@Override
    public void preHandle(WebRequest request) throws Exception {
    }
    @Override
    public void postHandle(WebRequest request, ModelMap model) throws Exception {
    }
    @Override
    public void afterCompletion(WebRequest request, Exception ex) throws Exception {
    }
  • 实现HandlerInterceptor
 @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) { 
    }
    @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 {
    }
二、针对于springboot 需要的一些配置参数
  1. 需要一个配置类实现 WebMvcConfigurer来加载拦截器
@Configuration
public class Config implements WebMvcConfigurer {

    @Autowired
    private ApiSignInterceptor apiSignInterceptor;


    @Override //拦截器配置
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(apiSignInterceptor) //拦截器注册对象
                .addPathPatterns("; //排除请求
    }

}

这里有一个很重要的点,就是拦截器引入,主要是针对 addInterceptors方法。一些人可能会这么去写:
通过new 的方式直接去加载拦截器

@Override //拦截器配置
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ApiSignInterceptor()) 
    }

实际上,这两种都是可以实现的,通过@Autowired 的方式去引入你的拦截器,你可以直接在拦截器里面引入其他service来写你的一些业务代码,比如存表之类的,不然的话,你通过new 的方式引入拦截器,你在拦截器里面引入其他的service,会是null,无法使用。

三、异常处理

拦截器里的异常是可以被全局异常捕获的,所以写个全局异常类就可以了,不需要额外在跳到controller里面或者通过response来传递。

@RestControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler({ BizException.class })
    public ResponseBean handleBizException(BizException e) {
        LogUtil.warn(e, "业务异常");
        return new ResponseBean<>(e.getErrorCode(), e.getErrorMsg());
    }
}
四、拦截器失效的几种情况
  1. 如果有其他config 继承了 WebMvcConfigurationSupport 类的,会导致拦截器失效。
  2. 拦截器没有加到配置里的。
  3. springboot 的Application 包位置未扫描到,需要加Scan的范围。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/858163.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号