Spring Security Java Config-自定义AuthenticationProvider和UserDetailsService [英] Spring Security Java Config - custom AuthenticationProvider and UserDetailsService
问题描述
我使用java配置来配置Spring Security,并且自定义了AuthenticationProvider和UserDetailsService,以在下面添加额外的登录字段 http://forum.spring.io/forum/spring-projects/security/95715-extra-login-fields
I use java configuration to configure Spring Security, and I have customized AuthenticationProvider and customized UserDetailsService, to add extra login field following http://forum.spring.io/forum/spring-projects/security/95715-extra-login-fields
我很难通过使用java配置将两个定制的类添加到Spring Security框架中. 如AuthenticationProvider#authenticationProvider的Java文档所述:
I have difficulty to add both of the customized Classes into Spring Security framework by using java configuration. As java doc of AuthenticationProvider#authenticationProvider describes:
基于自定义AuthenticationProvider添加身份验证,该身份验证提供者 传入.由于AuthenticationProvider实现是 未知,所有自定义操作都必须在外部进行,并且 AuthenticationManagerBuilder立即返回.
Add authentication based upon the custom AuthenticationProvider that is passed in. Since the AuthenticationProvider implementation is unknown, all customizations must be done externally and the AuthenticationManagerBuilder is returned immediately.
此方法不能确保UserDetailsService可用 用于getDefaultUserDetailsService()方法.
This method does NOT ensure that the UserDetailsService is available for the getDefaultUserDetailsService() method.
所以我的问题是在这种情况下设置UserDetailsService的方法是什么?
So my question is what is the approach to set UserDetailsService in this case?
推荐答案
以下是自定义AuthenticationProvider和自定义UserDetailsService的示例:
Here is an example of customized AuthenticationProvider and customized UserDetailsService:
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void registerGlobalAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthenticationProvider());
}
@Bean
AuthenticationProvider customAuthenticationProvider() {
CustomAuthenticationProvider impl = new CustomAuthenticationProvider();
impl.setUserDetailsService(customUserDetailsService());
/* other properties etc */
return impl ;
}
@Bean
UserDetailsService customUserDetailsService() {
/* custom UserDetailsService code here */
}
}
这篇关于Spring Security Java Config-自定义AuthenticationProvider和UserDetailsService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!