簡體   English   中英

在Spring安全性中registerGlobal(),configure(),configureGlobal(),configureGlobalSecurity之間的區別

[英]Difference between registerGlobal(), configure(), configureGlobal(),configureGlobalSecurity in Spring security

我有三個代碼片段都在做同樣的事情:創建內存中的身份驗證。 那么它如何影響在不同的方法名稱中定義它?

  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:

我將示例項目中的方法重命名為以下內​​容,令我驚訝的是它正在對用戶進行工作和身份驗證。

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

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

實際上,您只有2種不同的選擇。

選項1:僅使用注釋 (它涵蓋您的示例1,3和4 - 請注意您未在樣本中包含相關注釋)

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

(因為你可以看到方法的名稱並不重要,這就是為什么你在谷歌搜索代碼樣本時發現了這么多不同的方法名稱)

以下是它的外觀示例:

@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 )提供的方法 - 我想這只是一種更類型的保存方法,但其結果沒有區別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM