123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167 |
- [[oauth2-client-authorization-grants]]
- = [[oauth2Client-auth-grant-support]]Authorization Grant Support
- :spring-security-reference-base-url: https://docs.spring.io/spring-security/reference
- This section describes Spring Security's support for authorization grants.
- [[oauth2-client-authorization-code]]
- == [[oauth2Client-auth-code-grant]]Authorization Code
- [NOTE]
- ====
- See the OAuth 2.0 Authorization Framework for further details on the https://tools.ietf.org/html/rfc6749#section-1.3.1[Authorization Code] grant.
- ====
- [[oauth2-client-authorization-code-authorization]]
- === Obtaining Authorization
- [NOTE]
- ====
- See the https://tools.ietf.org/html/rfc6749#section-4.1.1[Authorization Request/Response] protocol flow for the Authorization Code grant.
- ====
- [[oauth2-client-authorization-code-authorization-request]]
- === Initiating the Authorization Request
- The `OAuth2AuthorizationRequestRedirectFilter` uses an `OAuth2AuthorizationRequestResolver` to resolve an `OAuth2AuthorizationRequest` and initiate the Authorization Code grant flow by redirecting the end-user's user-agent to the Authorization Server's Authorization Endpoint.
- The primary role of the `OAuth2AuthorizationRequestResolver` is to resolve an `OAuth2AuthorizationRequest` from the provided web request.
- 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`.
- Consider the following Spring Boot properties for an OAuth 2.0 Client registration:
- [source,yaml,attrs="-attributes"]
- ----
- spring:
- security:
- oauth2:
- client:
- registration:
- okta:
- client-id: okta-client-id
- client-secret: okta-client-secret
- authorization-grant-type: authorization_code
- redirect-uri: "{baseUrl}/authorized/okta"
- scope: read, write
- provider:
- okta:
- authorization-uri: https://dev-1234.oktapreview.com/oauth2/v1/authorize
- token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
- ----
- Given the preceding properties, a request with the base path `/oauth2/authorization/okta` initiates the Authorization Request redirect by the `OAuth2AuthorizationRequestRedirectFilter` and ultimately starts the Authorization Code grant flow.
- [NOTE]
- ====
- The `AuthorizationCodeOAuth2AuthorizedClientProvider` is an implementation of `OAuth2AuthorizedClientProvider` for the Authorization Code grant,
- which also initiates the Authorization Request redirect by the `OAuth2AuthorizationRequestRedirectFilter`.
- ====
- If the OAuth 2.0 Client is a https://tools.ietf.org/html/rfc6749#section-2.1[Public Client], configure the OAuth 2.0 Client registration as follows:
- [source,yaml,attrs="-attributes"]
- ----
- spring:
- security:
- oauth2:
- client:
- registration:
- okta:
- client-id: okta-client-id
- client-authentication-method: none
- authorization-grant-type: authorization_code
- redirect-uri: "{baseUrl}/authorized/okta"
- # ...
- ----
- Public Clients are supported by using https://tools.ietf.org/html/rfc7636[Proof Key for Code Exchange] (PKCE).
- If the client is running in an untrusted environment (such as a native application or web browser-based application) and is therefore incapable of maintaining the confidentiality of its credentials, PKCE is automatically used when the following conditions are true:
- . `client-secret` is omitted (or empty) and
- . `client-authentication-method` is set to `none` (`ClientAuthenticationMethod.NONE`)
- or
- . When `ClientRegistration.clientSettings.requireProofKey` is `true` (in this case `ClientRegistration.authorizationGrantType` must be `authorization_code`)
- [TIP]
- ====
- If the OAuth 2.0 Provider supports PKCE for https://tools.ietf.org/html/rfc6749#section-2.1[Confidential Clients], you may (optionally) configure it using `DefaultOAuth2AuthorizationRequestResolver.setAuthorizationRequestCustomizer(OAuth2AuthorizationRequestCustomizers.withPkce())`.
- ====
- [[oauth2-client-authorization-code-redirect-uri]]
- [[oauth2Client-auth-code-redirect-uri]]The `DefaultOAuth2AuthorizationRequestResolver` also supports `URI` template variables for the `redirect-uri` by using `UriComponentsBuilder`.
- The following configuration uses all the supported `URI` template variables:
- [source,yaml,attrs="-attributes"]
- ----
- spring:
- security:
- oauth2:
- client:
- registration:
- okta:
- # ...
- redirect-uri: "{baseScheme}://{baseHost}{basePort}{basePath}/authorized/{registrationId}"
- # ...
- ----
- [NOTE]
- ====
- `+{baseUrl}+` resolves to `+{baseScheme}://{baseHost}{basePort}{basePath}+`
- ====
- Configuring the `redirect-uri` with `URI` template variables is especially useful when the OAuth 2.0 Client is running behind a xref:features/exploits/http.adoc#http-proxy-server[Proxy Server].
- Doing so ensures that the `X-Forwarded-*` headers are used when expanding the `redirect-uri`.
- [[oauth2-client-authorization-code-authorization-request-resolver]]
- === Customizing the Authorization Request
- One of the primary use cases an `OAuth2AuthorizationRequestResolver` can realize is the ability to customize the Authorization Request with additional parameters above the standard parameters defined in the OAuth 2.0 Authorization Framework.
- For example, OpenID Connect defines additional OAuth 2.0 request parameters for the https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest[Authorization Code Flow] extending from the standard parameters defined in the https://tools.ietf.org/html/rfc6749#section-4.1.1[OAuth 2.0 Authorization Framework].
- One of those extended parameters is the `prompt` parameter.
- [NOTE]
- ====
- The `prompt` parameter is optional. Space delimited, case sensitive list of ASCII string values that specifies whether the Authorization Server prompts the End-User for re-authentication and consent. The defined values are: `none`, `login`, `consent`, and `select_account`.
- ====
- 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`.
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- @Configuration
- @EnableWebSecurity
- public class OAuth2LoginSecurityConfig {
- @Autowired
- private ClientRegistrationRepository clientRegistrationRepository;
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- .authorizeHttpRequests((authorize) -> authorize
- .anyRequest().authenticated()
- )
- .oauth2Login((oauth2) -> oauth2
- .authorizationEndpoint((authorization) -> authorization
- .authorizationRequestResolver(
- authorizationRequestResolver(this.clientRegistrationRepository)
- )
- )
- );
- return http.build();
- }
- private OAuth2AuthorizationRequestResolver authorizationRequestResolver(
- ClientRegistrationRepository clientRegistrationRepository) {
- DefaultOAuth2AuthorizationRequestResolver authorizationRequestResolver =
- new DefaultOAuth2AuthorizationRequestResolver(
- clientRegistrationRepository, "/oauth2/authorization");
- authorizationRequestResolver.setAuthorizationRequestCustomizer(
- authorizationRequestCustomizer());
- return authorizationRequestResolver;
- }
- private Consumer<OAuth2AuthorizationRequest.Builder> authorizationRequestCustomizer() {
- return customizer -> customizer
- .additionalParameters((params) -> params.put("prompt", "consent"));
- }
- }
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- @Configuration
- @EnableWebSecurity
- class SecurityConfig {
- @Autowired
- private lateinit var customClientRegistrationRepository: ClientRegistrationRepository
- @Bean
- open fun filterChain(http: HttpSecurity): SecurityFilterChain {
- http {
- authorizeHttpRequests {
- authorize(anyRequest, authenticated)
- }
- oauth2Login {
- authorizationEndpoint {
- authorizationRequestResolver = authorizationRequestResolver(customClientRegistrationRepository)
- }
- }
- }
- return http.build()
- }
- private fun authorizationRequestResolver(
- clientRegistrationRepository: ClientRegistrationRepository?): OAuth2AuthorizationRequestResolver {
- val authorizationRequestResolver = DefaultOAuth2AuthorizationRequestResolver(
- clientRegistrationRepository, "/oauth2/authorization")
- authorizationRequestResolver.setAuthorizationRequestCustomizer(
- authorizationRequestCustomizer())
- return authorizationRequestResolver
- }
- private fun authorizationRequestCustomizer(): Consumer<OAuth2AuthorizationRequest.Builder> {
- return Consumer { customizer ->
- customizer
- .additionalParameters { params -> params["prompt"] = "consent" }
- }
- }
- }
- ----
- ======
- For the simple use case where the additional request parameter is always the same for a specific provider, you can add it directly in the `authorization-uri` property.
- For example, if the value for the request parameter `prompt` is always `consent` for the provider `okta`, you can configure it as follows:
- [source,yaml]
- ----
- spring:
- security:
- oauth2:
- client:
- provider:
- okta:
- authorization-uri: https://dev-1234.oktapreview.com/oauth2/v1/authorize?prompt=consent
- ----
- The preceding example shows the common use case of adding a custom parameter on top of the standard parameters.
- Alternatively, if your requirements are more advanced, you can take full control in building the Authorization Request URI by overriding the `OAuth2AuthorizationRequest.authorizationRequestUri` property.
- [TIP]
- ====
- `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.
- ====
- The following example shows a variation of `authorizationRequestCustomizer()` from the preceding example and instead overrides the `OAuth2AuthorizationRequest.authorizationRequestUri` property:
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- private Consumer<OAuth2AuthorizationRequest.Builder> authorizationRequestCustomizer() {
- return customizer -> customizer
- .authorizationRequestUri((uriBuilder) -> uriBuilder
- .queryParam("prompt", "consent").build());
- }
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- private fun authorizationRequestCustomizer(): Consumer<OAuth2AuthorizationRequest.Builder> {
- return Consumer { customizer: OAuth2AuthorizationRequest.Builder ->
- customizer
- .authorizationRequestUri { uriBuilder: UriBuilder ->
- uriBuilder
- .queryParam("prompt", "consent").build()
- }
- }
- }
- ----
- ======
- [[oauth2-client-authorization-code-authorization-request-repository]]
- === Storing the Authorization Request
- The `AuthorizationRequestRepository` is responsible for the persistence of the `OAuth2AuthorizationRequest` from the time the Authorization Request is initiated to the time the Authorization Response is received (the callback).
- [TIP]
- ====
- The `OAuth2AuthorizationRequest` is used to correlate and validate the Authorization Response.
- ====
- The default implementation of `AuthorizationRequestRepository` is `HttpSessionOAuth2AuthorizationRequestRepository`, which stores the `OAuth2AuthorizationRequest` in the `HttpSession`.
- If you have a custom implementation of `AuthorizationRequestRepository`, you can configure it as follows:
- .AuthorizationRequestRepository Configuration
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- @Configuration
- @EnableWebSecurity
- public class OAuth2ClientSecurityConfig {
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- .oauth2Client((oauth2) -> oauth2
- .authorizationCodeGrant((codeGrant) -> codeGrant
- .authorizationRequestRepository(this.authorizationRequestRepository())
- // ...
- )
- )
- .oauth2Login((oauth2) -> oauth2
- .authorizationEndpoint((endpoint) -> endpoint
- .authorizationRequestRepository(this.authorizationRequestRepository())
- // ...
- )
- );
- return http.build();
- }
- @Bean
- public AuthorizationRequestRepository<OAuth2AuthorizationRequest> authorizationRequestRepository() {
- return new CustomOAuth2AuthorizationRequestRepository();
- }
- }
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- @Configuration
- @EnableWebSecurity
- class OAuth2ClientSecurityConfig {
- @Bean
- open fun filterChain(http: HttpSecurity): SecurityFilterChain {
- http {
- oauth2Client {
- authorizationCodeGrant {
- authorizationRequestRepository = authorizationRequestRepository()
- }
- }
- }
- return http.build()
- }
- }
- ----
- Xml::
- +
- [source,xml,role="secondary"]
- ----
- <http>
- <oauth2-client>
- <authorization-code-grant authorization-request-repository-ref="authorizationRequestRepository"/>
- </oauth2-client>
- </http>
- ----
- ======
- [[oauth2-client-authorization-code-access-token]]
- === Requesting an Access Token
- [NOTE]
- ====
- See the https://tools.ietf.org/html/rfc6749#section-4.1.3[Access Token Request/Response] protocol flow for the Authorization Code grant.
- ====
- The default implementation of `OAuth2AccessTokenResponseClient` for the Authorization Code grant is `RestClientAuthorizationCodeTokenResponseClient`, which uses a `RestClient` instance to exchange an authorization code for an access token at the Authorization Server’s Token Endpoint.
- :section-id: authorization-code
- :grant-type: Authorization Code
- :class-name: RestClientAuthorizationCodeTokenResponseClient
- :grant-request: OAuth2AuthorizationCodeGrantRequest
- :leveloffset: +1
- include::partial$servlet/oauth2/client/rest-client-access-token-response-client.adoc[]
- :leveloffset: -1
- [[oauth2-client-authorization-code-access-token-response-client-dsl]]
- === Customize using the DSL
- Whether you customize `{class-name}` or provide your own implementation of `OAuth2AccessTokenResponseClient`, you can configure it using the DSL (as an alternative to <<oauth2-client-authorization-code-access-token-response-client-bean,publishing a bean>>) as follows:
- .Access Token Response Configuration via DSL
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- @Configuration
- @EnableWebSecurity
- public class OAuth2ClientSecurityConfig {
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- .oauth2Client((oauth2) -> oauth2
- .authorizationCodeGrant((codeGrant) -> codeGrant
- .accessTokenResponseClient(this.accessTokenResponseClient())
- // ...
- )
- );
- return http.build();
- }
- }
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- @Configuration
- @EnableWebSecurity
- class OAuth2ClientSecurityConfig {
- @Bean
- open fun filterChain(http: HttpSecurity): SecurityFilterChain {
- http {
- oauth2Client {
- authorizationCodeGrant {
- accessTokenResponseClient = accessTokenResponseClient()
- }
- }
- }
- return http.build()
- }
- }
- ----
- Xml::
- +
- [source,xml,role="secondary"]
- ----
- <http>
- <oauth2-client>
- <authorization-code-grant access-token-response-client-ref="accessTokenResponseClient"/>
- </oauth2-client>
- </http>
- ----
- ======
- [[oauth2-client-refresh-token]]
- == [[oauth2Client-refresh-token-grant]]Refresh Token
- [NOTE]
- ====
- See the OAuth 2.0 Authorization Framework for further details on the https://tools.ietf.org/html/rfc6749#section-1.5[Refresh Token].
- ====
- [[oauth2-client-refresh-token-access-token]]
- === Refreshing an Access Token
- [NOTE]
- ====
- See the https://tools.ietf.org/html/rfc6749#section-6[Access Token Request/Response] protocol flow for the Refresh Token grant.
- ====
- The default implementation of `OAuth2AccessTokenResponseClient` for the Refresh Token grant is `RestClientRefreshTokenTokenResponseClient`, which uses a `RestClient` instance to obtain an access token at the Authorization Server’s Token Endpoint.
- :section-id: refresh-token
- :grant-type: Refresh Token
- :class-name: RestClientRefreshTokenTokenResponseClient
- :grant-request: OAuth2RefreshTokenGrantRequest
- :leveloffset: +1
- include::partial$servlet/oauth2/client/rest-client-access-token-response-client.adoc[]
- :leveloffset: -1
- [[oauth2-client-refresh-token-authorized-client-provider-builder]]
- === Customize using the Builder
- Whether you customize `RestClientRefreshTokenTokenResponseClient` or provide your own implementation of `OAuth2AccessTokenResponseClient`, you can configure it using the `OAuth2AuthorizedClientProviderBuilder` (as an alternative to <<oauth2-client-refresh-token-access-token-response-client-bean,publishing a bean>>) as follows:
- .Access Token Response Configuration via Builder
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- // Customize
- OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> refreshTokenTokenResponseClient = ...
- OAuth2AuthorizedClientProvider authorizedClientProvider =
- OAuth2AuthorizedClientProviderBuilder.builder()
- .authorizationCode()
- .refreshToken((configurer) -> configurer.accessTokenResponseClient(refreshTokenTokenResponseClient))
- .build();
- // ...
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- // Customize
- val refreshTokenTokenResponseClient: OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> = ...
- val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
- .authorizationCode()
- .refreshToken { it.accessTokenResponseClient(refreshTokenTokenResponseClient) }
- .build()
- // ...
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
- ----
- ======
- [NOTE]
- ====
- `OAuth2AuthorizedClientProviderBuilder.builder().refreshToken()` configures a `RefreshTokenOAuth2AuthorizedClientProvider`,
- which is an implementation of an `OAuth2AuthorizedClientProvider` for the Refresh Token grant.
- ====
- The `OAuth2RefreshToken` can optionally be returned in the Access Token Response for the `authorization_code` grant type.
- If the `OAuth2AuthorizedClient.getRefreshToken()` is available and the `OAuth2AuthorizedClient.getAccessToken()` is expired, it is automatically refreshed by the `RefreshTokenOAuth2AuthorizedClientProvider`.
- [[oauth2-client-client-credentials]]
- == [[oauth2Client-client-creds-grant]]Client Credentials
- [NOTE]
- ====
- Please refer to the OAuth 2.0 Authorization Framework for further details on the https://tools.ietf.org/html/rfc6749#section-1.3.4[Client Credentials] grant.
- ====
- [[oauth2-client-client-credentials-access-token]]
- === Requesting an Access Token
- [NOTE]
- ====
- See the https://tools.ietf.org/html/rfc6749#section-4.4.2[Access Token Request/Response] protocol flow for the Client Credentials grant.
- ====
- The default implementation of `OAuth2AccessTokenResponseClient` for the Client Credentials grant is `RestClientClientCredentialsTokenResponseClient`, which uses a `RestClient` instance to obtain an access token at the Authorization Server’s Token Endpoint.
- :section-id: client-credentials
- :grant-type: Client Credentials
- :class-name: RestClientClientCredentialsTokenResponseClient
- :grant-request: OAuth2ClientCredentialsGrantRequest
- :leveloffset: +1
- include::partial$servlet/oauth2/client/rest-client-access-token-response-client.adoc[]
- :leveloffset: -1
- [[oauth2-client-client-credentials-authorized-client-provider-builder]]
- === Customize using the Builder
- Whether you customize `RestClientClientCredentialsTokenResponseClient` or provide your own implementation of `OAuth2AccessTokenResponseClient`, you can configure it using the `OAuth2AuthorizedClientProviderBuilder` (as an alternative to <<oauth2-client-client-credentials-access-token-response-client-bean,publishing a bean>>) as follows:
- .Access Token Response Configuration via Builder
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- // Customize
- OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsTokenResponseClient = ...
- OAuth2AuthorizedClientProvider authorizedClientProvider =
- OAuth2AuthorizedClientProviderBuilder.builder()
- .clientCredentials((configurer) -> configurer.accessTokenResponseClient(clientCredentialsTokenResponseClient))
- .build();
- // ...
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- // Customize
- val clientCredentialsTokenResponseClient: OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> = ...
- val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
- .clientCredentials { it.accessTokenResponseClient(clientCredentialsTokenResponseClient) }
- .build()
- // ...
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
- ----
- ======
- [NOTE]
- ====
- `OAuth2AuthorizedClientProviderBuilder.builder().clientCredentials()` configures a `ClientCredentialsOAuth2AuthorizedClientProvider`,
- which is an implementation of an `OAuth2AuthorizedClientProvider` for the Client Credentials grant.
- ====
- [[oauth2-client-client-credentials-authorized-client-manager]]
- === Using the Access Token
- Consider the following Spring Boot properties for an OAuth 2.0 Client registration:
- [source,yaml]
- ----
- spring:
- security:
- oauth2:
- client:
- registration:
- okta:
- client-id: okta-client-id
- client-secret: okta-client-secret
- authorization-grant-type: client_credentials
- scope: read, write
- provider:
- okta:
- token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
- ----
- Further consider the following `OAuth2AuthorizedClientManager` `@Bean`:
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- @Bean
- public OAuth2AuthorizedClientManager authorizedClientManager(
- ClientRegistrationRepository clientRegistrationRepository,
- OAuth2AuthorizedClientRepository authorizedClientRepository) {
- OAuth2AuthorizedClientProvider authorizedClientProvider =
- OAuth2AuthorizedClientProviderBuilder.builder()
- .clientCredentials()
- .build();
- DefaultOAuth2AuthorizedClientManager authorizedClientManager =
- new DefaultOAuth2AuthorizedClientManager(
- clientRegistrationRepository, authorizedClientRepository);
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
- return authorizedClientManager;
- }
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- @Bean
- fun authorizedClientManager(
- clientRegistrationRepository: ClientRegistrationRepository,
- authorizedClientRepository: OAuth2AuthorizedClientRepository): OAuth2AuthorizedClientManager {
- val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
- .clientCredentials()
- .build()
- val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
- clientRegistrationRepository, authorizedClientRepository)
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
- return authorizedClientManager
- }
- ----
- ======
- Given the preceding properties and bean, you can obtain the `OAuth2AccessToken` as follows:
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- @Controller
- public class OAuth2ClientController {
- @Autowired
- private OAuth2AuthorizedClientManager authorizedClientManager;
- @GetMapping("/")
- public String index(Authentication authentication,
- HttpServletRequest servletRequest,
- HttpServletResponse servletResponse) {
- OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
- .principal(authentication)
- .attributes(attrs -> {
- attrs.put(HttpServletRequest.class.getName(), servletRequest);
- attrs.put(HttpServletResponse.class.getName(), servletResponse);
- })
- .build();
- OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest);
- OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
- // ...
- return "index";
- }
- }
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- class OAuth2ClientController {
- @Autowired
- private lateinit var authorizedClientManager: OAuth2AuthorizedClientManager
- @GetMapping("/")
- fun index(authentication: Authentication?,
- servletRequest: HttpServletRequest,
- servletResponse: HttpServletResponse): String {
- val authorizeRequest: OAuth2AuthorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
- .principal(authentication)
- .attributes(Consumer { attrs: MutableMap<String, Any> ->
- attrs[HttpServletRequest::class.java.name] = servletRequest
- attrs[HttpServletResponse::class.java.name] = servletResponse
- })
- .build()
- val authorizedClient = authorizedClientManager.authorize(authorizeRequest)
- val accessToken: OAuth2AccessToken = authorizedClient.accessToken
- // ...
- return "index"
- }
- }
- ----
- ======
- [NOTE]
- ====
- `HttpServletRequest` and `HttpServletResponse` are both OPTIONAL attributes.
- If not provided, they default to `ServletRequestAttributes` by using `RequestContextHolder.getRequestAttributes()`.
- ====
- [[oauth2-client-jwt-bearer]]
- == JWT Bearer
- [NOTE]
- ====
- Please refer to JSON Web Token (JWT) Profile for OAuth 2.0 Client Authentication and Authorization Grants for further details on the https://datatracker.ietf.org/doc/html/rfc7523[JWT Bearer] grant.
- ====
- [[oauth2-client-jwt-bearer-access-token]]
- === Requesting an Access Token
- [NOTE]
- ====
- Please refer to the https://datatracker.ietf.org/doc/html/rfc7523#section-2.1[Access Token Request/Response] protocol flow for the JWT Bearer grant.
- ====
- The default implementation of `OAuth2AccessTokenResponseClient` for the JWT Bearer grant is `RestClientJwtBearerTokenResponseClient`, which uses a `RestClient` instance to obtain an access token at the Authorization Server’s Token Endpoint.
- :section-id: jwt-bearer
- :grant-type: JWT Bearer
- :class-name: RestClientJwtBearerTokenResponseClient
- :grant-request: JwtBearerGrantRequest
- :leveloffset: +1
- include::partial$servlet/oauth2/client/rest-client-access-token-response-client.adoc[]
- :leveloffset: -1
- [[oauth2-client-jwt-bearer-authorized-client-provider-builder]]
- === Customize using the Builder
- Whether you customize `RestClientJwtBearerTokenResponseClient` or provide your own implementation of `OAuth2AccessTokenResponseClient`, you can configure it using the `OAuth2AuthorizedClientProviderBuilder` (as an alternative to <<oauth2-client-jwt-bearer-access-token-response-client-bean,publishing a bean>>) as follows:
- .Access Token Response Configuration via Builder
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- // Customize
- OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> jwtBearerTokenResponseClient = ...
- JwtBearerOAuth2AuthorizedClientProvider jwtBearerAuthorizedClientProvider = new JwtBearerOAuth2AuthorizedClientProvider();
- jwtBearerAuthorizedClientProvider.setAccessTokenResponseClient(jwtBearerTokenResponseClient);
- OAuth2AuthorizedClientProvider authorizedClientProvider =
- OAuth2AuthorizedClientProviderBuilder.builder()
- .provider(jwtBearerAuthorizedClientProvider)
- .build();
- // ...
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- // Customize
- val jwtBearerTokenResponseClient: OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> = ...
- val jwtBearerAuthorizedClientProvider = JwtBearerOAuth2AuthorizedClientProvider()
- jwtBearerAuthorizedClientProvider.setAccessTokenResponseClient(jwtBearerTokenResponseClient)
- val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
- .provider(jwtBearerAuthorizedClientProvider)
- .build()
- // ...
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
- ----
- ======
- [[oauth2-client-jwt-bearer-authorized-client-manager]]
- === Using the Access Token
- Given the following Spring Boot properties for an OAuth 2.0 Client registration:
- [source,yaml]
- ----
- spring:
- security:
- oauth2:
- client:
- registration:
- okta:
- client-id: okta-client-id
- client-secret: okta-client-secret
- authorization-grant-type: urn:ietf:params:oauth:grant-type:jwt-bearer
- scope: read
- provider:
- okta:
- token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
- ----
- ...and the `OAuth2AuthorizedClientManager` `@Bean`:
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- @Bean
- public OAuth2AuthorizedClientManager authorizedClientManager(
- ClientRegistrationRepository clientRegistrationRepository,
- OAuth2AuthorizedClientRepository authorizedClientRepository) {
- JwtBearerOAuth2AuthorizedClientProvider jwtBearerAuthorizedClientProvider =
- new JwtBearerOAuth2AuthorizedClientProvider();
- OAuth2AuthorizedClientProvider authorizedClientProvider =
- OAuth2AuthorizedClientProviderBuilder.builder()
- .provider(jwtBearerAuthorizedClientProvider)
- .build();
- DefaultOAuth2AuthorizedClientManager authorizedClientManager =
- new DefaultOAuth2AuthorizedClientManager(
- clientRegistrationRepository, authorizedClientRepository);
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
- return authorizedClientManager;
- }
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- @Bean
- fun authorizedClientManager(
- clientRegistrationRepository: ClientRegistrationRepository,
- authorizedClientRepository: OAuth2AuthorizedClientRepository): OAuth2AuthorizedClientManager {
- val jwtBearerAuthorizedClientProvider = JwtBearerOAuth2AuthorizedClientProvider()
- val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
- .provider(jwtBearerAuthorizedClientProvider)
- .build()
- val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
- clientRegistrationRepository, authorizedClientRepository)
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
- return authorizedClientManager
- }
- ----
- ======
- You may obtain the `OAuth2AccessToken` as follows:
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- @RestController
- public class OAuth2ResourceServerController {
- @Autowired
- private OAuth2AuthorizedClientManager authorizedClientManager;
- @GetMapping("/resource")
- public String resource(JwtAuthenticationToken jwtAuthentication) {
- OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
- .principal(jwtAuthentication)
- .build();
- OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest);
- OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
- // ...
- }
- }
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- class OAuth2ResourceServerController {
- @Autowired
- private lateinit var authorizedClientManager: OAuth2AuthorizedClientManager
- @GetMapping("/resource")
- fun resource(jwtAuthentication: JwtAuthenticationToken?): String {
- val authorizeRequest: OAuth2AuthorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
- .principal(jwtAuthentication)
- .build()
- val authorizedClient = authorizedClientManager.authorize(authorizeRequest)
- val accessToken: OAuth2AccessToken = authorizedClient.accessToken
- // ...
- }
- }
- ----
- ======
- [NOTE]
- ====
- `JwtBearerOAuth2AuthorizedClientProvider` resolves the `Jwt` assertion via `OAuth2AuthorizationContext.getPrincipal().getPrincipal()` by default, hence the use of `JwtAuthenticationToken` in the preceding example.
- ====
- [TIP]
- ====
- If you need to resolve the `Jwt` assertion from a different source, you can provide `JwtBearerOAuth2AuthorizedClientProvider.setJwtAssertionResolver()` with a custom `Function<OAuth2AuthorizationContext, Jwt>`.
- ====
- [[oauth2-client-token-exchange]]
- == [[oauth2Client-token-exchange-grant]]Token Exchange
- [NOTE]
- ====
- Please refer to OAuth 2.0 Token Exchange for further details on the https://datatracker.ietf.org/doc/html/rfc8693[Token Exchange] grant.
- ====
- [[oauth2-client-token-exchange-access-token]]
- === Requesting an Access Token
- [NOTE]
- ====
- Please refer to the https://datatracker.ietf.org/doc/html/rfc8693#section-2[Token Exchange Request and Response] protocol flow for the Token Exchange grant.
- ====
- The default implementation of `OAuth2AccessTokenResponseClient` for the Token Exchange grant is `RestClientTokenExchangeTokenResponseClient`, which uses a `RestClient` instance to obtain an access token at the Authorization Server’s Token Endpoint.
- :section-id: token-exchange
- :grant-type: Token Exchange
- :class-name: RestClientTokenExchangeTokenResponseClient
- :grant-request: TokenExchangeGrantRequest
- :leveloffset: +1
- include::partial$servlet/oauth2/client/rest-client-access-token-response-client.adoc[]
- :leveloffset: -1
- [[oauth2-client-token-exchange-authorized-client-provider-builder]]
- === Customize using the Builder
- Whether you customize `RestClientTokenExchangeTokenResponseClient` or provide your own implementation of `OAuth2AccessTokenResponseClient`, you can configure it using the `OAuth2AuthorizedClientProviderBuilder` (as an alternative to <<oauth2-client-token-exchange-access-token-response-client-bean,publishing a bean>>) as follows:
- .Access Token Response Configuration via Builder
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- // Customize
- OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> tokenExchangeTokenResponseClient = ...
- TokenExchangeOAuth2AuthorizedClientProvider tokenExchangeAuthorizedClientProvider = new TokenExchangeOAuth2AuthorizedClientProvider();
- tokenExchangeAuthorizedClientProvider.setAccessTokenResponseClient(tokenExchangeTokenResponseClient);
- OAuth2AuthorizedClientProvider authorizedClientProvider =
- OAuth2AuthorizedClientProviderBuilder.builder()
- .provider(tokenExchangeAuthorizedClientProvider)
- .build();
- // ...
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- // Customize
- val tokenExchangeTokenResponseClient: OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> = ...
- val tokenExchangeAuthorizedClientProvider = TokenExchangeOAuth2AuthorizedClientProvider()
- tokenExchangeAuthorizedClientProvider.setAccessTokenResponseClient(tokenExchangeTokenResponseClient)
- val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
- .provider(tokenExchangeAuthorizedClientProvider)
- .build()
- // ...
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
- ----
- ======
- [[oauth2-client-token-exchange-authorized-client-manager]]
- === [[token-exchange-grant-access-token]]Using the Access Token
- Given the following Spring Boot properties for an OAuth 2.0 Client registration:
- [source,yaml]
- ----
- spring:
- security:
- oauth2:
- client:
- registration:
- okta:
- client-id: okta-client-id
- client-secret: okta-client-secret
- authorization-grant-type: urn:ietf:params:oauth:grant-type:token-exchange
- scope: read
- provider:
- okta:
- token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
- ----
- ...and the `OAuth2AuthorizedClientManager` `@Bean`:
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- @Bean
- public OAuth2AuthorizedClientManager authorizedClientManager(
- ClientRegistrationRepository clientRegistrationRepository,
- OAuth2AuthorizedClientRepository authorizedClientRepository) {
- TokenExchangeOAuth2AuthorizedClientProvider tokenExchangeAuthorizedClientProvider =
- new TokenExchangeOAuth2AuthorizedClientProvider();
- OAuth2AuthorizedClientProvider authorizedClientProvider =
- OAuth2AuthorizedClientProviderBuilder.builder()
- .provider(tokenExchangeAuthorizedClientProvider)
- .build();
- DefaultOAuth2AuthorizedClientManager authorizedClientManager =
- new DefaultOAuth2AuthorizedClientManager(
- clientRegistrationRepository, authorizedClientRepository);
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
- return authorizedClientManager;
- }
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- @Bean
- fun authorizedClientManager(
- clientRegistrationRepository: ClientRegistrationRepository,
- authorizedClientRepository: OAuth2AuthorizedClientRepository): OAuth2AuthorizedClientManager {
- val tokenExchangeAuthorizedClientProvider = TokenExchangeOAuth2AuthorizedClientProvider()
- val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
- .provider(tokenExchangeAuthorizedClientProvider)
- .build()
- val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
- clientRegistrationRepository, authorizedClientRepository)
- authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
- return authorizedClientManager
- }
- ----
- ======
- You may obtain the `OAuth2AccessToken` as follows:
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- @RestController
- public class OAuth2ResourceServerController {
- @Autowired
- private OAuth2AuthorizedClientManager authorizedClientManager;
- @GetMapping("/resource")
- public String resource(JwtAuthenticationToken jwtAuthentication) {
- OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
- .principal(jwtAuthentication)
- .build();
- OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest);
- OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
- // ...
- }
- }
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- class OAuth2ResourceServerController {
- @Autowired
- private lateinit var authorizedClientManager: OAuth2AuthorizedClientManager
- @GetMapping("/resource")
- fun resource(jwtAuthentication: JwtAuthenticationToken?): String {
- val authorizeRequest: OAuth2AuthorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
- .principal(jwtAuthentication)
- .build()
- val authorizedClient = authorizedClientManager.authorize(authorizeRequest)
- val accessToken: OAuth2AccessToken = authorizedClient.accessToken
- // ...
- }
- }
- ----
- ======
- [NOTE]
- ====
- `TokenExchangeOAuth2AuthorizedClientProvider` resolves the subject token (as an `OAuth2Token`) via `OAuth2AuthorizationContext.getPrincipal().getPrincipal()` by default, hence the use of `JwtAuthenticationToken` in the preceding example.
- An actor token is not resolved by default.
- ====
- [TIP]
- ====
- If you need to resolve the subject token from a different source, you can provide `TokenExchangeOAuth2AuthorizedClientProvider.setSubjectTokenResolver()` with a custom `Function<OAuth2AuthorizationContext, OAuth2Token>`.
- ====
- [TIP]
- ====
- If you need to resolve an actor token, you can provide `TokenExchangeOAuth2AuthorizedClientProvider.setActorTokenResolver()` with a custom `Function<OAuth2AuthorizationContext, OAuth2Token>`.
- ====
|