WebMvcConfigurer添加多個攔截器的攔截路徑問題


結論:每個攔截器的addPathPatterns,excludePathPatterns添加的路徑是各自獨立的,如果添加的一個攔截器沒有addPathPattern任何一個url則默認攔截所有請求,如果沒有excludePathPatterns任何一個請求,則默認不放過任何一個請求。

 

驗證過程:

兩個攔截器:

public class TestInterceptor1 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        response.getOutputStream().write("To TestInterceptor1...".getBytes());

        return true;
    }
}

public class TestInterceptor2 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        response.getOutputStream().write("To TestInterceptor2...".getBytes());

        return true;
    }
}

 

情景一:兩個攔截器的攔截路徑不同

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry interceptorRegistry){
        interceptorRegistry.addInterceptor(new TestInterceptor1()).addPathPatterns("/ready");
        interceptorRegistry.addInterceptor(new TestInterceptor2()).addPathPatterns("/health");
    }
}

-------------測試結果-------------

 

 

 

 

 

情景二 :其中一個攔截器不配置攔截路徑

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry interceptorRegistry){
        interceptorRegistry.addInterceptor(new TestInterceptor1()).addPathPatterns("/ready");
        interceptorRegistry.addInterceptor(new TestInterceptor2());
    }
}

-------------測試結果-------------

 

 

 

 

情景三:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry interceptorRegistry){
        interceptorRegistry.addInterceptor(new TestInterceptor1());
        interceptorRegistry.addInterceptor(new TestInterceptor2()).addPathPatterns("/health");
    }
}

-------------測試結果-------------

 

 

 

 

 

 情景四:其中一個攔截器的攔截路徑在另一個攔截器的不攔截路徑內

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry interceptorRegistry){
        interceptorRegistry.addInterceptor(new TestInterceptor1()).addPathPatterns("/ready");
        interceptorRegistry.addInterceptor(new TestInterceptor2()).excludePathPatterns("/ready");
    }
}

-------------測試結果-------------

 

 

 

 

 

 

 

從源碼上看每個攔截器interceptor的includePatterns和excludePatterns也確實是獨立的,可以參考下

public InterceptorRegistration addInterceptor(HandlerInterceptor interceptor) {
   InterceptorRegistration registration = new InterceptorRegistration(interceptor);
   this.registrations.add(registration);
   return registration;
}

public InterceptorRegistration addPathPatterns(List<String> patterns) {
   this.includePatterns.addAll(patterns);
   return this;
}

public InterceptorRegistration excludePathPatterns(List<String> patterns) {
   this.excludePatterns.addAll(patterns);
   return this;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM