Spring Security Java Config - custom AuthenticationProvider and UserDetailsService

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

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

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:

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.

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

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

回答1:

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 */     } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!