|
@@ -1,26 +1,37 @@
|
|
|
[[servlet-authentication-userdetailsservice]]
|
|
|
= UserDetailsService
|
|
|
|
|
|
+{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`.
|
|
|
+
|
|
|
You can define custom authentication by exposing a custom `UserDetailsService` as a bean.
|
|
|
-For example, the following will customize authentication assuming that `SpringDataUserDetailsService` implements `UserDetailsService`:
|
|
|
+For example, the following will customize authentication assuming that `CustomUserDetailsService` implements `UserDetailsService`:
|
|
|
|
|
|
NOTE: This is only used if the `AuthenticationManagerBuilder` has not been populated and no `AuthenticationProviderBean` is defined.
|
|
|
|
|
|
-[source,java]
|
|
|
+.Custom UserDetailsService Bean
|
|
|
+====
|
|
|
+.Java
|
|
|
+[source,java,role="primary"]
|
|
|
----
|
|
|
@Bean
|
|
|
-public SpringDataUserDetailsService springDataUserDetailsService() {
|
|
|
- return new SpringDataUserDetailsService();
|
|
|
+CustomUserDetailsService customUserDetailsService() {
|
|
|
+ return new CustomUserDetailsService();
|
|
|
}
|
|
|
----
|
|
|
|
|
|
-You can also customize how passwords are encoded by exposing a `PasswordEncoder` as a bean.
|
|
|
-For example, if you use bcrypt you can add a bean definition as shown below:
|
|
|
+.XML
|
|
|
+[source,java,role="secondary"]
|
|
|
+----
|
|
|
+<b:bean class="example.CustomUserDetailsService"/>
|
|
|
+----
|
|
|
|
|
|
-[source,java]
|
|
|
+.Kotlin
|
|
|
+[source,kotlin,role="secondary"]
|
|
|
----
|
|
|
@Bean
|
|
|
-public BCryptPasswordEncoder passwordEncoder() {
|
|
|
- return new BCryptPasswordEncoder();
|
|
|
-}
|
|
|
+fun customUserDetailsService() = CustomUserDetailsService()
|
|
|
----
|
|
|
+====
|
|
|
+
|
|
|
+// FIXME: Add CustomUserDetails example with links to @AuthenticationPrincipal
|