Skip to content

Commit 4da7235

Browse files
committed
Document OAuth2AuthorizationRequest customization improvements
Fixes gh-8071
1 parent ad9bb7f commit 4da7235

File tree

1 file changed

+20
-65
lines changed

1 file changed

+20
-65
lines changed

docs/manual/src/docs/asciidoc/_includes/servlet/oauth2/oauth2-client.adoc

+20-65
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ One of those extended parameters is the `prompt` parameter.
505505
[NOTE]
506506
OPTIONAL. Space delimited, case sensitive list of ASCII string values that specifies whether the Authorization Server prompts the End-User for reauthentication and consent. The defined values are: none, login, consent, select_account
507507

508-
The following example shows how to implement an `OAuth2AuthorizationRequestResolver` that customizes the Authorization Request for `oauth2Login()`, by including the request parameter `prompt=consent`.
508+
The following example shows how to configure the `DefaultOAuth2AuthorizationRequestResolver` with a `Consumer<OAuth2AuthorizationRequest.Builder>` that customizes the Authorization Request for `oauth2Login()`, by including the request parameter `prompt=consent`.
509509

510510
[source,java]
511511
----
@@ -524,72 +524,32 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
524524
.oauth2Login(oauth2 -> oauth2
525525
.authorizationEndpoint(authorization -> authorization
526526
.authorizationRequestResolver(
527-
new CustomAuthorizationRequestResolver(
528-
this.clientRegistrationRepository) <1>
527+
authorizationRequestResolver(this.clientRegistrationRepository)
529528
)
530529
)
531530
);
532531
}
533-
}
534-
535-
public class CustomAuthorizationRequestResolver implements OAuth2AuthorizationRequestResolver {
536-
private final OAuth2AuthorizationRequestResolver defaultAuthorizationRequestResolver;
537532
538-
public CustomAuthorizationRequestResolver(
533+
private OAuth2AuthorizationRequestResolver authorizationRequestResolver(
539534
ClientRegistrationRepository clientRegistrationRepository) {
540535
541-
this.defaultAuthorizationRequestResolver =
536+
DefaultOAuth2AuthorizationRequestResolver authorizationRequestResolver =
542537
new DefaultOAuth2AuthorizationRequestResolver(
543538
clientRegistrationRepository, "/oauth2/authorization");
544-
}
545-
546-
@Override
547-
public OAuth2AuthorizationRequest resolve(HttpServletRequest request) {
548-
OAuth2AuthorizationRequest authorizationRequest =
549-
this.defaultAuthorizationRequestResolver.resolve(request); <2>
550-
551-
return authorizationRequest != null ? <3>
552-
customAuthorizationRequest(authorizationRequest) :
553-
null;
554-
}
555-
556-
@Override
557-
public OAuth2AuthorizationRequest resolve(
558-
HttpServletRequest request, String clientRegistrationId) {
559-
560-
OAuth2AuthorizationRequest authorizationRequest =
561-
this.defaultAuthorizationRequestResolver.resolve(
562-
request, clientRegistrationId); <2>
539+
authorizationRequestResolver.setAuthorizationRequestCustomizer(
540+
authorizationRequestCustomizer());
563541
564-
return authorizationRequest != null ? <3>
565-
customAuthorizationRequest(authorizationRequest) :
566-
null;
542+
return authorizationRequestResolver;
567543
}
568544
569-
private OAuth2AuthorizationRequest customAuthorizationRequest(
570-
OAuth2AuthorizationRequest authorizationRequest) {
571-
572-
Map<String, Object> additionalParameters =
573-
new LinkedHashMap<>(authorizationRequest.getAdditionalParameters());
574-
additionalParameters.put("prompt", "consent"); <4>
575-
576-
return OAuth2AuthorizationRequest.from(authorizationRequest) <5>
577-
.additionalParameters(additionalParameters) <6>
578-
.build();
545+
private Consumer<OAuth2AuthorizationRequest.Builder> authorizationRequestCustomizer() {
546+
return customizer -> customizer
547+
.additionalParameters(params -> params.put("prompt", "consent"));
579548
}
580549
}
581550
----
582-
<1> Configure the custom `OAuth2AuthorizationRequestResolver`
583-
<2> Attempt to resolve the `OAuth2AuthorizationRequest` using the `DefaultOAuth2AuthorizationRequestResolver`
584-
<3> If an `OAuth2AuthorizationRequest` was resolved than return a customized version else return `null`
585-
<4> Add custom parameters to the existing `OAuth2AuthorizationRequest.additionalParameters`
586-
<5> Create a copy of the default `OAuth2AuthorizationRequest` which returns an `OAuth2AuthorizationRequest.Builder` for further modifications
587-
<6> Override the default `additionalParameters`
588-
589-
[TIP]
590-
`OAuth2AuthorizationRequest.Builder.build()` constructs the `OAuth2AuthorizationRequest.authorizationRequestUri`, which represents the complete Authorization Request URI including all query parameters using the `application/x-www-form-urlencoded` format.
591551

592-
For the simple use case, where the additional request parameter is always the same for a specific provider, it can be added directly in the `authorization-uri`.
552+
For the simple use case, where the additional request parameter is always the same for a specific provider, it may be added directly in the `authorization-uri` property.
593553

594554
For example, if the value for the request parameter `prompt` is always `consent` for the provider `okta`, than simply configure as follows:
595555

@@ -605,24 +565,19 @@ spring:
605565
----
606566

607567
The preceding example shows the common use case of adding a custom parameter on top of the standard parameters.
608-
Alternatively, if your requirements are more advanced, than you can take full control in building the Authorization Request URI by simply overriding the `OAuth2AuthorizationRequest.authorizationRequestUri` property.
568+
Alternatively, if your requirements are more advanced, you can take full control in building the Authorization Request URI by simply overriding the `OAuth2AuthorizationRequest.authorizationRequestUri` property.
609569

610-
The following example shows a variation of the `customAuthorizationRequest()` method from the preceding example, and instead overrides the `OAuth2AuthorizationRequest.authorizationRequestUri` property.
570+
[TIP]
571+
`OAuth2AuthorizationRequest.Builder.build()` constructs the `OAuth2AuthorizationRequest.authorizationRequestUri`, which represents the Authorization Request URI including all query parameters using the `application/x-www-form-urlencoded` format.
572+
573+
The following example shows a variation of `authorizationRequestCustomizer()` from the preceding example, and instead overrides the `OAuth2AuthorizationRequest.authorizationRequestUri` property.
611574

612575
[source,java]
613576
----
614-
private OAuth2AuthorizationRequest customAuthorizationRequest(
615-
OAuth2AuthorizationRequest authorizationRequest) {
616-
617-
String customAuthorizationRequestUri = UriComponentsBuilder
618-
.fromUriString(authorizationRequest.getAuthorizationRequestUri())
619-
.queryParam("prompt", "consent")
620-
.build(true)
621-
.toUriString();
622-
623-
return OAuth2AuthorizationRequest.from(authorizationRequest)
624-
.authorizationRequestUri(customAuthorizationRequestUri)
625-
.build();
577+
private Consumer<OAuth2AuthorizationRequest.Builder> authorizationRequestCustomizer() {
578+
return customizer -> customizer
579+
.authorizationRequestUri(uriBuilder -> uriBuilder
580+
.queryParam("prompt", "consent").build());
626581
}
627582
----
628583

0 commit comments

Comments
 (0)