Skip to content

Commit d009a7b

Browse files
committed
Add ProviderManager to Reference
Closes gh-8029
1 parent bd593a3 commit d009a7b

File tree

14 files changed

+53
-67
lines changed

14 files changed

+53
-67
lines changed

docs/manual/src/docs/asciidoc/_includes/servlet/appendix/namespace.adoc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,8 +2039,7 @@ Its use is described in the <<ns-auth-manager,namespace introduction>>.
20392039
[[nsa-authentication-manager-erase-credentials]]
20402040
* **erase-credentials**
20412041
If set to true, the AuthenticationManager will attempt to clear any credentials data in the returned Authentication object, once the user has been authenticated.
2042-
Literally it maps to the `eraseCredentialsAfterAuthentication` property of the `ProviderManager`.
2043-
This is discussed in the <<core-services-erasing-credentials,Core Services>> chapter.
2042+
Literally it maps to the `eraseCredentialsAfterAuthentication` property of the <<servlet-authentication-providermanager,`ProviderManager`>>.
20442043

20452044

20462045
[[nsa-authentication-manager-id]]

docs/manual/src/docs/asciidoc/_includes/servlet/architecture/core-services.adoc

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,8 @@
33
Now that we have a high-level overview of the Spring Security architecture and its core classes, let's take a closer look at one or two of the core interfaces and their implementations, in particular the `AuthenticationManager`, `UserDetailsService` and the `AccessDecisionManager`.
44
These crop up regularly throughout the remainder of this document so it's important you know how they are configured and how they operate.
55

6-
7-
[[core-services-authentication-manager]]
8-
=== The AuthenticationManager, ProviderManager and AuthenticationProvider
9-
The `AuthenticationManager` is just an interface, so the implementation can be anything we choose, but how does it work in practice? What if we need to check multiple authentication databases or a combination of different authentication services such as a database and an LDAP server?
10-
11-
The default implementation in Spring Security is called `ProviderManager` and rather than handling the authentication request itself, it delegates to a list of configured `AuthenticationProvider` s, each of which is queried in turn to see if it can perform the authentication.
12-
Each provider will either throw an exception or return a fully populated `Authentication` object.
13-
Remember our good friends, `UserDetails` and `UserDetailsService`? If not, head back to the previous chapter and refresh your memory.
14-
The most common approach to verifying an authentication request is to load the corresponding `UserDetails` and check the loaded password against the one that has been entered by the user.
15-
This is the approach used by the `DaoAuthenticationProvider` (see below).
16-
The loaded `UserDetails` object - and particularly the `GrantedAuthority` s it contains - will be used when building the fully populated `Authentication` object which is returned from a successful authentication and stored in the `SecurityContext`.
17-
18-
If you are using the namespace, an instance of `ProviderManager` is created and maintained internally, and you add providers to it by using the namespace authentication provider elements (see <<ns-auth-manager,the namespace chapter>>).
19-
In this case, you should not declare a `ProviderManager` bean in your application context.
20-
However, if you are not using the namespace then you would declare it like so:
21-
22-
[source,xml]
23-
----
24-
25-
<bean id="authenticationManager"
26-
class="org.springframework.security.authentication.ProviderManager">
27-
<constructor-arg>
28-
<list>
29-
<ref local="daoAuthenticationProvider"/>
30-
<ref local="anonymousAuthenticationProvider"/>
31-
<ref local="ldapAuthenticationProvider"/>
32-
</list>
33-
</constructor-arg>
34-
</bean>
35-
----
36-
37-
In the above example we have three providers.
38-
They are tried in the order shown (which is implied by the use of a `List`), with each provider able to attempt authentication, or skip authentication by simply returning `null`.
39-
If all implementations return null, the `ProviderManager` will throw a `ProviderNotFoundException`.
40-
If you're interested in learning more about chaining providers, please refer to the `ProviderManager` Javadoc.
41-
42-
Authentication mechanisms such as a web form-login processing filter are injected with a reference to the `ProviderManager` and will call it to handle their authentication requests.
43-
The providers you require will sometimes be interchangeable with the authentication mechanisms, while at other times they will depend on a specific authentication mechanism.
44-
For example, `DaoAuthenticationProvider` and `LdapAuthenticationProvider` are compatible with any mechanism which submits a simple username/password authentication request and so will work with form-based logins or HTTP Basic authentication.
45-
On the other hand, some authentication mechanisms create an authentication request object which can only be interpreted by a single type of `AuthenticationProvider`.
46-
An example of this would be JA-SIG CAS, which uses the notion of a service ticket and so can therefore only be authenticated by a `CasAuthenticationProvider`.
47-
You needn't be too concerned about this, because if you forget to register a suitable provider, you'll simply receive a `ProviderNotFoundException` when an attempt to authenticate is made.
48-
49-
50-
[[core-services-erasing-credentials]]
51-
==== Erasing Credentials on Successful Authentication
52-
By default (from Spring Security 3.1 onwards) the `ProviderManager` will attempt to clear any sensitive credentials information from the `Authentication` object which is returned by a successful authentication request.
53-
This prevents information like passwords being retained longer than necessary.
54-
55-
This may cause issues when you are using a cache of user objects, for example, to improve performance in a stateless application.
56-
If the `Authentication` contains a reference to an object in the cache (such as a `UserDetails` instance) and this has its credentials removed, then it will no longer be possible to authenticate against the cached value.
57-
You need to take this into account if you are using a cache.
58-
An obvious solution is to make a copy of the object first, either in the cache implementation or in the `AuthenticationProvider` which creates the returned `Authentication` object.
59-
Alternatively, you can disable the `eraseCredentialsAfterAuthentication` property on `ProviderManager`.
60-
See the Javadoc for more information.
61-
62-
636
[[core-services-dao-provider]]
64-
==== DaoAuthenticationProvider
7+
=== DaoAuthenticationProvider
658
The simplest `AuthenticationProvider` implemented by Spring Security is `DaoAuthenticationProvider`, which is also one of the earliest supported by the framework.
669
It leverages a `UserDetailsService` (as a DAO) in order to lookup the username, password and `GrantedAuthority` s.
6710
It authenticates the user simply by comparing the password submitted in a `UsernamePasswordAuthenticationToken` against the one loaded by the `UserDetailsService`.

