user-details-service.adoc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. [[servlet-authentication-userdetailsservice]]
  2. = UserDetailsService
  3. {security-api-url}org/springframework/security/core/userdetails/UserDetailsService.html[`UserDetailsService`] is used by xref:servlet/authentication/passwords/dao-authentication-provider.adoc#servlet-authentication-daoauthenticationprovider[`DaoAuthenticationProvider`] for retrieving a username, password, and other attributes for authenticating with a username and password.
  4. Spring Security provides xref:servlet/authentication/passwords/in-memory.adoc#servlet-authentication-inmemory[in-memory] and xref:servlet/authentication/passwords/jdbc.adoc#servlet-authentication-jdbc[JDBC] implementations of `UserDetailsService`.
  5. You can define custom authentication by exposing a custom `UserDetailsService` as a bean.
  6. For example, the following will customize authentication assuming that `CustomUserDetailsService` implements `UserDetailsService`:
  7. NOTE: This is only used if the `AuthenticationManagerBuilder` has not been populated and no `AuthenticationProviderBean` is defined.
  8. .Custom UserDetailsService Bean
  9. ====
  10. .Java
  11. [source,java,role="primary"]
  12. ----
  13. @Bean
  14. CustomUserDetailsService customUserDetailsService() {
  15. return new CustomUserDetailsService();
  16. }
  17. ----
  18. .XML
  19. [source,java,role="secondary"]
  20. ----
  21. <b:bean class="example.CustomUserDetailsService"/>
  22. ----
  23. .Kotlin
  24. [source,kotlin,role="secondary"]
  25. ----
  26. @Bean
  27. fun customUserDetailsService() = CustomUserDetailsService()
  28. ----
  29. ====
  30. // FIXME: Add CustomUserDetails example with links to @AuthenticationPrincipal