spring interceptor失效

时间:2024-07-06 07:31:20
适用方式
  1. 增加interceptor,implements HandlerInterceptor,实例如下
	@Slf4j
public class AccessInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("accessInterceptor:权限查询。。。。。。。。");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
  1. 注册到WebMvcConfigurationSupport或者WebMvcConfigurer的继承类中
@Configuration
public class AxLogInterWebConfig extends WebMvcConfigurationSupport {
    @Bean
    LogInterceptor logInterceptor() {
        return new LogInterceptor();
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(logInterceptor());
    }
}
失效分析

如果失效了的话,那么可能出现如下原因

  1. 没有扫描到包
  2. path配置错误

上面是常规原因,最有可能的原因是:
3. WebMvcConfigurationSupport优先级大于WebMvcConfigurer,如果继承的话,那么优先继承WebMvcConfigurationSupport
4. 注意:WebMvcConfigurationSupport只能生效一个,如果有多个,那么也只能生效一个。