Spring Security Java Config-自定义AuthenticationProvider和UserDetailsS​​ervice [英] Spring Security Java Config - custom AuthenticationProvider and UserDetailsService

查看:332
本文介绍了Spring Security Java Config-自定义AuthenticationProvider和UserDetailsS​​ervice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用java配置来配置Spring Security,并且自定义了AuthenticationProvider和UserDetailsS​​ervice,以在下面添加额外的登录字段 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.

此方法不能确保UserDetailsS​​ervice可用 用于getDefaultUserDetailsS​​ervice()方法.

This method does NOT ensure that the UserDetailsService is available for the getDefaultUserDetailsService() method.

所以我的问题是在这种情况下设置UserDetailsS​​ervice的方法是什么?

So my question is what is the approach to set UserDetailsService in this case?

推荐答案

以下是自定义AuthenticationProvider和自定义UserDetailsS​​ervice的示例:

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和UserDetailsS​​ervice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