Spring, Interceptor's excludePathPatterns function is not working properly
Asked Answered
G

6

4

I am working on Spring Framework and I wanted to write an interceptor and I wrote it eventually and it is working fine. but at a point, I dont want my interceptor to intercept the request that is when user wants to logout and the session is being invalidated. But it is not happening as per my expectation.

I am adding interceptors by extending the WebMvcConfigurerAdapter and by utilizing the addInterceptors method and here is the code.

public void addInterceptors(InterceptorRegistry registry) {
    super.addInterceptors(registry);
    registry.addInterceptor( loggerInterceptor );
    registry.addInterceptor( authenticationInterceptor ).excludePathPatterns("/invalidate");   
    }

Have I done anything wrong here.? excludePathPatterns - > My URL ends with /invalidate. So please guide me, how to device a proper pattern.

Groos answered 20/1, 2014 at 7:31 Comment(1)
I have used following methods but it always runs the authentication interceptor. /**/invalidate*, */invalidate, /invalidate, /**/invalidate, **/invalidateGroos
S
9

Have you tried as below?

@Configuration
@EnableWebMvc
public class MyWebConfig extends WebMvcConfigurerAdapter 
{
  @Override
  public void addInterceptors(InterceptorRegistry registry) 
  {
    registry.addInterceptor(new MyCustomInterceptor())
            .addPathPatterns("/**")
            .excludePathPatterns("/foo/**");
  }
}

Reference

Refer this java doc for better understanding.

Swane answered 20/1, 2014 at 8:8 Comment(4)
I've tried this exact same thing and it's not working for me. My interceptor is catching every request, not respecting the excludePathPatterns(...). I have a simple controller that I want to process a specific request, but can't get the request due to the fact that my interceptor is catching all request.Affix
With Spring 5+, WebMvcConfigurerAdapter is deprecated, you need to implement WebMvcConfigurer (interface).Tipstaff
@HatemJaber I have the same problem, for no reasons the interceptor is trapping every request. The only solution I found so far is checking the path inside the interceptor.Henryhenryetta
@Chandra Sekhar Can I have the excludePathPatterns without the addPathPatterns ? I assume if I don't specify the addPathPatterns, it'll default to everything.Semasiology
R
1

I think maybe your code triggers another error,the url path changed to another url(/error etc),then the /error url path that you have not excluded,pls check your code or debug a breakpoint as this picture.

Roth answered 27/4, 2018 at 8:25 Comment(0)
L
0

It's Unnecessary add:

addPathPatterns("/**")

MappedInterceptor.java:

Ligula answered 22/9, 2016 at 8:39 Comment(1)
This answer doesn't address the question; it's a comment on another answer. For those that are confused about the relevance of MappedInterceptor, an instance of it is created behind the scenes when an interceptor is added. See github.com/spring-projects/spring-framework/blob/master/… Note at the bottom of that class, in the toArray method, that if the List of included patterns is empty then null is returned rather than an empty array.Cycad
P
0

In my case, I had to debug the whole FrameworkServlet of Spring, and somewhere, an exception was thrown saying: "No parameters named hashId".

Turned out my requestParam wasn't named hashId but id, so the interceptor was correctly applied to that service url path.

Make sure the excludedPattern starts with "/" and is requested with the parameter names exactly as defined in the controller.

Pitre answered 25/1, 2018 at 20:20 Comment(0)
O
0

I have found the same issue while implementing the similar requirement. Please try below in your code if you are using Spring 5+ version.

@Configuration
public class BasicInterceptorAppConfig implements **WebMvcConfigurer** {

    @Autowired
    YourCustomInterceptor yourCustomInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry interceptorRegistry){
        interceptorRegistry.addInterceptor(yourCustomInterceptor).addPathPatterns("/**").excludePathPatterns("/admin.html", "/swagger-ui.html");

    }

}

Please user WebMvcConfigurer for Spring 5+ version. Hope this may help someone who is looking similar solution.

Orderly answered 24/3, 2021 at 12:5 Comment(0)
P
0

In my case it was missing trailing slash.

registry.addInterceptor(fooInterceptor()).excludePathPatterns("/internal/bar**");
instead of

registry.addInterceptor(fooInterceptor()).excludePathPatterns("/internal/bar/**");

Payable answered 20/11, 2023 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.