Skip to content

Commit 1c257af

Browse files
committed
Update ref doc for oauth2-client
1 parent b55b291 commit 1c257af

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

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

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ public class OAuth2ClientController {
179179
180180
@RequestMapping("/")
181181
public String index() {
182-
ClientRegistration googleRegistration =
183-
this.clientRegistrationRepository.findByRegistrationId("google");
182+
ClientRegistration oktaRegistration =
183+
this.clientRegistrationRepository.findByRegistrationId("okta");
184184
185185
...
186186
@@ -207,38 +207,34 @@ Whereas, the primary role of `OAuth2AuthorizedClientService` is to manage `OAuth
207207

208208
From a developer perspective, the `OAuth2AuthorizedClientRepository` or `OAuth2AuthorizedClientService` provides the capability to lookup an `OAuth2AccessToken` associated with a client so that it may be used to initiate a protected resource request.
209209

210-
[NOTE]
211-
Spring Boot 2.x auto-configuration registers an `OAuth2AuthorizedClientRepository` and/or `OAuth2AuthorizedClientService` `@Bean` in the `ApplicationContext`.
212-
However, the application may choose to override and register a custom `OAuth2AuthorizedClientRepository` or `OAuth2AuthorizedClientService` `@Bean`.
213-
214210
The following listing shows an example:
215211

216212
[source,java]
217213
----
218214
@Controller
219-
public class OAuth2LoginController {
215+
public class OAuth2ClientController {
220216
221-
@Autowired
222-
private OAuth2AuthorizedClientService authorizedClientService;
217+
@Autowired
218+
private OAuth2AuthorizedClientService authorizedClientService;
223219
224-
@RequestMapping("/userinfo")
225-
public String userinfo(OAuth2AuthenticationToken authentication) {
226-
// authentication.getAuthorizedClientRegistrationId() returns the
227-
// registrationId of the Client that was authorized during the oauth2Login() flow
228-
OAuth2AuthorizedClient authorizedClient =
229-
this.authorizedClientService.loadAuthorizedClient(
230-
authentication.getAuthorizedClientRegistrationId(),
231-
authentication.getName());
220+
@RequestMapping("/")
221+
public String index(Authentication authentication) {
222+
OAuth2AuthorizedClient authorizedClient =
223+
this.authorizedClientService.loadAuthorizedClient("okta", authentication.getName());
232224
233-
OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
225+
OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
234226
235-
...
227+
...
236228
237-
return "userinfo";
238-
}
229+
return "index";
230+
}
239231
}
240232
----
241233

234+
[NOTE]
235+
Spring Boot 2.x auto-configuration registers an `OAuth2AuthorizedClientRepository` and/or `OAuth2AuthorizedClientService` `@Bean` in the `ApplicationContext`.
236+
However, the application may choose to override and register a custom `OAuth2AuthorizedClientRepository` or `OAuth2AuthorizedClientService` `@Bean`.
237+
242238

243239
[[oauth2Client-authorized-manager-provider]]
244240
==== OAuth2AuthorizedClientManager / OAuth2AuthorizedClientProvider
@@ -311,6 +307,29 @@ The `OAuth2AuthorizationRequestRedirectFilter` uses an `OAuth2AuthorizationReque
311307
The primary role of the `OAuth2AuthorizationRequestResolver` is to resolve an `OAuth2AuthorizationRequest` from the provided web request.
312308
The default implementation `DefaultOAuth2AuthorizationRequestResolver` matches on the (default) path `/oauth2/authorization/{registrationId}` extracting the `registrationId` and using it to build the `OAuth2AuthorizationRequest` for the associated `ClientRegistration`.
313309

310+
Given the following Spring Boot 2.x properties for an OAuth 2.0 Client registration:
311+
312+
[source,yaml]
313+
----
314+
spring:
315+
security:
316+
oauth2:
317+
client:
318+
registration:
319+
okta:
320+
client-id: okta-client-id
321+
client-secret: okta-client-secret
322+
authorization-grant-type: authorization_code
323+
redirect-uri: "{baseUrl}/authorized/okta"
324+
scope: read, write
325+
----
326+
327+
A request with the base path `/oauth2/authorization/okta` will initiate the Authorization Request redirect by the `OAuth2AuthorizationRequestRedirectFilter` and ultimately start the Authorization Code grant flow.
328+
329+
[NOTE]
330+
The `AuthorizationCodeOAuth2AuthorizedClientProvider` is an implementation of `OAuth2AuthorizedClientProvider` for the Authorization Code grant,
331+
which also initiates the Authorization Request redirect by the `OAuth2AuthorizationRequestRedirectFilter`.
332+
314333

315334
===== Customizing the Authorization Request
316335

@@ -471,7 +490,7 @@ Please refer to the https://tools.ietf.org/html/rfc6749#section-4.1.3[Access Tok
471490

472491
The primary role of the `OAuth2AccessTokenResponseClient` is to exchange an authorization grant credential for an access token credential at the Authorization Server's Token Endpoint.
473492

474-
The default implementation of `OAuth2AccessTokenResponseClient` for the `authorization_code` grant is `DefaultAuthorizationCodeTokenResponseClient`, which uses a `RestOperations` for exchanging an authorization code for an access token at the Token Endpoint.
493+
The default implementation of `OAuth2AccessTokenResponseClient` for the Authorization Code grant is `DefaultAuthorizationCodeTokenResponseClient`, which uses a `RestOperations` for exchanging an authorization code for an access token at the Token Endpoint.
475494

476495
The `DefaultAuthorizationCodeTokenResponseClient` is quite flexible as it allows you to customize the pre-processing of the Token Request and/or post-handling of the Token Response.
477496

@@ -543,15 +562,15 @@ This is a convenient alternative compared to looking up the `OAuth2AuthorizedCli
543562
[source,java]
544563
----
545564
@Controller
546-
public class OAuth2LoginController {
565+
public class OAuth2ClientController {
547566
548-
@RequestMapping("/userinfo")
549-
public String userinfo(@RegisteredOAuth2AuthorizedClient("google") OAuth2AuthorizedClient authorizedClient) {
567+
@RequestMapping("/")
568+
public String index(@RegisteredOAuth2AuthorizedClient("okta") OAuth2AuthorizedClient authorizedClient) {
550569
OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
551570
552571
...
553572
554-
return "userinfo";
573+
return "index";
555574
}
556575
}
557576
----

0 commit comments

Comments
 (0)