Skip to content

Commit 5cc296d

Browse files
Update mfa.adoc
Signed-off-by: Tran Ngoc Nhan <[email protected]>
1 parent 20ae9dc commit 5cc296d

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

docs/modules/ROOT/pages/servlet/authentication/mfa.adoc

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ The `@EnableMultiFactorAuthentication` `authorities` property is just a shortcut
4444
When an `AuthorizationManagerFactory` Bean is available, it is used by Spring Security to create authorization rules, like `hasAnyRole(String)`, that are defined on the `AuthorizationManagerFactory` Bean interface.
4545
The implementation published by `@EnableMultiFactorAuthentication` will ensure that each authorization is combined with the requirement of having the specified factors.
4646

47-
The `AuthorizationManagerFactory` Bean below is what is published in the previously discussed xref:./mfa.adoc#emfa[`@EnableMultiFactorAuthentication` example].
47+
The `AuthorizationManagerFactory` Bean below is what is published in the previously discussed <<emfa, `@EnableMultiFactorAuthentication` example>>.
4848

4949
include-code::./UseAuthorizationManagerFactoryConfiguration[tag=authorizationManagerFactoryBean,indent=0]
5050

5151
[[selective-mfa]]
5252
== Selectively Requiring MFA
5353

54-
We have demonstrated how to configure an entire application to require MFA by using xref:./mfa.adoc#emfa[`@EnableMultiFactorAuthentication`]s `authorities` property.
54+
We have demonstrated how to configure an entire application to require MFA by using <<emfa, ``@EnableMultiFactorAuthentication``s>> `authorities` property.
5555
However, there are times that an application only wants parts of the application to require MFA.
5656
Consider the following requirements:
5757

@@ -61,7 +61,7 @@ Consider the following requirements:
6161

6262
In this case, some URLs require MFA while others do not.
6363
This means that the global approach that we saw before does not work.
64-
Fortunately, we can use what we learned in xref:./mfa.adoc#authorization-manager-factory[] to solve this in a concise manner.
64+
Fortunately, we can use what we learned in <<authorization-manager-factory>> to solve this in a concise manner.
6565

6666
Start by specifying `@EnableMultiFactorAuthentication` without any authorities.
6767
By doing so we enable MFA support, but no `AuthorizationManagerFactory` Bean is published.
@@ -118,10 +118,10 @@ To enable the MFA rules globally, we can publish an `AuthorizationManagerFactory
118118

119119
include-code::./AdminMfaAuthorizationManagerConfiguration[tag=authorizationManagerFactory,indent=0]
120120
<1> Inject the custom `AuthorizationManager` as the javadoc:org.springframework.security.authorization.DefaultAuthorizationManagerFactory#setAdditionalAuthorization(org.springframework.security.authorization.AuthorizationManager)[DefaultAuthorization.additionalAuthorization].
121-
This instructs `DefaultAuthorizationManagerFactory` that any authorization rule should apply our custom `AuthorizationManager` along with any authorization requirements defined by the application (e.g. `hasRole("ADMIN")).
121+
This instructs `DefaultAuthorizationManagerFactory` that any authorization rule should apply our custom `AuthorizationManager` along with any authorization requirements defined by the application (e.g. `hasRole("ADMIN")`).
122122
<2> Publish `DefaultAuthorizationManagerFactory` as a Bean, so it is used globally
123123

124-
This should feel very similar to our previous example in xref:./mfa.adoc#authorization-manager-factory[].
124+
This should feel very similar to our previous example in <<authorization-manager-factory>>.
125125
The difference is that in the previous example, the `AuthorizationManagerFactories` is setting `DefaultAuthorization.additionalAuthorization` with a built in `AuthorizationManager` that always requires the same authorities.
126126

127127
We can now define our authorization rules which are combined with `AdminMfaAuthorizationManager`.
@@ -138,10 +138,10 @@ If we preferred, we could change our logic to enable MFA based upon the roles ra
138138
[[raam-mfa]]
139139
== RequiredAuthoritiesAuthorizationManager
140140

141-
We've demonstrated how we can dynamically determine the authorities for a particular user in xref:./mfa.adoc#programmatic-mfa[] using a custom `AuthorizationManager`.
141+
We've demonstrated how we can dynamically determine the authorities for a particular user in <<programmatic-mfa>> using a custom `AuthorizationManager`.
142142
However, this is such a common scenario that Spring Security provides built in support using javadoc:org.springframework.security.authorization.RequiredAuthoritiesAuthorizationManager[] and javadoc:org.springframework.security.authorization.RequiredAuthoritiesRepository[].
143143

144-
Let's implement the same requirement that we did in xref:./mfa.adoc#programmatic-mfa[] using the built-in support.
144+
Let's implement the same requirement that we did in <<programmatic-mfa>> using the built-in support.
145145

146146
We start by creating the `RequiredAuthoritiesAuthorizationManager` Bean to use.
147147

