简体   繁体   中英

Spring Boot Interceptor called anyway of exclude pattterns

Having a Interceptor registered with:

@Override
protected void addInterceptors(final InterceptorRegistry registry) {
    registry.addInterceptor(new ThymeleafLayoutInterceptor()).addPathPatterns("/**").excludePathPatterns("/json/**").excludePathPatterns("/static/**");
}

In my understanding the interceptor shall be called for every request but not for requests with
/static/
or
/json/ in their path. But, the interceptor seems to be called from every resource, also from resources with static in their path.

A print out of in the PostHandle method of my interceptor

final ResourceHttpRequestHandler h = (ResourceHttpRequestHandler) handler;
System.out.println(h.getLocations());

results in

[class path resource [static/]]

I tried pattern like
1. /static/**
2. /static/*,
3. /static/
4. static/

How can this be possible and how can i correct the issue?

You are calling excludePathPatterns twice.

This should do the job

@Override
protected void addInterceptors(final InterceptorRegistry registry) {
    registry.addInterceptor(new ThymeleafLayoutInterceptor())
            .addPathPatterns("/**")
            .excludePathPatterns("/json/**", "/static/**");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM