何时使用Spring Security的antMatcher()?

sur*_*a2k 39 spring-mvc spring-security spring-security4

我们什么时候使用antMatcher()vs 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()
   ...
Run Code Online (Sandbox Code Playgroud)

我期待的是,

  • 任何匹配的请求都/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()不再需要?

dur*_*dur 52

您需要antMatcher多个HttpSecurity,请参阅Spring Security Reference:

5.7多个HttpSecurity

我们可以配置多个HttpSecurity实例,就像我们可以有多个<http>块一样.关键是要WebSecurityConfigurationAdapter多次延长.例如,以下是对以URL开头的不同配置的示例/api/.

@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();
      }
  }
}
Run Code Online (Sandbox Code Playgroud)

1正常配置身份验证

2创建WebSecurityConfigurerAdapter包含的实例@Order以指定WebSecurityConfigurerAdapter应首先考虑的实例.

3 http.antMatcher表明HttpSecurity这只适用于以.开头的网址/api/

4创建另一个实例WebSecurityConfigurerAdapter.如果URL未/api/以此配置启动,则将使用此配置.之后考虑此配置,ApiWebSecurityConfigurationAdapter因为它具有之后的@Order1(无@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()
Run Code Online (Sandbox Code Playgroud)


Pat*_*ard 35

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

antMatcher()是一种方法HttpSecurity,它没有任何关系authorizeRequests().基本上,http.antMatcher()告诉Spring只配置HttpSecurity路径是否匹配此模式.

authorizeRequests().antMatchers()则使用申请授权,你指定一个或多个路径antMatchers().如permitAll()hasRole('USER3').只有在第一个http.antMatcher()匹配时才会应用这些.

  • @AngelO'Sphere 这是对 Apache 的 Ant 构建工具的引用。它有一种处理模式匹配的独特方式,“antMatcher”借鉴了这种方式。https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html (9认同)
  • antMatcher中的“ ant”代表什么? (2认同)

小智 8

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