user-details-service.adoc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. [tabs]
  10. ======
  11. Java::
  12. +
  13. [source,java,role="primary"]
  14. ----
  15. @Bean
  16. CustomUserDetailsService customUserDetailsService() {
  17. return new CustomUserDetailsService();
  18. }
  19. ----
  20. XML::
  21. +
  22. [source,java,role="secondary"]
  23. ----
  24. <b:bean class="example.CustomUserDetailsService"/>
  25. ----
  26. Kotlin::
  27. +
  28. [source,kotlin,role="secondary"]
  29. ----
  30. @Bean
  31. fun customUserDetailsService() = CustomUserDetailsService()
  32. ----
  33. ======
  34. // FIXME: Add CustomUserDetails example with links to @AuthenticationPrincipal