MVC Java Config - HandlerInterceptor not excluding paths

烈酒焚心 提交于 2019-12-03 03:14:44

The patterns you specify for include and exclude are ant bases path expressions and not normal URL expressions as you would express in web.xml to map a servlet or filter for instance.

To make an exclude work you have to also include an include path (as you already noticed with your second remark). Next change your exclude pattern to /**/*.ecxld.

Your current expression *.ecxld would match file.ecxld but it will not match /file.ecxld or even /foo/file.ecxld. The /**/ part takes care of that. However to make it work it also requires an includePathExpression (the code checks if there is an includePathExpression when not it is ignoring the excludePathExpression).

So in short change 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()
     );

 }
wjentner

I know this was a long while ago but I just stumbled over the same problem. During my search I found the following blog. There it is mentioned that if the interceptors are configured as beans they will be automatically added to the chain. I am now using Spring 4.1.x so there might be a difference but what solved it for me was the following:

  1. (I tried to avoid defining them as a spring beans. It didn't help.)
  2. I configured the interceptors as spring beans (so I could autowire stuff into them see here)
  3. I changed my definition as follows:

    registry.addInterceptor(getMyHandlerInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/user/login");

By putting the addPathPatterns before the excludePathPatterns the behavior of the interceptor suddenly worked fine.

After debugging, the interceptors are not executed in the order they were added. In the above example, interceptorOne, then interceptorTwo, then the handler (with the excluded pattern) was executed.

I run into this trouble, can't exclude the path. After I debug, found out is because Spring security redirect to "/login", because of "/login" is included in "/**", that why cannot access.

Solution is add the login & logout link as exclude paths too!

I've faced a similar problem while working with SpringBoot.

How I solved this problem?

I made a method to return a new instance of the Interceptor. And you will have to write the excludePathPatters after the addPathPattern method of the registry.

Here's the code snippet:

@Bean
public AuthInterceptor getAuthInterceptor() {
    return new AuthInterceptor();
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(**getAuthInterceptor()**)
        .addPathPatterns("/**")
        .excludePathPatterns("/login/**");
}

I hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!