user-details-service.adoc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. [[servlet-authentication-userdetailsservice]]
  2. = UserDetailsService
  3. javadoc:org.springframework.security.core.userdetails.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], xref:servlet/authentication/passwords/jdbc.adoc#servlet-authentication-jdbc[JDBC], and xref:servlet/authentication/passwords/caching.adoc#servlet-authentication-caching-user-details[caching] 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. [tabs]
  13. ======
  14. Java::
  15. +
  16. [source,java,role="primary"]
  17. ----
  18. @Bean
  19. CustomUserDetailsService customUserDetailsService() {
  20. return new CustomUserDetailsService();
  21. }
  22. ----
  23. XML::
  24. +
  25. [source,java,role="secondary"]
  26. ----
  27. <b:bean class="example.CustomUserDetailsService"/>
  28. ----
  29. Kotlin::
  30. +
  31. [source,kotlin,role="secondary"]
  32. ----
  33. @Bean
  34. fun customUserDetailsService() = CustomUserDetailsService()
  35. ----
  36. ======
  37. // FIXME: Add CustomUserDetails example with links to @AuthenticationPrincipal