javaspringspring-bootinterceptor

Exclude paths from interceptor in spring boot


I have a simple interceptor that checks the subdomain, and I want to exclude a few paths from it. I followed the spring documentation Documentation and tried multiple solutions that I found here, but none of them worked for me. This is my interceptor.

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Autowired
JdbcTemplate jdbcTemplate;

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new HandlerInterceptor() {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            String subdomain = extractSubdomain(request);
            verifySubdomain(subdomain);
            return true;
        }
    }).excludePathPatterns("/**/clientcontrol/**", "/**/localhost/**").pathMatcher(new AntPathMatcher());
    }
}

I also tried the following:

.excludePathPatterns("/clientcontrol/**", "/localhost/**").pathMatcher(new AntPathMatcher());

.excludePathPatterns("/clientcontrol/**", "/localhost/**");

.addPathPatterns("/**").excludePathPatterns("/clientcontrol/**", "/localhost/**").pathMatcher(new AntPathMatcher());

But still, the interceptor kicks in, and my verifySubdomain method throws an exception.


Solution

  • For some reason, the following didn't work for me

    .excludePathPatterns("/**/clientcontrol/**");
    

    My whole address was http://localhost:8080/api/clientcontrol/foo, and technically, it should have worked, but it didn't. When I tried the following, it worked.

    .excludePathPatterns("/api/clientcontrol/**");