docs/manual/src/docs/asciidoc/_includes/servlet/architecture/index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[[servlet-architecture]]
22
= Architecture and Implementation
3+
// FIXME: change to something like Servlet Security: The Big Picture
34
:figures: images/servlet/architecture
45

56
This section discusses Spring Security's high level architecture within Servlet based applications.

docs/manual/src/docs/asciidoc/_includes/servlet/architecture/technical-overview.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Remember the advantage that whatever your `UserDetailsService` returns can alway
4242
There is often some confusion about `UserDetailsService`.
4343
It is purely a DAO for user data and performs no other function other than to supply that data to other components within the framework.
4444
In particular, it __does not__ authenticate the user, which is done by the `AuthenticationManager`.
45-
In many cases it makes more sense to <<core-services-authentication-manager,implement `AuthenticationProvider`>> directly if you require a custom authentication process.
45+
In many cases it makes more sense to implement `AuthenticationProvider` directly if you require a custom authentication process.
4646
4747
====
4848

@@ -188,7 +188,7 @@ All you need to do is write a filter (or equivalent) that reads the third-party
188188
In this case you also need to think about things which are normally taken care of automatically by the built-in authentication infrastructure.
189189
For example, you might need to pre-emptively create an HTTP session to <<tech-intro-sec-context-persistence,cache the context between requests>>, before you write the response to the client footnote:[It isn't possible to create a session once the response has been committed.].
190190

191-
If you're wondering how the `AuthenticationManager` is implemented in a real world example, we'll look at that in the <<core-services-authentication-manager,core services chapter>>.
191+
If you're wondering how the `AuthenticationManager` is implemented in a real world example, we'll look at that in the <<servlet-authentication-authenticationmanager,`AuthenticationManager`>>.
192192

193193

194194
[[tech-intro-web-authentication]]

docs/manual/src/docs/asciidoc/_includes/servlet/authentication/architecture/authentication-manager.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
The `Authentication` that is returned is then set on the <<servlet-authentication-securitycontextholder>>.
66
If you are not integrating with <<servlet-filterchainproxy,Spring Security's ``Filters``s>>, you can set the `SecurityContextHolder` directly and are not required to use an `AuthenticationManager`.
77

8-
While the implementation of `AuthenticationManager` could be anything, Spring Security provides `ProviderManager` which allows users to provide multiple `AuthenticationProvider` implementations.
8+
While the implementation of `AuthenticationManager` could be anything, the most common implementation is <<servlet-authentication-providermanager,`ProviderManager`>>.
99
// FIXME: link to ProviderManager
1010
// FIXME: add configuration

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ include::abstract-authentication-processing-filter.adoc[leveloffset=+1]
1212

1313
include::authentication-manager.adoc[leveloffset=+1]
1414

15-
// authenticationmanager
16-
17-
// providermanager
15+
include::provider-manager.adoc[leveloffset=+1]
1816

1917
// authenticationprovider
2018

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[[servlet-authentication-providermanager]]
2+
= ProviderManager
3+
:figures: images/servlet/authentication/architecture
4+
5+
{security-api-url}org/springframework/security/authentication/ProviderManager.html[`ProviderManager`] is the most commonly used implementation of <<servlet-authentication-authenticationmanager,`AuthenticationManager`>>.
6+
`ProviderManager` delegates to a `List` of ``AuthenticationProvider``s.
7+
// FIXME: link to AuthenticationProvider
8+
Each `AuthenticationProvider` has an opportunity to indicate that authentication should be successful, fail, or indicate it cannot make a decision and allow a downstream `AuthenticationProvider` to decide.
9+
If none of the configured ``AuthenticationProvider``s can authenticate, then authentication will fail with a `ProviderNotFoundException` which is a special `AuthenticationException` that indicates the `ProviderManager` was not configured support the type of `Authentication` that was passed into it.
10+
11+
image::{figures}/providermanager.png[]
12+
13+
In practice each `AuthenticationProvider` knows how to perform a specific type of authentication.
14+
For example, one `AuthenticationProvider` might be able to validate a username/password, while another might be able to authenticate a SAML assertion.
15+
This allows each `AuthenticationProvider` to do a very specific type of authentication, while supporting multiple types of authentication and only exposing a single `AuthenticationManager` bean.
16+
17+
`ProviderManager` also allows configuring an optional parent `AuthenticationManager` which is consulted in the event that no `AuthenticationProvider` can perform authentication.
18+
The parent can be any type of `AuthenticationManager`, but it is often an instance of `ProviderManager`.
19+
20+
image::{figures}/providermanager-parent.png[]
21+
22+
In fact, multiple `ProviderManager` instances might share the same parent `AuthenticationManager`.
23+
This is somewhat common in scenarios where there are multiple <<servlet-securityfilterchain,`SecurityFilterChain`>> instances that have some authentication in common (the shared parent `AuthenticationManager`), but also different authentication mechanisms (the different `ProviderManager` instances).
24+
25+
image::{figures}/providermanagers-parent.png[]
26+
27+
[[servlet-authentication-providermanager-erasing-credentials]]
28+
By default `ProviderManager` will attempt to clear any sensitive credentials information from the `Authentication` object which is returned by a successful authentication request.
29+
This prevents information like passwords being retained longer than necessary in the `HttpSession`.
30+
31+
This may cause issues when you are using a cache of user objects, for example, to improve performance in a stateless application.
32+
If the `Authentication` contains a reference to an object in the cache (such as a `UserDetails` instance) and this has its credentials removed, then it will no longer be possible to authenticate against the cached value.
33+
You need to take this into account if you are using a cache.
34+
An obvious solution is to make a copy of the object first, either in the cache implementation or in the `AuthenticationProvider` which creates the returned `Authentication` object.
35+
Alternatively, you can disable the `eraseCredentialsAfterAuthentication` property on `ProviderManager`.
36+
See the {security-api-url}org/springframework/security/authentication/ProviderManager.html[Javadoc] for more information.

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@ This section discusses:
77
[[servlet-authentication-architecture]]
88
*Architecture Components*
99

10+
This section describes the main architectural components of Spring Security's used in Servlet authentication.
11+
If you need concrete flows that explain how these pieces fit together, look in specific sections.
12+
// FIXME: add for example see form login if you want to see more concrete flows.
13+
1014
* <<servlet-authentication-securitycontextholder>> - The `SecurityContextHolder` is where Spring Security stores the details of who is <<authentication,authenticated>>.
1115
* <<servlet-authentication-securitycontext>> - is obtained from the `SecurityContextHolder` and contains the `Authentication` of the currently authenticated user.
1216
* <<servlet-authentication-authentication>> - Can be the input to `AuthenticationManager` to provide the credentials a user has provided to authenticate or the current user from the `SecurityContext`.
1317
* <<servlet-authentication-granted-authority>> - An authority that is granted to the principal on the `Authentication` (i.e. roles, scopes, etc.)
1418
* <<servlet-authentication-authenticationentrypoint>> - used for requesting credentials from a client (i.e. redirecting to a log in page, sending a `WWW-Authenticate` response, etc.)
15-
* <<servlet-authentication-abstractprocessingfilter>> - a base `Filter` used for authentication
19+
* <<servlet-authentication-abstractprocessingfilter>> - a base `Filter` used for authentication.
20+
This also gives a good idea of the high level flow of authentication and how pieces work together.
1621
* <<servlet-authentication-authenticationmanager>> - the API that defines how Spring Security's Filters perform <<authentication,authentication>>.
22+
* <<servlet-authentication-providermanager>> - the most common implementation of `AuthenticationManager`.
1723

24+
[[servlet-authentication-mechanisms]]
1825
*Authentication Mechanisms*
1926

27+
// FIXME: brief description
28+
2029
* <<servlet-authentication-unpwd>> - how to authenticate with a username/password
2130
// FIXME: Add other mechanisms
2231

Loading

0 commit comments

Comments
 (0)