2019-12-06 10:39:55 -06:00
[[servlet-authentication-userdetailsservice]]
2020-01-13 15:46:50 -06:00
= UserDetailsService
2019-09-22 00:56:30 -06:00
2020-03-02 11:56:56 -06:00
{security-api-url}org/springframework/security/core/userdetails/UserDetailsService.html[`UserDetailsService`] is used by <<servlet-authentication-daoauthenticationprovider,`DaoAuthenticationProvider`>> for retrieving a username, password, and other attributes for authenticating with a username and password.
Spring Security provides <<servlet-authentication-inmemory,in-memory>> and <<servlet-authentication-jdbc,JDBC>> implementations of `UserDetailsService`.
2019-09-22 00:56:30 -06:00
You can define custom authentication by exposing a custom `UserDetailsService` as a bean.
2020-03-02 11:56:56 -06:00
For example, the following will customize authentication assuming that `CustomUserDetailsService` implements `UserDetailsService`:
2019-09-22 00:56:30 -06:00
NOTE: This is only used if the `AuthenticationManagerBuilder` has not been populated and no `AuthenticationProviderBean` is defined.
2020-03-02 11:56:56 -06:00
.Custom UserDetailsService Bean
====
.Java
[source,java,role="primary"]
2019-09-22 00:56:30 -06:00
----
@Bean
2020-03-02 11:56:56 -06:00
CustomUserDetailsService customUserDetailsService() {
return new CustomUserDetailsService();
2019-09-22 00:56:30 -06:00
}
----
2020-03-02 11:56:56 -06:00
.XML
[source,java,role="secondary"]
----
<b:bean class="example.CustomUserDetailsService"/>
----
2019-09-22 00:56:30 -06:00
2020-03-02 11:56:56 -06:00
.Kotlin
[source,kotlin,role="secondary"]
2019-09-22 00:56:30 -06:00
----
@Bean
2020-03-02 11:56:56 -06:00
fun customUserDetailsService() = CustomUserDetailsService()
2019-09-22 00:56:30 -06:00
----
2020-03-02 11:56:56 -06:00
====
// FIXME: Add CustomUserDetails example with links to @AuthenticationPrincipal