user-details-service.adoc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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, a 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 listing customizes authentication, assuming that `CustomUserDetailsService` implements `UserDetailsService`:
  7. [NOTE]
  8. ====
  9. This is only used if the `AuthenticationManagerBuilder` has not been populated and no `AuthenticationProviderBean` is defined.
  10. ====
  11. .Custom UserDetailsService Bean
  12. ====
  13. .Java
  14. [source,java,role="primary"]
  15. ----
  16. @Bean
  17. CustomUserDetailsService customUserDetailsService() {
  18. return new CustomUserDetailsService();
  19. }
  20. ----
  21. .XML
  22. [source,java,role="secondary"]
  23. ----
  24. <b:bean class="example.CustomUserDetailsService"/>
  25. ----
  26. .Kotlin
  27. [source,kotlin,role="secondary"]
  28. ----
  29. @Bean
  30. fun customUserDetailsService() = CustomUserDetailsService()
  31. ----
  32. ====
  33. // FIXME: Add CustomUserDetails example with links to @AuthenticationPrincipal