7

I have written an HandlerInterceptorAdapter implementation and would like to exclude the path patterns that authenticate the user and refresh the user while registering token, the URLS of thje resource mostly has the email address as a path param, which of the below code snippet is a valid form to do so

 @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        registry.addInterceptor(new AuthorizationInterceptor()).excludePathPatterns("/somepath/*/someresource/*");
    }

or

@Override
public void addInterceptors(InterceptorRegistry registry) {
    super.addInterceptors(registry);

    registry.addInterceptor(new AuthorizationInterceptor()).excludePathPatterns("/somepath/{email}/someresource/*");
}

EDIT:::

Enhanced the code using the PatternMatcher, still not working

 @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);

        registry.addInterceptor(new AuthorizationInterceptor())
                .excludePathPatterns(
                        "/somepath",
                        "/somepath/*/authenticate",
                        "somapath/*/someresource/verify"
                ).pathMatcher(new AntPathMatcher());
    }
Somasundaram Sekar
  • 5,244
  • 6
  • 43
  • 85

2 Answers2

8

Try this:

@Override
public void addInterceptors(InterceptorRegistry registry) {
    super.addInterceptors(registry);

    registry.addInterceptor(new AuthorizationInterceptor())
            .excludePathPatterns(
                    "/somepath",
                    "/somepath/**/authenticate",
                    "somapath/**/someresource/verify"
            ).pathMatcher(new AntPathMatcher());
}

The mapping matches URLs using the following rules:

? matches one character
* matches zero or more characters
** matches zero or more directories in a path
Saurabh
  • 333
  • 1
  • 2
  • 11
3

It will be better if you could simply have different url for excluding. For example:

@Override
public void addInterceptors(InterceptorRegistry registry) {
    super.addInterceptors(registry);
    registry.addInterceptor(new AuthorizationInterceptor()).excludePathPatterns("/somepath/someresource/**");
}

Than, everything starting with "/somepath/someresource/" will be excluded. Hope it helps.

More links on that: Spring MVC 3, Interceptor on all excluding some defined paths or About mvc:intercepter,how to set excluded path or simply docs

Community
  • 1
  • 1
m.aibin
  • 3,528
  • 4
  • 28
  • 47
  • Thank you, but the resources are identified by the email address I would like to exclude the pattern that are part of my resources "/users", /users/*/authenticate","users/*/contactinformation/phone/verify" with the asterisk after the "/users" I mean to indicate it is a dynamic path param and as far I have tested until now today it doesnt seem to work, looking for an solution to the problem – Somasundaram Sekar Nov 23 '15 at 06:57
  • Changing the way I Identify the resources to work around this seems a bit like an accidental architecture. – Somasundaram Sekar Nov 23 '15 at 07:01
  • You need to rethink your urls. Why not to have different urls for verify? For example simply /users/verify/** and then you can use different parameters – m.aibin Nov 23 '15 at 17:44
  • May be, but help me get that straight. I was in opinion that since the resource say a phone number has to be verified, the url identifying the number will be /users/{UUID of user}/phone/{phone}/verify, where in I navigate to that particular resource perform some manipulation over that, is my understanding off the principles of REST. – Somasundaram Sekar Nov 23 '15 at 21:53
  • 1
    You can have /users/verify/** and then use /users/verify/{user}/phone/{phone} or even /users/verify/{user}/{phone}. It's just the beginning of the url that you need to rethink ;) – m.aibin Nov 23 '15 at 23:09