bei*_*man 12 java spring-security
我正在开发基于Spring Security Java的配置.
我创建了自己的MyAuthenticationProvider
,我想在ProviderManager
(单个实例AuthenticationManager
)中注册.
我发现ProviderManager
有一个提供商列表,我可以注册我的单一
MyAuthenticationProvider
.
这是我配置的一部分:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(MyAuthenticationProvider);
}
}
Run Code Online (Sandbox Code Playgroud)
我发现AuthenticationManagerBuilder
了parentAuthenticationManager
,defaultUserDetailsService
等诸多领域.
我的问题是:
@Autowired
注释在哪里添加AuthenticationManagerBuilder
auth?是否AuthenticationManagerBuilder
已在应用程序上下文中创建?AuthenticationManagerBuilder
注入的默认状态是什么?默认情况下,状态我的意思是会不会有一些parentAuthenticationManager
,authenticationProvider
在S已经注册AuthenticationManagerBuilder
?auth.authenticationProvider(MyAuthenticationProvider)
,这是否意味着我在添加一个提供商AuthenticationManagerBuilder
?这是什么意思?摘自Spring文档
configureGlobal方法的名称并不重要.但是,仅在使用@EnableWebSecurity,@ EnableWebMvcSecurity,@ EnableGlobalMethodSecurity或@EnableGlobalAuthentication注释的类中配置AuthenticationManagerBuilder非常重要.否则会产生不可预测的结果.
@EnableWebSecurity
元注释为 @EnableGlobalAuthentication
...
@EnableGlobalAuthentication
@Configuration
public @interface EnableWebSecurity {
...
Run Code Online (Sandbox Code Playgroud)
和@EnableGlobalAuthentication
进口AuthenticationConfiguration
:
...
@Import(AuthenticationConfiguration.class)
@Configuration
public @interface EnableGlobalAuthentication {
}
Run Code Online (Sandbox Code Playgroud)
在 中AuthenticationConfiguration
,您将看到声明了一个AuthenticationManagerBuilder
bean:
...
@Bean
public AuthenticationManagerBuilder authenticationManagerBuilder(
ObjectPostProcessor<Object> objectPostProcessor, ApplicationContext context) {
...
}
Run Code Online (Sandbox Code Playgroud)
当你@Autowire
是AuthenticationManagerBuilder
,这就是你会得到的。您可以使用多种方法轻松配置内存中、jdbc、ldap、...身份验证。
背景:
Spring Security Java 配置经历了几个阶段以将您的配置与ApplicationContext
.a 中的getHttp()
方法结合在一起WebSecurityConfigurerAdapter
。
例如,这是一个摘录:
AuthenticationManager authenticationManager = authenticationManager();
authenticationBuilder.parentAuthenticationManager(authenticationManager);
Run Code Online (Sandbox Code Playgroud)
为了让您了解配置序列的“不直接”程度,上面的 authenticationManager 变量将是:
configure(AuthenticationManagerBuilder auth)
@Autowired
的AuthenticationManagerBuilder
bean的方法中添加的身份验证管理器默认状态下我的意思是会有一些 [...] authenticationProviders 已经注册在
AuthenticationManagerBuilder
如果您查看AuthenticationConfiguration
,您会看到默认情况下,InitializeUserDetailsBeanManagerConfigurer
应用于AuthenticationManagerBuilder
bean。只要它UserDetailsService
在上下文中找到一个bean 并且没有添加其他提供者,它就会添加一个DaoAuthenticationProvider
. 这就是为什么在Spring Security 参考中,只提供一个 @Bean UserDetailsService
bean 就足够了。
但是,一旦您添加了身份验证提供程序,“默认”提供程序就不会注册。
回答3:
是的。AuthenticationManagerBuilder 的代码添加您的提供程序:
public AuthenticationManagerBuilder authenticationProvider(AuthenticationProvider authenticationProvider) {
this.authenticationProviders.add(authenticationProvider);
return this;
}
Run Code Online (Sandbox Code Playgroud)
4的答案很简单:
这意味着,一旦您拥有该注释之一,您就可以根据需要命名您的方法:
@Configuration
@EnableWebSecurity //or @EnableWebMvcSecurity or @EnableGlobalMethodSecurity....
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void myCoolMethodName(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(MyAuthenticationProvider);
}
}
Run Code Online (Sandbox Code Playgroud)
“不这样做会产生不可预测的结果”
如果保留名称但不保留注释,则可能不起作用。
归档时间: |
|
查看次数: |
15647 次 |
最近记录: |