【问题标题】:When to use Spring Security`s antMatcher()?何时使用 Spring Security 的 antMatcher()?
【发布时间】:2016-06-23 18:22:42
【问题描述】:

我们什么时候使用antMatcher()antMatchers()

例如:

http
   .antMatcher("/high_level_url_A/**")
   .authorizeRequests()
      .antMatchers("/high_level_url_A/sub_level_1").hasRole('USER')
      .antMatchers("/high_level_url_A/sub_level_2").hasRole('USER2')
      .somethingElse()
      .anyRequest().authenticated()
      .and()
   .antMatcher("/high_level_url_B/**")
   .authorizeRequests()
      .antMatchers("/high_level_url_B/sub_level_1").permitAll()
      .antMatchers("/high_level_url_B/sub_level_2").hasRole('USER3')
      .somethingElse()
      .anyRequest().authenticated()
      .and()
   ...

我期待的是,

  • 任何与/high_level_url_A/** 匹配的请求都应经过身份验证 + /high_level_url_A/sub_level_1 仅适用于 USER,/high_level_url_A/sub_level_2 仅适用于 USER2
  • 任何与 /high_level_url_B/** 匹配的请求都应经过身份验证 + /high_level_url_B/sub_level_1 用于公共访问,/high_level_url_A/sub_level_2 仅用于 USER3。
  • 我不关心的任何其他模式 - 但应该公开吗?

这些天我看到最新的例子不包括antMatcher()。这是为什么? antMatcher() 是否不再需要?

【问题讨论】:

    标签: spring-mvc spring-security spring-security4


    【解决方案1】:

    基本上http.antMatcher() 告诉 Spring 仅在路径匹配此模式时才配置 HttpSecurity

    【讨论】:

      【解决方案2】:

      多个HttpSecurity需要antMatcher,见Spring Security Reference

      5.7 多个 HttpSecurity

      我们可以配置多个 HttpSecurity 实例,就像我们可以拥有多个 <http> 块一样。关键是多次扩展WebSecurityConfigurationAdapter。例如,以下是对以 /api/ 开头的 URL 进行不同配置的示例。

      @EnableWebSecurity
      public class MultiHttpSecurityConfig {
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) { 1
            auth
                .inMemoryAuthentication()
                    .withUser("user").password("password").roles("USER").and()
                    .withUser("admin").password("password").roles("USER", "ADMIN");
        }
      
        @Configuration
        @Order(1)                                                        2
        public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
            protected void configure(HttpSecurity http) throws Exception {
                http
                    .antMatcher("/api/**")                               3
                    .authorizeRequests()
                        .anyRequest().hasRole("ADMIN")
                        .and()
                    .httpBasic();
            }
        }    
      
        @Configuration                                                   4
        public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
      
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http
                    .authorizeRequests()
                        .anyRequest().authenticated()
                        .and()
                    .formLogin();
            }
        }
      }
      

      1 正常配置身份验证

      2 创建一个包含@OrderWebSecurityConfigurerAdapter 实例,以指定应首先考虑哪个WebSecurityConfigurerAdapter

      3 http.antMatcher 声明此 HttpSecurity 仅适用于以 /api/ 开头的 URL

      4 创建WebSecurityConfigurerAdapter 的另一个实例。如果 URL 不以 /api/ 开头,将使用此配置。此配置在ApiWebSecurityConfigurationAdapter 之后考虑,因为它在1 之后有一个@Order 值(没有@Order 默认为最后一个)。

      在您的情况下,您不需要antMatcher,因为您只有一种配置。您修改后的代码:

      http
          .authorizeRequests()
              .antMatchers("/high_level_url_A/sub_level_1").hasRole('USER')
              .antMatchers("/high_level_url_A/sub_level_2").hasRole('USER2')
              .somethingElse() // for /high_level_url_A/**
              .antMatchers("/high_level_url_A/**").authenticated()
              .antMatchers("/high_level_url_B/sub_level_1").permitAll()
              .antMatchers("/high_level_url_B/sub_level_2").hasRole('USER3')
              .somethingElse() // for /high_level_url_B/**
              .antMatchers("/high_level_url_B/**").authenticated()
              .anyRequest().permitAll()
      

      【讨论】:

        【解决方案3】:

        我正在更新我的答案...

        antMatcher()HttpSecurity的一个方法,和authorizeRequests()没有任何关系。基本上,http.antMatcher() 告诉 Spring 仅在路径匹配此模式时配置 HttpSecurity

        然后使用authorizeRequests().antMatchers() 将授权应用于您在antMatchers() 中指定的一个或多个路径。如permitAll()hasRole('USER3')。这些只有在第一个 http.antMatcher() 匹配时才会应用。

        【讨论】:

        猜你喜欢
        • 2018-06-26
        • 2021-04-21
        • 2020-01-18
        • 2019-11-15
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 2018-04-28
        • 1970-01-01
        相关资源
        最近更新 更多