Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC handler interceptor with exclude path pattern with pathparam

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());
    }
like image 206
Somasundaram Sekar Avatar asked Nov 23 '15 05:11

Somasundaram Sekar


2 Answers

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
like image 146
Saurabh Avatar answered Oct 20 '22 17:10

Saurabh


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

like image 39
m.aibin Avatar answered Oct 20 '22 18:10

m.aibin