RegisterGlobal(), configure(), configureGlobal(), configureGlobal Security in Spring Security 之间的区别
我有以下三个代码片段都做同样的事情:创建内存中身份验证。那么它如何影响在不同的方法名称中定义它呢?
- 注册全球
- 配置
- configureGlobal
- 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");
}