Spring Security:如何在FilterRegistrationBean中使用多个URL模式? [英] Spring Security: How to use multiple URL patterns in FilterRegistrationBean?

查看:1178
本文介绍了Spring Security:如何在FilterRegistrationBean中使用多个URL模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个豆子

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

在那个bean中,我对/api/cat** URL使用两种模式.问题是,当我尝试使用复杂的后缀(/api/cat/1/feed)调用终结点时,我的过滤器不会拦截该请求.但是当我调用/api/cat/api/got端点时就可以了-过滤器可以按预期工作并拦截请求.

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.

如何为我的案例(/api/cat/api/cat/**)使用多个URL模式?

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

推荐答案

如@Tarun Lalwani所述,您需要使用*而不是**,因为在这种情况下**不是有效的网址格式.

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/*"
    );

/api/cat/1/api/cat/1/feed/api/dog/1/api/dog/1/feed,...

如果要复制仅匹配/api/this/api/not/that/api/*行为,则需要使用以下模式:/api/*/.

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

这篇关于Spring Security:如何在FilterRegistrationBean中使用多个URL模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