MVC Java config – handlerinterceptor does not exclude paths

I have an MVC Java configuration, but handlerinterceptor does not exclude some modes

On the line marked XXX, if

1) I add addpatterns ("/ * *") and excludepathpatterns ("*. Ecxld") to the interceptorregistration of handlerinterceptor, handlerinterceptor Prehanlde() is not called at all For example addPathPatterns(“/ **”). excludePathPatterns(“*.ecxld”)

2) I only add excludepathpatterns ("*. Ecxld") to the interceptorregistration of handlerinterceptor, handlerinterceptor Pre hanlde() is still executed

(other interceptor calls are normal)

Any pointer is appreciated

thank you

@Configuration
public class MyMVCConfigurerAdapter extends WebMvcConfigurerAdapter {

 @Override
 public void addInterceptors(final InterceptorRegistry registry) {

     registry.addInterceptor(getInterceptorOne());

     registry.addInterceptor(getMyHandlerInterceptor())
                 .excludePathPatterns("*.ecxld");  // **xxx**

     registry.addInterceptor(getInterceptorTwo()
     );

 }

Solution

The pattern you specified for inclusion and exclusion is ant base path expressions instead of normal URL expressions. You will XML to map servlets or filters

To exclude work, you must also include an include path (as you have noticed the second comment) Next, change the exclusion mode to / * * / * ecxld.

Your current expression * Ecxld will match file Ecxld, but it doesn't match / file Ecxld or even / foo / file ecxld. / ** / Part responsible But in order for it to work, it also needs an includepathexpression (code checks, if there is no includepathexpression, ignore includepathexpression)

So simply put, your configuration to the following should solve your problem

@Configuration
public class MyMVCConfigurerAdapter extends WebMvcConfigurerAdapter {

 @Override
 public void addInterceptors(final InterceptorRegistry registry) {

     registry.addInterceptor(getInterceptorOne());

     registry.addInterceptor(getMyHandlerInterceptor())
                 .includePathPatterns("/**")
                 .excludePathPatterns("/**/*.ecxld");  

     registry.addInterceptor(getInterceptorTwo()
     );

 }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>