2

I have an interceptor that is supposed to intercept urls with different patterns like:

  • myapp/something/add/whatever
  • myapp/something/add
  • myapp/something/addWhatever
  • myapp/something/somethingelse/add
  • etc...

I have to intercept all urls which contain "add". There are a lot of somethings and somethingelses...

I have tried different patterns but it seems that they are all wrong:

  • **/add/*
  • **/add*
  • **/add/ ** (I added a blank space before the last ** so it doesn't format it to bold)

The interceptor is something like

public class MyInterceptor implements HandlerInterceptor {
}

I configure it in

@Configuration
@EnableSpringDataWebSupport
@EnableWebMvc
class MvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(final InterceptorRegistry registry) {                       
        registry.addInterceptor(getMyInterceptor()).addPathPatterns("**/add/*", "**/add/**", "**/add*");
    }

    @Bean
    public MyInterceptor getMyInterceptor() {
        return new MyInterceptor();
    }
}

If I try to access

http://localhost:8080/myapp/something/add/somethingelse

my interceptor doesn't intercept it...

alex
  • 8,904
  • 6
  • 49
  • 75
diminuta
  • 1,545
  • 8
  • 32
  • 55
  • 1
    Try `**/add*/**` Here is the AntPathMatcher API page: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html – DwB Jan 17 '18 at 17:24
  • Did you try `/**/add/**` and `/**/add*`, because the path must be absolute. – dur Jan 17 '18 at 17:25
  • You might want this `addPathPatterns("**/add*", "**/add*/**")` to include stuff that ends in "add" – DwB Jan 17 '18 at 17:28
  • Thank you sir, @DwB, I have read the API, it is really helpful. - https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html – Eddy Dec 26 '18 at 02:30

2 Answers2

1

I had a similar issue. Here are my suggestions.

First use global interceptor and check the request uri:

public class MyInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String uri = request.getRequestURI();

        if(uri.contains("/add")){
            // do your job
        }

        return super.preHandle(request, response, handler);
    }
}

In my case, alls add- methods are PUT, or POST requests. So I'm checking this in my global interceptor:

public class MyInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String method = request.getMethod();
        if("PUT".equals(method) || "POST".equals(method)){
            // do your job
        }

        return super.preHandle(request, response, handler);
    }
}

configure it without addPathPatterns:

@Configuration
@EnableSpringDataWebSupport
@EnableWebMvc
class MvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(final InterceptorRegistry registry) {
        registry.addInterceptor(getMyInterceptor());
    }

    @Bean
    public MyInterceptor getMyInterceptor() {
        return new MyInterceptor();
    }
}
alex
  • 8,904
  • 6
  • 49
  • 75
0

Apparently this can be fixed by Change the bean type to "Mapped Interceptor" and wrapping it; though people don't seem to know why its an issue in the first place.

Based on this solution: https://stackoverflow.com/a/35948730/857994

John Humphreys
  • 37,047
  • 37
  • 155
  • 255