RegisterGlobal(), configure(), configureGlobal(), configureGlobal Security in Spring Security 之间的区别

2022-09-01 00:35:06

我有以下三个代码片段都做同样的事情:创建内存中身份验证。那么它如何影响在不同的方法名称中定义它呢?

  1. 注册全球
  2. 配置
  3. configureGlobal
  4. configureGlobalSecurity

第一个:

public void registerGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
      .inMemoryAuthentication()
        .withUser("user").password("password").roles("USER").and()
        .withUser("admin").password("password").roles("USER","ADMIN");
    }
}

第二个:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
         .inMemoryAuthentication()
              .withUser("user").password("password").roles("USER");
 }

第三个:

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
         .inMemoryAuthentication()
              .withUser("user").password("password").roles("USER");
}

第四:

@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth)     throws Exception {
    auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");
}

更新 1 :还有一件事我想补充一点:

configure() 方法存在于 WebSecurityConfigurerAdapter 类中,而其他方法不存在。

更新 2:

我将示例项目中的方法重命名为下面,令我惊讶的是,它正在工作并对用户进行身份验证。

你可以给它起任何名字,它的工作原理

@Autowired
public void anyMethodName(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");      
}

答案 1

实际上,您只有2个不同的选择。

选项 1:仅使用批注(它涵盖了示例 1、3 和 4 - 请注意,您未在示例中包含相关批注)

registerGlobal、、是完全相同的做事方式。您可以根据自己的喜好命名方法。唯一的约束是:configureGlobalconfigureGlobalSecurity

(如您所见,方法的名称并不重要,这就是为什么您在谷歌搜索代码示例时发现这么多不同的方法名称的原因)

下面是它的外观示例:

@EnableWebSecurity
public class MyConfiguration {

    @Autowired
    public void whatever(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
          .withUser("user").password("password").roles("USER").and()
          .withUser("admin").password("password").roles("USER", "ADMIN");
    }

    ...

}

选项 2:使用注释 + 方法覆盖(它涵盖了您的示例 2)

WebSecurityConfigurerAdapter(或任何实现WebSecurityConfigurer的@Configuration类)的子类中,覆盖是一种方便的方法,但它具有与其他选项相同的效果。configure


如何选择正确的方法?

这只是一个品味/编程风格的问题,因为这两种方法具有相同的效果。

当您希望/需要将配置保留在单个类中,但是您的@Configuration类已经扩展了其他类(并且您不想实现整个WebSecurityConfigurer接口)时,第一个选项是有意义的。


让我们更详细地解释我的最后一点。Spring 提供了许多适配器类,您可以扩展这些类以加快 Spring 配置的开发。

举个例子,让我们看一个常用的适配器:WebMvcConfigurerAdapter。您将从非常简单的配置开始,如下所示:

@EnableWebMvc
@Configuration
@ComponentScan({ "com.company.mypackage" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {

}

这里重要的是:您的类已经扩展了一个适配器类,因此您无法扩展另一个适配器类


现在,您需要添加安全配置。您可以选择将其包含在现有配置类中,也可以创建新的特定于安全性的配置类。以下是这两种方法的示例:SpringWebConfig

1) 单@Configuration类方法

这里需要注意的是:SpringWebConfig扩展了WebMvcConfigurerAdapter + @EnableWebSecurity

@EnableWebMvc
@Configuration
@ComponentScan({ "com.company.mypackage" })
@EnableWebSecurity
public class SpringWebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    public void whatever(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
          .withUser("user").password("password").roles("USER").and()
          .withUser("admin").password("password").roles("USER", "ADMIN");
    }     
}


2)特定安全@Configuration类

这里需要注意的是:MySecurityConfig扩展了WebSecurityConfigurerAdapter

保持你的SpringWebConfig原样,并创建一个新类:@Configuration

@Configuration
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
    @Overide
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
          .withUser("user").password("password").roles("USER").and()
          .withUser("admin").password("password").roles("USER", "ADMIN");
    }
}

答案 2

对于以下各项之间的区别:和registerGlobal(AuthenticationManagerBuilder auth)configureGlobal(AuthenticationManagerBuilder auth)

configureGlobal 方法的名称并不重要。但是,仅在使用@EnableWebSecurity、@EnableWebMvcSecurity、@EnableGlobalMethodSecurity或@EnableGlobalAuthentication注释的类中配置 AuthenticationManagerBuilder 非常重要。否则会产生不可预测的结果。

来源:
“Hello Spring Security Java Config”指南中的“创建您的Spring Security配置”一章。


protected void configure(AuthenticationManagerBuilder auth)是一种可能由WebSecurityConfigurer(及其接口)提供的方法 - 我会说这只是一种更类型的保存方法,但其结果没有区别。WebSecurityConfigurer


推荐