Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Security: How to use multiple URL patterns in FilterRegistrationBean?

I have a bean

@Bean
public FilterRegistrationBean animalsFilterRegistration() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(new AnimalsFilter());
    registration.addUrlPatterns(
        "/api/cat",
        "/api/cat/**",
        "/api/dog"
    );
    ...
    return registration;
}

In that bean, I use two patterns for /api/cat** URLs. The problem is that when I try to call endpoints with complex postfix (/api/cat/1/feed), my filter does not intercept the request. But it's OK when I call /api/cat and /api/got endpoints -- filter works as expected and intercepts requests.

How can I use multiple URL patterns for my case (/api/cat, /api/cat/**)?

PS

I have tried to use next pattern combinations:

1) /api/cat, /api/cat**, /api/dog
2) /api/cat, /api/cat/**, /api/dog
3) /api/cat**, /api/dog
like image 933
Alex Avatar asked Jun 26 '17 21:06

Alex


1 Answers

As mentioned by @Tarun Lalwani, you need to use * instead of **, because ** is not a valid url pattern in this case.

In your case, try the following:

    registration.addUrlPatterns(
        "/api/cat",
        "/api/cat/*",
        "/api/dog",
        "/api/dog/*"
    );

those would match /api/cat/1, /api/cat/1/feed, /api/dog/1, /api/dog/1/feed, ...

If you want to replicate the /api/* behavior that would, only match /api/this but /api/not/that, then you need to use the following pattern: /api/*/.

like image 140
TwiN Avatar answered Sep 18 '22 12:09

TwiN