Skip to content

Commit 0ba3ff6

Browse files
committed
Update UserDetailsService Docs
Closes gh-8048
1 parent 2ce9eef commit 0ba3ff6

File tree

7 files changed

+63
-15
lines changed

7 files changed

+63
-15
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[[servlet-authentication-daoauthenticationprovider]]
2+
= DaoAuthenticationProvider
3+
4+
{security-api-url}org/springframework/security/authentication/dao/DaoAuthenticationProvider.html[`DaoAuthenticationProvider`] is an <<servlet-authentication-authenticationprovider,`AuthenticationProvider`>> implementation that leverages a <<servlet-authentication-userdetailsservice,`UserDetailsService`>> and <<servlet-authentication-password-storage,`PasswordEncoder`>> to authenticate a username and password.
5+
6+
Let's take a look at how `DaoAuthenticationProvider` works within Spring Security.
7+
The figure explains details of how the <<servlet-authentication-authenticationmanager,`AuthenticationManager`>> in figures from <<servlet-authentication-unpwd-input,Reading the Username & Password>> works.
8+
9+
.`DaoAuthenticationProvider` Usage
10+
image::{figures}/daoauthenticationprovider.png[]
11+
12+
image:{icondir}/number_1.png[] The authentication `Filter` from <<servlet-authentication-unpwd-input,Reading the Username & Password>> passes a `UsernamePasswordAuthenticationToken` to the `AuthenticationManager` which is implemented by <<servlet-authentication-providermanager,`ProviderManager`>>.
13+
14+
image:{icondir}/number_2.png[] The `ProviderManager` is configured to use an <<servlet-authentication-authenticationprovider>> of type `DaoAuthenticationProvider`.
15+
16+
image:{icondir}/number_3.png[] `DaoAuthenticationProvider` looks up the `UserDetails` from the `UserDetailsService`.
17+
18+
image:{icondir}/number_4.png[] `DaoAuthenticationProvider` then uses the <<servlet-authentication-password-storage,`PasswordEncoder`>> to validate the password on the `UserDetails` returned in the previous step.
19+
20+
image:{icondir}/number_5.png[] When authentication is successful, the <<servlet-authentication-authentication,`Authentication`>> that is returned is of type `UsernamePasswordAuthenticationToken` and has a principal that is the `UserDetails` returned by the configured `UserDetailsService`.
21+
Ultimately, the returned `UsernamePasswordAuthenticationToken` will be set on the <<servlet-authentication-securitycontextholder,`SecurityContextHolder`>> by the authentication `Filter`.

docs/manual/src/docs/asciidoc/_includes/servlet/authentication/unpwd/index.adoc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@ One of the most common ways to authenticate a user is by validating a username a
77
As such, Spring Security provides comprehensive support for authenticating with a username and password.
88

99
[[servlet-authentication-unpwd-input]]
10+
*Reading the Username & Password*
11+
1012
Spring Security provides the following built in mechanisms for reading a username and password from the `HttpServletRequest`:
1113

1214
* <<servlet-authentication-form,Form Login>>
1315
* <<servlet-authentication-basic,Basic Authentication>>
1416
* <<servlet-authentication-digest,Digest Authentication>>
1517

1618
[[servlet-authentication-unpwd-storage]]
19+
*Storage Mechanisms*
20+
1721
Each of the supported mechanisms for reading a username and password can leverage any of the supported storage mechanisms:
1822

1923
* Simple Storage with <<servlet-authentication-inmemory>>
2024
* Relational Databases with <<servlet-authentication-jdbc>>
21-
* LDAP Servers with <<servlet-authentication-ldap>>
2225
* Custom data stores with <<servlet-authentication-userdetailsservice>>
26+
* LDAP storage with <<servlet-authentication-ldap>>
2327

2428
include::form.adoc[leveloffset=+1]
2529

@@ -31,6 +35,12 @@ include::in-memory.adoc[leveloffset=+1]
3135

3236
include::jdbc.adoc[leveloffset=+1]
3337

34-
include::ldap.adoc[leveloffset=+1]
38+
include::user-details.adoc[leveloffset=+1]
3539

3640
include::user-details-service.adoc[leveloffset=+1]
41+
42+
include::password-encoder.adoc[leveloffset=+1]
43+
44+
include::dao-authentication-provider.adoc[leveloffset=+1]
45+
46+
include::ldap.adoc[leveloffset=+1]
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
[[servlet-password-storage]]
2-
= Password Storage
1+
[[servlet-authentication-password-storage]]
2+
= PasswordEncoder
33

4-
Spring Security provides
4+
Spring Security's servlet support storing passwords securely by integrating with <<authentication-password-storage,`PasswordEncoder`>>.
5+
Customizing the `PasswordEncoder` implementation used by Spring Security can be done by <<authentication-password-storage-configuration,exposing a `PasswordEncoder` Bean>>.
Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
[[servlet-authentication-userdetailsservice]]
22
= UserDetailsService
33

4+
{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.
5+
Spring Security provides <<servlet-authentication-inmemory,in-memory>> and <<servlet-authentication-jdbc,JDBC>> implementations of `UserDetailsService`.
6+
47
You can define custom authentication by exposing a custom `UserDetailsService` as a bean.
5-
For example, the following will customize authentication assuming that `SpringDataUserDetailsService` implements `UserDetailsService`:
8+
For example, the following will customize authentication assuming that `CustomUserDetailsService` implements `UserDetailsService`:
69

710
NOTE: This is only used if the `AuthenticationManagerBuilder` has not been populated and no `AuthenticationProviderBean` is defined.
811

9-
[source,java]
12+
.Custom UserDetailsService Bean
13+
====
14+
.Java
15+
[source,java,role="primary"]
1016
----
1117
@Bean
12-
public SpringDataUserDetailsService springDataUserDetailsService() {
13-
return new SpringDataUserDetailsService();
18+
CustomUserDetailsService customUserDetailsService() {
19+
return new CustomUserDetailsService();
1420
}
1521
----
1622
17-
You can also customize how passwords are encoded by exposing a `PasswordEncoder` as a bean.
18-
For example, if you use bcrypt you can add a bean definition as shown below:
23+
.XML
24+
[source,java,role="secondary"]
25+
----
26+
<b:bean class="example.CustomUserDetailsService"/>
27+
----
1928
20-
[source,java]
29+
.Kotlin
30+
[source,kotlin,role="secondary"]
2131
----
2232
@Bean
23-
public BCryptPasswordEncoder passwordEncoder() {
24-
return new BCryptPasswordEncoder();
25-
}
33+
fun customUserDetailsService() = CustomUserDetailsService()
2634
----
35+
====
36+
37+
// FIXME: Add CustomUserDetails example with links to @AuthenticationPrincipal
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[[servlet-authentication-userdetails]]
2+
= UserDetails
3+
4+
{security-api-url}org/springframework/security/core/userdetails/UserDetails.html[`UserDetails`] is returned by the <<servlet-authentication-userdetailsservice,`UserDetailsService`>>.
5+
The <<servlet-authentication-daoauthenticationprovider,`DaoAuthenticationProvider`>> validates the `UserDetails` and then returns an <<servlet-authentication-authentication,`Authentication`>> that has a principal that is the `UserDetails` returned by the configured `UserDetailsService`.

0 commit comments

Comments
 (0)