Ch Faizan Mustansar
Ch Faizan Mustansar

Reputation: 364

Spring, Interceptor's excludePathPatterns function is not working properly

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.

Upvotes: 4

Views: 19418

Answers (6)

Tomas F.
Tomas F.

Reputation: 720

In my case it was missing trailing slash.

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

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

Upvotes: 0

Kushwaha
Kushwaha

Reputation: 918

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.

Upvotes: 0

ross
ross

Reputation: 11

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.

Upvotes: 1

Camilo
Camilo

Reputation: 469

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.

Upvotes: 0

user6863171
user6863171

Reputation: 11

It's Unnecessary add:

addPathPatterns("/**")

MappedInterceptor.java:

Upvotes: 0

Chandra Sekhar
Chandra Sekhar

Reputation: 16516

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.

Upvotes: 9

Related Questions