Exclude Spring Request HandlerInterceptor by Path-Pattern

后端 未结 3 1246
借酒劲吻你
借酒劲吻你 2021-02-14 17:39

I know we can map different url to different interceptor, or we can map multiple url to single interceptor too. I am just curious to know if we also have exclude option. for exa

3条回答
  •  感动是毒
    2021-02-14 18:14

    I think in spring-boot 2.0 version, this have changed a lot right now. Below is the implementation you can easily add and configure the path pattern.

    @Component
    public class ServiceRequestAppConfig implements WebMvcConfigurer {
    
        @Autowired
        ServiceRequestInterceptor sri;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            String pathPattern = "/admin/**";
            registry.addInterceptor(sri).excludePathPatterns(pathPattern);
        }
    
    }
    
    
    @Component
    public class ServiceRequestInterceptor extends HandlerInterceptorAdapter {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
            // Your logic
            return true;
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex){
             // Some useful techniques
        }
    
    }
    

提交回复
热议问题