小编典典

何时使用Spring Security的antMatcher()?

spring-mvc

什么时候使用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()
   ...

我期望的是

  • 任何/high_level_url_A/**与之匹配的请求都应通过身份验证+ /high_level_url_A/sub_level_1仅针对USER和/high_level_url_A/sub_level_2USER2
  • 任何/high_level_url_B/**与之匹配的请求都应经过身份验证+, /high_level_url_B/sub_level_1以供公共访问,并且/high_level_url_A/sub_level_2仅适用于USER3。
  • 我不在乎其他任何模式-但应该公开吗?

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


阅读 927

收藏
2020-06-01

共1个答案

小编典典

您需要antMatcher多个HttpSecurity,请参阅SpringSecurity 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();
           }
      }
     }

1正常配置身份验证

2创建一个WebSecurityConfigurerAdapter包含的实例,@Order以指定WebSecurityConfigurerAdapter应首先考虑的对象。

3 http.antMatcher指出这HttpSecurity仅适用于以开头的URL/api/

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

在您的情况下,您不需要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()
2020-06-01