Java – Spring Security: How to use multiple URL patterns in FilterRegistrationBean

javaservlet-filtersspringspring-security

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

Best Solution

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/*/.