Spring Security Java Configuration - User AuthenticationProvider and UserDetailsService

I use java configuration to configure Spring Security, and I configured AuthenticationProvider and configured UserDetailsService to add an additional login field after http://forum.spring.io/forum/spring-projects/security/95715-extra-login-fields

I find it difficult to add both custom classes to the Spring Security framework using java configuration. How the java authentication document Provider # authenticationProvider describes:

Add authentication based on the custom AuthenticationProvider that is being passed. Since the implementation of AuthenticationProvider is unknown, all settings must be performed externally, and AuthenticationManagerBuilder is returned immediately.

This method does NOT make UserDetailsService available to the getDefaultUserDetailsService () method.

So my question is, what is the approach to setting a UserDetailsService in this case?

+7
spring-java-config spring-security
source share
1 answer

Here is an example of a custom AuthenticationProvider and a custom 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 */ } } 
+4
source share

All Articles