servlet-authentication-passwords-storage-user-details-service.md 1.4 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# UserDetailsService

[`UserDetailsService`](https://docs.spring.io/spring-security/site/docs/5.6.2/api/org/springframework/security/core/userdetails/UserDetailsService.html) is used by [`DaoAuthenticationProvider`](dao-authentication-provider.html#servlet-authentication-daoauthenticationprovider) for retrieving a username, password, and other attributes for authenticating with a username and password.
Spring Security provides [in-memory](in-memory.html#servlet-authentication-inmemory) and [JDBC](jdbc.html#servlet-authentication-jdbc) implementations of `UserDetailsService`.

You can define custom authentication by exposing a custom `UserDetailsService` as a bean.
For example, the following will customize authentication assuming that `CustomUserDetailsService` implements `UserDetailsService`:

|   |This is only used if the `AuthenticationManagerBuilder` has not been populated and no `AuthenticationProviderBean` is defined.|
|---|------------------------------------------------------------------------------------------------------------------------------|

Example 1. Custom UserDetailsService Bean

Java

```
@Bean
CustomUserDetailsService customUserDetailsService() {
	return new CustomUserDetailsService();
}
```

XML

```
<b:bean class="example.CustomUserDetailsService"/>
```

Kotlin

```
@Bean
fun customUserDetailsService() = CustomUserDetailsService()
```

[UserDetails](user-details.html)[PasswordEncoder](password-encoder.html)