2016-01-26 257 views
4

我玩弄春季安全配置和發現,是最常見的方式來配置內存認證使用configureGlobal()方法:'configure'和'configureGlobal'方法有什麼區別?

@Configuration 
@EnableWebMvcSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

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

但還有另一種方式,這是不太廣泛使用,從WebSecurityConfigurerAdapter首要configure()方法:

@Configuration 
@EnableWebMvcSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

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

我只是想知道,什麼是它們之間的區別,什麼是使用configureGlobal()方法在configure()一個點?

回答

1

正如spring security doc說:

configureGlobal方法的名稱並不重要。然而,其 對於僅在類別 中配置AuthenticationManagerBuilder是重要的,其用@EnableWebSecurity,@EnableGlobalMethodSecurity, 或@EnableGlobalAuthentication註釋。否則會產生難以預料的 結果。

相關問題