Skip to content

Commit c1ae997

Browse files
committed
Update ref doc for OAuth2AuthorizedClientManager
Issue gh-7403
1 parent ff54eb8 commit c1ae997

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

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

+48
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,54 @@ public OAuth2AuthorizedClientManager authorizedClientManager(
285285
Spring Boot 2.x auto-configuration registers an `OAuth2AuthorizedClientManager` `@Bean` in the `ApplicationContext`.
286286
However, the application may choose to override and register a custom `OAuth2AuthorizedClientManager` `@Bean`.
287287

288+
The `DefaultOAuth2AuthorizedClientManager` is also associated with a `contextAttributesMapper` of type `Function<OAuth2AuthorizeRequest, Map<String, Object>>`, which is responsible for mapping attribute(s) from the `OAuth2AuthorizeRequest` to a `Map` of attributes to be associated to the `OAuth2AuthorizationContext`.
289+
This can be useful when you need to supply an `OAuth2AuthorizedClientProvider` with required (supported) attribute(s), eg. the `PasswordOAuth2AuthorizedClientProvider` requires the resource owner's `username` and `password` to be available in `OAuth2AuthorizationContext.getAttributes()`.
290+
291+
The following code shows an example of the `contextAttributesMapper`:
292+
293+
[source,java]
294+
----
295+
@Bean
296+
public OAuth2AuthorizedClientManager authorizedClientManager(
297+
ClientRegistrationRepository clientRegistrationRepository,
298+
OAuth2AuthorizedClientRepository authorizedClientRepository) {
299+
300+
OAuth2AuthorizedClientProvider authorizedClientProvider =
301+
OAuth2AuthorizedClientProviderBuilder.builder()
302+
.password()
303+
.refreshToken()
304+
.build();
305+
306+
DefaultOAuth2AuthorizedClientManager authorizedClientManager =
307+
new DefaultOAuth2AuthorizedClientManager(
308+
clientRegistrationRepository, authorizedClientRepository);
309+
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
310+
311+
// Assuming the `username` and `password` are supplied as `HttpServletRequest` parameters,
312+
// map the `HttpServletRequest` parameters to `OAuth2AuthorizationContext.getAttributes()`
313+
authorizedClientManager.setContextAttributesMapper(contextAttributesMapper());
314+
315+
return authorizedClientManager;
316+
}
317+
318+
private Function<OAuth2AuthorizeRequest, Map<String, Object>> contextAttributesMapper() {
319+
return authorizeRequest -> {
320+
Map<String, Object> contextAttributes = Collections.emptyMap();
321+
HttpServletRequest servletRequest = authorizeRequest.getAttribute(HttpServletRequest.class.getName());
322+
String username = servletRequest.getParameter(OAuth2ParameterNames.USERNAME);
323+
String password = servletRequest.getParameter(OAuth2ParameterNames.PASSWORD);
324+
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
325+
contextAttributes = new HashMap<>();
326+
327+
// `PasswordOAuth2AuthorizedClientProvider` requires both attributes
328+
contextAttributes.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username);
329+
contextAttributes.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password);
330+
}
331+
return contextAttributes;
332+
};
333+
}
334+
----
335+
288336

289337
[[oauth2Client-auth-grant-support]]
290338
=== Authorization Grant Support

0 commit comments

Comments
 (0)