spring-security/docs/modules/ROOT/pages/servlet/authentication/passwords/user-details-service.adoc

41 lines
1.5 KiB
Plaintext
Raw Normal View History

[[servlet-authentication-userdetailsservice]]
= UserDetailsService
2019-09-22 02:56:30 -04:00
{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.
2021-08-25 14:31:00 -04:00
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`.
2019-09-22 02:56:30 -04:00
You can define custom authentication by exposing a custom `UserDetailsService` as a bean.
For example, the following listing customizes authentication, assuming that `CustomUserDetailsService` implements `UserDetailsService`:
2019-09-22 02:56:30 -04:00
[NOTE]
====
This is only used if the `AuthenticationManagerBuilder` has not been populated and no `AuthenticationProviderBean` is defined.
====
2019-09-22 02:56:30 -04:00
.Custom UserDetailsService Bean
====
.Java
[source,java,role="primary"]
2019-09-22 02:56:30 -04:00
----
@Bean
CustomUserDetailsService customUserDetailsService() {
return new CustomUserDetailsService();
2019-09-22 02:56:30 -04:00
}
----
.XML
[source,java,role="secondary"]
----
<b:bean class="example.CustomUserDetailsService"/>
----
2019-09-22 02:56:30 -04:00
.Kotlin
[source,kotlin,role="secondary"]
2019-09-22 02:56:30 -04:00
----
@Bean
fun customUserDetailsService() = CustomUserDetailsService()
2019-09-22 02:56:30 -04:00
----
====
// FIXME: Add CustomUserDetails example with links to @AuthenticationPrincipal