【问题标题】:Spring security application of antMatcher() vs. antMatchers()antMatcher() 与 antMatchers() 的 Spring 安全应用
【发布时间】:2018-04-28 03:03:31
【问题描述】:

只是想看看我是否以正确的方式解释answer to this question。

如果我们只需要像这样保护一条路径:

http.antMatcher("/api/**").authorizeRequests()....

然后使用antMatcher()。

如果我们需要像这样保护多个 URL 路径:

http
.authorizeRequests()
    .antMatchers("/high_level_url_A/sub_level_1").hasRole('USER')
    .antMatchers("/high_level_url_A/sub_level_2").hasRole('USER2')
    ...

然后使用antMatchers()。

this question 中有两个答案,但每个答案中提供的示例与另一个中给出的示例相矛盾。第一个回答说作者不需要antMatcher(),第二个回答说总是以`antMatcher() IIUC开头。

【问题讨论】:

    标签: java spring-mvc spring-boot spring-security


    【解决方案1】:

    HttpSecurity.antMatcher() 将HttpSecurity 实例的默认请求匹配器从AnyRequestMatcher 更改为AntPathRequestMatcher。 ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry.antMatchers() 用于将授权规则应用于与当前 HttpSecurity 实例关联的端点子集。

    示例代码:

    http
        .antMatcher("/api/**")
        .httpBasic()
            .disable()
        .authorizeRequests()
            .antMatchers("/api/user/**", "/api/ticket/**", "/index")
                .hasRole("USER");
    

    在上面的示例中,所有匹配 /api/** 的端点都禁用了基本授权。此外,匹配 /api/user/** 或 /api/ticket/** 的端点将要求请求的身份验证包含 ROLE_USER。但是,当用户尝试访问 /index 时,他们会遇到基本的身份验证提示。输入凭据后,无论请求的身份验证是否包含 ROLE_USER,用户都将被授予对端点的访问权限。这是因为 .antMatcher("/api/**") 将整个 HttpSecurity 实例的范围限制为特定的 AntMatcher。

    下面的示例将确保 HttpSecurity 的范围包括之前的三个 AntMatchers,仅此而已:

    http
        .requestMatchers()
            .antMatchers("/api/user/**", "/api/ticket/**", "/index")
            .and()
        .httpBasic()
            .disable()
        .authorizeRequests()
            .any()
                .hasRole("USER");
    

    编辑 如果您使用#hasRole(),那么您的角色不应以“ROLE_”开头,因为它会自动插入。

    【讨论】:

    • @Ole:不,您不进行全局安全设置,您始终为配置进行设置。您只需配置与 antMatcher() 一起使用的配置。
    • 为了清楚起见,如果您不使用 Spring Boot,您的示例结果会发生变化,因为 Spring Boot 具有默认配置。为了使您的示例与 Spring Boot 一起使用,配置顺序必须正确。
    • 因此,如果我们执行 http.antMatcher("/api/**") 以确保我得到它,那么 Spring 会为 "/api/**" 创建一个配置,然后我们继续为该配置应用特定的配置设置?如果我们说 http.antMatcher("v2/api/**") 那么 Spring 创建另一个适用于 API 版本 2 的安全配置?
    • @Ole 在同一个 HttpSecurity 实例上调用 antMatcher() 两次只会替换原来的 antMatcher。要将相同的 HttpSecurity 配置应用于多个 antMatchers,请使用 http.requestMatchers()。 antMatchers()。另一种方法是创建多个 SecurityConfigurer,每个都使用自己的 antMatcher,然后用 \@order 注释它们以确保 spring 加载所有它们。
    • 你好@dsep,我正在使用混合配置,我需要在我的 JwtSecurityConfig 中授权两个不同的路径。例如:/web/** 和 /mobile/**,我该怎么做? stackoverflow.com/questions/58231064/…
    【解决方案2】:

    antMatcher() 允许将 HttpSecurity 配置为仅在匹配提供的 ant 模式时调用。

    如果需要更高级的配置,请考虑使用 requestMatchers() 或 requestMatcher(RequestMatcher)。

    调用 antMatcher() 将覆盖之前对 antMatcher()、mvcMatcher()、requestMatchers()、regexMatcher( ) 和 requestMatcher()

    请参阅下面使用 requestMatchers 的示例

       @Configuration
       @EnableWebSecurity
       public class RequestMatchersSecurityConfig extends WebSecurityConfigurerAdapter {
      
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .requestMatchers((requestMatchers) ->
                    requestMatchers
                        .antMatchers("/api/**")
                        .antMatchers("/oauth/**")
                )
                .authorizeRequests((authorizeRequests) ->
                    authorizeRequests
                        .antMatchers("/**").hasRole("USER")
                )
                .httpBasic(withDefaults());
        }
       }
    

    下面的配置也和上面的配置一样。

       @Configuration
       @EnableWebSecurity
       public class RequestMatchersSecurityConfig extends WebSecurityConfigurerAdapter {
      
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .requestMatchers((requestMatchers) ->
                    requestMatchers
                        .antMatchers("/api/**")
                )
                .requestMatchers((requestMatchers) ->
                requestMatchers
                    .antMatchers("/oauth/**")
                )
                .authorizeRequests((authorizeRequests) ->
                    authorizeRequests
                        .antMatchers("/**").hasRole("USER")
                )
                .httpBasic(withDefaults());
        }
       }
    

    【讨论】:

      猜你喜欢
      • 2015-04-02
      • 2015-08-29
      • 2019-05-27
      • 2015-12-22
      • 2020-04-16
      • 1970-01-01
      • 2020-05-24
      • 2018-09-28
      • 2019-04-20
      相关资源
      最近更新 更多