@@ -153,10 +153,11 @@ Next we can define an `AuthorizationManagerFactory` that uses the `RequiredAutho
153153

154154
include-code::./RequiredAuthoritiesAuthorizationManagerConfiguration[tag=authorizationManagerFactory,indent=0]
155155
<1> Inject the `RequiredAuthoritiesAuthorizationManager` as the javadoc:org.springframework.security.authorization.DefaultAuthorizationManagerFactory#setAdditionalAuthorization(org.springframework.security.authorization.AuthorizationManager)[DefaultAuthorization.additionalAuthorization].
156-
This instructs `DefaultAuthorizationManagerFactory` that any authorization rule should apply `RequiredAuthoritiesAuthorizationManager` along with any authorization requirements defined by the application (e.g. `hasRole("ADMIN")).
156+
This instructs `DefaultAuthorizationManagerFactory` that any authorization rule should apply `RequiredAuthoritiesAuthorizationManager` along with any authorization requirements defined by the application (e.g. `hasRole("ADMIN")`).
157157
<2> Publish `DefaultAuthorizationManagerFactory` as a Bean, so it is used globally
158158

159159
We can now define our authorization rules which are combined with `RequiredAuthoritiesAuthorizationManager`.
160+
160161
include-code::./RequiredAuthoritiesAuthorizationManagerConfiguration[tag=httpSecurity,indent=0]
161162
<1> URLs that begin with `/admin/**` require `ROLE_ADMIN`.
162163
If the username is `admin`, then `FACTOR_OTT` and `FACTOR_PASSWORD` are also required.
@@ -167,7 +168,7 @@ Our example uses an in memory mapping of usernames to the additional required au
167168
For more dynamic use cases that can be determined by the username, a custom implementation of javadoc:org.springframework.security.authorization.RequiredAuthoritiesRepository[] can be created.
168169
Possible examples would be looking up if a user has enabled MFA in an explicit setting, determining if a user has registered a passkey, etc.
169170

170-
For cases that need to determine MFA based upon the `Authentication`, a custom `AuthorizationManger` can be used as demonstrated in xref:./mfa.adoc#programmatic-mfa[]
171+
For cases that need to determine MFA based upon the `Authentication`, a custom `AuthorizationManger` can be used as demonstrated in <<programmatic-mfa>>.
171172

172173

173174
[[hasallauthorities]]
@@ -196,7 +197,7 @@ Can you imagine what it would be like to declare hundreds of rules like this?
196197
What's more that it becomes difficult to express more complicated authorization rules.
197198
For example, how would you require two factors and either `ROLE_ADMIN` or `ROLE_USER`?
198199

199-
The answer to these questions, as we have already seen, is to use xref:./mfa.adoc#egmfa[]
200+
The answer to these questions, as we have already seen, is to use <<emfa>>
200201

201202
[[re-authentication]]
202203
== Re-authentication
@@ -211,7 +212,7 @@ By default, this application has two authentication mechanisms that it allows, m
211212
If there is a set of endpoints that require a specific factor, we can specify that in `authorizeHttpRequests` as follows:
212213

213214
include-code::./RequireOttConfiguration[tag=httpSecurity,indent=0]
214-
<1> - States that all `/profile/**` endpoints require one-time-token login to be authorized
215+
<1> States that all `/profile/**` endpoints require one-time-token login to be authorized
215216

216217
Given the above configuration, users can log in with any mechanism that you support.
217218
And, if they want to visit the profile page, then Spring Security will redirect them to the One-Time-Token Login page to obtain it.

docs/src/test/java/org/springframework/security/docs/servlet/authentication/programmaticmfa/AdminMfaAuthorizationManagerConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
3434
// @formatter:off
3535
http
3636
.authorizeHttpRequests((authorize) -> authorize
37+
// <1>
3738
.requestMatchers("/admin/**").hasRole("ADMIN")
39+
// <2>
3840
.anyRequest().authenticated()
3941
)
4042
.formLogin(Customizer.withDefaults())

docs/src/test/java/org/springframework/security/docs/servlet/authentication/raammfa/RequiredAuthoritiesAuthorizationManagerConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
2828
// @formatter:off
2929
http
3030
.authorizeHttpRequests((authorize) -> authorize
31-
.requestMatchers("/admin/**").hasRole("ADMIN")
32-
.anyRequest().authenticated()
31+
.requestMatchers("/admin/**").hasRole("ADMIN") // <1>
32+
.anyRequest().authenticated() // <2>
3333
)
3434
.formLogin(Customizer.withDefaults())
3535
.oneTimeTokenLogin(Customizer.withDefaults());

docs/src/test/kotlin/org/springframework/security/kt/docs/servlet/authentication/raammfa/RequiredAuthoritiesAuthorizationManagerConfiguration.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ internal class RequiredAuthoritiesAuthorizationManagerConfiguration {
2727
// @formatter:off
2828
http {
2929
authorizeHttpRequests {
30-
authorize("/admin/**", hasRole("ADMIN"))
31-
authorize(anyRequest, authenticated)
30+
authorize("/admin/**", hasRole("ADMIN")) // <1>
31+
authorize(anyRequest, authenticated) // <2>
3232
}
3333
formLogin { }
3434
oneTimeTokenLogin { }

0 commit comments

Comments
 (0)