MVC Java Config - HandlerInterceptor 不排除路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20243130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:31:38  来源:igfitidea点击:

MVC Java Config - HandlerInterceptor not excluding paths

javaspring-mvcspring-3

提问by solarwind

I have a MVC Java configuration but the HandlerInterceptoris not excluding some patterns.

我有一个 MVC Java 配置,但HandlerInterceptor不排除某些模式。

At the line marked with xxx, if

在标有xxx的那一行,如果

1) I add both addPatterns("/**")and excludePathPatterns("*.ecxld")to the HandlerInterceptor's InterceptorRegistration, the HandlerInterceptor.preHanlde()is NOT invoked at all. e.g .addPathPatterns("/**").excludePathPatterns("*.ecxld")

1)我将addPatterns("/**")和都添加excludePathPatterns("*.ecxld")HandlerInterceptor's InterceptorRegistrationHandlerInterceptor.preHanlde()根本没有被调用。例如.addPathPatterns("/**").excludePathPatterns("*.ecxld")

2) I add only excludePathPatterns("*.ecxld")to the HandlerInterceptor's InterceptorRegistration, the HandlerInterceptor.preHanlde()is still executed.

2)我只添加excludePathPatterns("*.ecxld")HandlerInterceptor's InterceptorRegistrationHandlerInterceptor.preHanlde()仍然被执行。

(the other interceptors are invoked fine).

(其他拦截器被调用得很好)。

Any pointers appreciated.

任何指针表示赞赏。

Thanks

谢谢

@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()
     );

 }

采纳答案by solarwind

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.

调试后,拦截器不会按照添加的顺序执行。在上面的例子中,interceptorOne,然后是interceptorTwo,然后是处理程序(具有排除模式)被执行。

回答by M. Deinum

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

您为 include 和 exclude 指定的模式是基于 ant 的路径表达式,而不是您在 web.xml 中表示以映射 servlet 或过滤器的普通 URL 表达式。

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.

要进行排除工作,您还必须包含一个包含路径(正如您在第二条评论中已经注意到的那样)。接下来将您的排除模式更改为/**/*.ecxld.

Your current expression *.ecxldwould match file.ecxldbut it will not match /file.ecxldor even /foo/file.ecxld. The /**/part takes care of that. However to make it work it also requires an includePathExpression(the code checksif there is an includePathExpressionwhen not it is ignoring the excludePathExpression).

您当前的表达式*.ecxld会匹配,file.ecxld但不会匹配/file.ecxld,甚至不匹配/foo/file.ecxld。这/**/部分负责处理。然而,为了使其工作,它还需要一个includePathExpression(代码检查是否有一个includePathExpression,而不是它忽略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()
     );

 }

回答by 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:

我知道这是很久以前的事了,但我偶然发现了同样的问题。在我的搜索过程中,我发现了以下博客。那里提到如果拦截器被配置为 bean,它们将自动添加到链中。我现在使用的是 Spring 4.1.x,所以可能会有所不同,但为我解决的问题如下:

  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");

  1. (我试图避免将它们定义为弹簧豆。它没有帮助。)
  2. 我将拦截器配置为 spring bean(因此我可以将内容自动装配到它们中,请参见此处
  3. 我改变了我的定义如下:

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

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

通过将 addPathPatterns 放在 excludePathPatterns 之前,拦截器的行为突然工作正常。

回答by Mr Hoelee

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.

我遇到了这个麻烦,无法排除路径。经过我调试,发现是因为Spring安全重定向到“/login”,因为“/login”包含在“/**”中,所以无法访问。

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

解决方案是添加登录和注销链接作为排除路径!

回答by vs_lala

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

我在使用 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.

我做了一个方法来返回拦截器的新实例。并且您必须在注册表的 addPathPattern 方法之后编写 excludePathPatters。

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.

我希望这有帮助。