SpringBoot中使用拦截器

1、开发拦截器

public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        System.out.println("======1=====");
        return true;//返回true 放行  返回false阻止
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("=====2=====");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {
        System.out.println("=====3=====");
    }
}

2、配置拦截器

@Configuration
public class WebMvcConfig  implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
          registry.addInterceptor("拦截器")
                .addPathPatterns("拦截路径")
                .excludePathPatterns("排除路径")
                      .order("指定执行顺序")
    }
}

注意:order用来执行多个拦截器的执行顺序,order书写是自然数,按照自然数顺序执行

最后修改:2021 年 10 月 19 日 12 : 28 AM
如果觉得我的文章对你有用,请随意赞赏