在Spring安全性中registerMlobal(),configure(),configureGlobal(),configureGlobalSecurity之间的区别

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

  1. registerGlobal
  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:

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

你把它命名为什么,它的工作原理

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

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

选项1:仅使用注释 (它涵盖您的示例1,3和4 – 请注意您未在样本中包含相关注释)

registerGlobalconfigureGlobalconfigureGlobalSecurity完全相同的做事方式。 您可以根据自己的喜好命名方法。 唯一的限制是:

  • 使用@Autowired注释方法
  • 该方法必须位于使用以下内容之一注释的类中: @ EnableWebSecurity , @ EnableWebMvcSecurity , @ EnableGlobalMethodSecurity或@EnableGlobalAuthentication
  • (当然,该方法的参数类型为AuthenticationManagerBuilder

(因为你可以看到方法的名称并不重要,这就是为什么你在谷歌搜索代码样本时发现了这么多不同的方法名称)

以下是它的外观示例:

 @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)

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

如何选择正确的方法?

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

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


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

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

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

这里有什么重要的:你的类已经扩展了一个Adapter类,所以你不能扩展另一个类

现在,您需要添加安全配置。 您可以选择将其包含在现有的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"); } } 

区别: 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 )提供的方法 – 我想这只是一种更类型的保存方法,但其结果没有区别。