authorization-grants.adoc 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  1. [[oauth2-client-authorization-grants]]
  2. = [[oauth2Client-auth-grant-support]]Authorization Grant Support
  3. This section describes Spring Security's support for authorization grants.
  4. [[oauth2-client-authorization-code]]
  5. == [[oauth2Client-auth-code-grant]]Authorization Code
  6. [NOTE]
  7. ====
  8. Please refer to the OAuth 2.0 Authorization Framework for further details on the https://tools.ietf.org/html/rfc6749#section-1.3.1[Authorization Code] grant.
  9. ====
  10. [[oauth2-client-authorization-code-authorization]]
  11. === Obtaining Authorization
  12. [NOTE]
  13. ====
  14. Please refer to the https://tools.ietf.org/html/rfc6749#section-4.1.1[Authorization Request/Response] protocol flow for the Authorization Code grant.
  15. ====
  16. [[oauth2-client-authorization-code-authorization-request]]
  17. === Initiating the Authorization Request
  18. The `OAuth2AuthorizationRequestRedirectWebFilter` uses a `ServerOAuth2AuthorizationRequestResolver` 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.
  19. The primary role of the `ServerOAuth2AuthorizationRequestResolver` is to resolve an `OAuth2AuthorizationRequest` from the provided web request.
  20. The default implementation `DefaultServerOAuth2AuthorizationRequestResolver` matches on the (default) path `+/oauth2/authorization/{registrationId}+` extracting the `registrationId` and using it to build the `OAuth2AuthorizationRequest` for the associated `ClientRegistration`.
  21. Given the following Spring Boot properties for an OAuth 2.0 Client registration:
  22. [source,yaml,attrs="-attributes"]
  23. ----
  24. spring:
  25. security:
  26. oauth2:
  27. client:
  28. registration:
  29. okta:
  30. client-id: okta-client-id
  31. client-secret: okta-client-secret
  32. authorization-grant-type: authorization_code
  33. redirect-uri: "{baseUrl}/authorized/okta"
  34. scope: read, write
  35. provider:
  36. okta:
  37. authorization-uri: https://dev-1234.oktapreview.com/oauth2/v1/authorize
  38. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  39. ----
  40. A request with the base path `/oauth2/authorization/okta` will initiate the Authorization Request redirect by the `OAuth2AuthorizationRequestRedirectWebFilter` and ultimately start the Authorization Code grant flow.
  41. [NOTE]
  42. ====
  43. The `AuthorizationCodeReactiveOAuth2AuthorizedClientProvider` is an implementation of `ReactiveOAuth2AuthorizedClientProvider` for the Authorization Code grant,
  44. which also initiates the Authorization Request redirect by the `OAuth2AuthorizationRequestRedirectWebFilter`.
  45. ====
  46. If the OAuth 2.0 Client is a https://tools.ietf.org/html/rfc6749#section-2.1[Public Client], then configure the OAuth 2.0 Client registration as follows:
  47. [source,yaml,attrs="-attributes"]
  48. ----
  49. spring:
  50. security:
  51. oauth2:
  52. client:
  53. registration:
  54. okta:
  55. client-id: okta-client-id
  56. client-authentication-method: none
  57. authorization-grant-type: authorization_code
  58. redirect-uri: "{baseUrl}/authorized/okta"
  59. # ...
  60. ----
  61. Public Clients are supported using https://tools.ietf.org/html/rfc7636[Proof Key for Code Exchange] (PKCE).
  62. If the client is running in an untrusted environment (eg. native application or web browser-based application) and therefore incapable of maintaining the confidentiality of it's credentials, PKCE will automatically be used when the following conditions are true:
  63. . `client-secret` is omitted (or empty)
  64. . `client-authentication-method` is set to "none" (`ClientAuthenticationMethod.NONE`)
  65. [TIP]
  66. ====
  67. 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 `DefaultServerOAuth2AuthorizationRequestResolver.setAuthorizationRequestCustomizer(OAuth2AuthorizationRequestCustomizers.withPkce())`.
  68. ====
  69. [[oauth2-client-authorization-code-redirect-uri]]
  70. [[oauth2Client-auth-code-redirect-uri]]The `DefaultServerOAuth2AuthorizationRequestResolver` also supports `URI` template variables for the `redirect-uri` using `UriComponentsBuilder`.
  71. The following configuration uses all the supported `URI` template variables:
  72. [source,yaml,attrs="-attributes"]
  73. ----
  74. spring:
  75. security:
  76. oauth2:
  77. client:
  78. registration:
  79. okta:
  80. # ...
  81. redirect-uri: "{baseScheme}://{baseHost}{basePort}{basePath}/authorized/{registrationId}"
  82. # ...
  83. ----
  84. [NOTE]
  85. ====
  86. `+{baseUrl}+` resolves to `+{baseScheme}://{baseHost}{basePort}{basePath}+`
  87. ====
  88. 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].
  89. This ensures that the `X-Forwarded-*` headers are used when expanding the `redirect-uri`.
  90. [[oauth2-client-authorization-code-authorization-request-resolver]]
  91. === Customizing the Authorization Request
  92. One of the primary use cases a `ServerOAuth2AuthorizationRequestResolver` 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.
  93. 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].
  94. One of those extended parameters is the `prompt` parameter.
  95. [NOTE]
  96. ====
  97. 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`.
  98. ====
  99. The following example shows how to configure the `DefaultServerOAuth2AuthorizationRequestResolver` with a `Consumer<OAuth2AuthorizationRequest.Builder>` that customizes the Authorization Request for `oauth2Login()`, by including the request parameter `prompt=consent`.
  100. [tabs]
  101. ======
  102. Java::
  103. +
  104. [source,java,role="primary"]
  105. ----
  106. @Configuration
  107. @EnableWebFluxSecurity
  108. public class OAuth2LoginSecurityConfig {
  109. @Autowired
  110. private ReactiveClientRegistrationRepository clientRegistrationRepository;
  111. @Bean
  112. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  113. http
  114. .authorizeExchange(authorize -> authorize
  115. .anyExchange().authenticated()
  116. )
  117. .oauth2Login(oauth2 -> oauth2
  118. .authorizationRequestResolver(
  119. authorizationRequestResolver(this.clientRegistrationRepository)
  120. )
  121. );
  122. return http.build();
  123. }
  124. private ServerOAuth2AuthorizationRequestResolver authorizationRequestResolver(
  125. ReactiveClientRegistrationRepository clientRegistrationRepository) {
  126. DefaultServerOAuth2AuthorizationRequestResolver authorizationRequestResolver =
  127. new DefaultServerOAuth2AuthorizationRequestResolver(
  128. clientRegistrationRepository);
  129. authorizationRequestResolver.setAuthorizationRequestCustomizer(
  130. authorizationRequestCustomizer());
  131. return authorizationRequestResolver;
  132. }
  133. private Consumer<OAuth2AuthorizationRequest.Builder> authorizationRequestCustomizer() {
  134. return customizer -> customizer
  135. .additionalParameters(params -> params.put("prompt", "consent"));
  136. }
  137. }
  138. ----
  139. Kotlin::
  140. +
  141. [source,kotlin,role="secondary"]
  142. ----
  143. @Configuration
  144. @EnableWebFluxSecurity
  145. class SecurityConfig {
  146. @Autowired
  147. private lateinit var customClientRegistrationRepository: ReactiveClientRegistrationRepository
  148. @Bean
  149. fun securityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  150. http {
  151. authorizeExchange {
  152. authorize(anyExchange, authenticated)
  153. }
  154. oauth2Login {
  155. authorizationRequestResolver = authorizationRequestResolver(customClientRegistrationRepository)
  156. }
  157. }
  158. return http.build()
  159. }
  160. private fun authorizationRequestResolver(
  161. clientRegistrationRepository: ReactiveClientRegistrationRepository): ServerOAuth2AuthorizationRequestResolver {
  162. val authorizationRequestResolver = DefaultServerOAuth2AuthorizationRequestResolver(
  163. clientRegistrationRepository)
  164. authorizationRequestResolver.setAuthorizationRequestCustomizer(
  165. authorizationRequestCustomizer())
  166. return authorizationRequestResolver
  167. }
  168. private fun authorizationRequestCustomizer(): Consumer<OAuth2AuthorizationRequest.Builder> {
  169. return Consumer { customizer ->
  170. customizer
  171. .additionalParameters { params -> params["prompt"] = "consent" }
  172. }
  173. }
  174. }
  175. ----
  176. ======
  177. 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.
  178. For example, if the value for the request parameter `prompt` is always `consent` for the provider `okta`, than simply configure as follows:
  179. [source,yaml]
  180. ----
  181. spring:
  182. security:
  183. oauth2:
  184. client:
  185. provider:
  186. okta:
  187. authorization-uri: https://dev-1234.oktapreview.com/oauth2/v1/authorize?prompt=consent
  188. ----
  189. The preceding example shows the common use case of adding a custom parameter on top of the standard parameters.
  190. 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.
  191. [TIP]
  192. ====
  193. `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.
  194. ====
  195. The following example shows a variation of `authorizationRequestCustomizer()` from the preceding example, and instead overrides the `OAuth2AuthorizationRequest.authorizationRequestUri` property.
  196. [tabs]
  197. ======
  198. Java::
  199. +
  200. [source,java,role="primary"]
  201. ----
  202. private Consumer<OAuth2AuthorizationRequest.Builder> authorizationRequestCustomizer() {
  203. return customizer -> customizer
  204. .authorizationRequestUri(uriBuilder -> uriBuilder
  205. .queryParam("prompt", "consent").build());
  206. }
  207. ----
  208. Kotlin::
  209. +
  210. [source,kotlin,role="secondary"]
  211. ----
  212. private fun authorizationRequestCustomizer(): Consumer<OAuth2AuthorizationRequest.Builder> {
  213. return Consumer { customizer: OAuth2AuthorizationRequest.Builder ->
  214. customizer
  215. .authorizationRequestUri { uriBuilder: UriBuilder ->
  216. uriBuilder
  217. .queryParam("prompt", "consent").build()
  218. }
  219. }
  220. }
  221. ----
  222. ======
  223. [[oauth2-client-authorization-code-authorization-request-repository]]
  224. === Storing the Authorization Request
  225. The `ServerAuthorizationRequestRepository` 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).
  226. [TIP]
  227. ====
  228. The `OAuth2AuthorizationRequest` is used to correlate and validate the Authorization Response.
  229. ====
  230. The default implementation of `ServerAuthorizationRequestRepository` is `WebSessionOAuth2ServerAuthorizationRequestRepository`, which stores the `OAuth2AuthorizationRequest` in the `WebSession`.
  231. If you have a custom implementation of `ServerAuthorizationRequestRepository`, you may configure it as shown in the following example:
  232. .ServerAuthorizationRequestRepository Configuration
  233. [tabs]
  234. ======
  235. Java::
  236. +
  237. [source,java,role="primary"]
  238. ----
  239. @Configuration
  240. @EnableWebFluxSecurity
  241. public class OAuth2ClientSecurityConfig {
  242. @Bean
  243. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  244. http
  245. .oauth2Client(oauth2 -> oauth2
  246. .authorizationRequestRepository(this.authorizationRequestRepository())
  247. // ...
  248. );
  249. return http.build();
  250. }
  251. }
  252. ----
  253. Kotlin::
  254. +
  255. [source,kotlin,role="secondary"]
  256. ----
  257. @Configuration
  258. @EnableWebFluxSecurity
  259. class OAuth2ClientSecurityConfig {
  260. @Bean
  261. fun securityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  262. http {
  263. oauth2Client {
  264. authorizationRequestRepository = authorizationRequestRepository()
  265. }
  266. }
  267. return http.build()
  268. }
  269. }
  270. ----
  271. ======
  272. [[oauth2-client-authorization-code-access-token]]
  273. === Requesting an Access Token
  274. [NOTE]
  275. ====
  276. Please refer to the https://tools.ietf.org/html/rfc6749#section-4.1.3[Access Token Request/Response] protocol flow for the Authorization Code grant.
  277. ====
  278. The default implementation of `ReactiveOAuth2AccessTokenResponseClient` for the Authorization Code grant is `WebClientReactiveAuthorizationCodeTokenResponseClient`, which uses a `WebClient` for exchanging an authorization code for an access token at the Authorization Server’s Token Endpoint.
  279. :section-id: authorization-code
  280. :grant-type: Authorization Code
  281. :class-name: WebClientReactiveAuthorizationCodeTokenResponseClient
  282. :grant-request: OAuth2AuthorizationCodeGrantRequest
  283. :leveloffset: +1
  284. include::partial$reactive/oauth2/client/web-client-access-token-response-client.adoc[]
  285. :leveloffset: -1
  286. [[oauth2-client-authorization-code-access-token-response-client-dsl]]
  287. === Customize using the DSL
  288. Whether you customize `{class-name}` or provide your own implementation of `ReactiveOAuth2AccessTokenResponseClient`, you can configure it using the DSL (as an alternative to <<oauth2-client-authorization-code-access-token-response-client-bean,publishing a bean>>) as shown in the following example:
  289. .Access Token Response Configuration via DSL
  290. [tabs]
  291. ======
  292. Java::
  293. +
  294. [source,java,role="primary"]
  295. ----
  296. @Configuration
  297. @EnableWebFluxSecurity
  298. public class OAuth2ClientSecurityConfig {
  299. @Bean
  300. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  301. http
  302. .oauth2Client(oauth2 -> oauth2
  303. .authenticationManager(this.authorizationCodeAuthenticationManager())
  304. // ...
  305. );
  306. return http.build();
  307. }
  308. private ReactiveAuthenticationManager authorizationCodeAuthenticationManager() {
  309. WebClientReactiveAuthorizationCodeTokenResponseClient accessTokenResponseClient =
  310. new WebClientReactiveAuthorizationCodeTokenResponseClient();
  311. // ...
  312. return new OAuth2AuthorizationCodeReactiveAuthenticationManager(accessTokenResponseClient);
  313. }
  314. }
  315. ----
  316. Kotlin::
  317. +
  318. [source,kotlin,role="secondary"]
  319. ----
  320. @Configuration
  321. @EnableWebFluxSecurity
  322. class OAuth2ClientSecurityConfig {
  323. @Bean
  324. fun securityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  325. http {
  326. oauth2Client {
  327. authenticationManager = authorizationCodeAuthenticationManager()
  328. }
  329. }
  330. return http.build()
  331. }
  332. private fun authorizationCodeAuthenticationManager(): ReactiveAuthenticationManager {
  333. val accessTokenResponseClient = WebClientReactiveAuthorizationCodeTokenResponseClient()
  334. // ...
  335. return OAuth2AuthorizationCodeReactiveAuthenticationManager(accessTokenResponseClient)
  336. }
  337. }
  338. ----
  339. ======
  340. [[oauth2-client-refresh-token]]
  341. == [[oauth2Client-refresh-token-grant]]Refresh Token
  342. [NOTE]
  343. ====
  344. Please refer to the OAuth 2.0 Authorization Framework for further details on the https://tools.ietf.org/html/rfc6749#section-1.5[Refresh Token].
  345. ====
  346. [[oauth2-client-refresh-token-access-token]]
  347. === Refreshing an Access Token
  348. [NOTE]
  349. ====
  350. Please refer to the https://tools.ietf.org/html/rfc6749#section-6[Access Token Request/Response] protocol flow for the Refresh Token grant.
  351. ====
  352. The default implementation of `ReactiveOAuth2AccessTokenResponseClient` for the Refresh Token grant is `WebClientReactiveRefreshTokenTokenResponseClient`, which uses a `WebClient` when refreshing an access token at the Authorization Server’s Token Endpoint.
  353. :section-id: refresh-token
  354. :grant-type: Refresh Token
  355. :class-name: WebClientReactiveRefreshTokenTokenResponseClient
  356. :grant-request: OAuth2RefreshTokenGrantRequest
  357. :leveloffset: +1
  358. include::partial$reactive/oauth2/client/web-client-access-token-response-client.adoc[]
  359. :leveloffset: -1
  360. [[oauth2-client-refresh-token-authorized-client-provider-builder]]
  361. === Customize using the Builder
  362. Whether you customize `WebClientReactiveRefreshTokenTokenResponseClient` or provide your own implementation of `ReactiveOAuth2AccessTokenResponseClient`, you can configure it using the `ReactiveOAuth2AuthorizedClientProviderBuilder` (as an alternative to <<oauth2-client-refresh-token-access-token-response-client-bean,publishing a bean>>) as follows:
  363. .Access Token Response Configuration via Builder
  364. [tabs]
  365. ======
  366. Java::
  367. +
  368. [source,java,role="primary"]
  369. ----
  370. // Customize
  371. ReactiveOAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> refreshTokenTokenResponseClient = ...
  372. ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
  373. ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  374. .authorizationCode()
  375. .refreshToken(configurer -> configurer.accessTokenResponseClient(refreshTokenTokenResponseClient))
  376. .build();
  377. // ...
  378. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  379. ----
  380. Kotlin::
  381. +
  382. [source,kotlin,role="secondary"]
  383. ----
  384. // Customize
  385. val refreshTokenTokenResponseClient: ReactiveOAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> = ...
  386. val authorizedClientProvider: ReactiveOAuth2AuthorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  387. .authorizationCode()
  388. .refreshToken { it.accessTokenResponseClient(refreshTokenTokenResponseClient) }
  389. .build()
  390. // ...
  391. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  392. ----
  393. ======
  394. [NOTE]
  395. ====
  396. `ReactiveOAuth2AuthorizedClientProviderBuilder.builder().refreshToken()` configures a `RefreshTokenReactiveOAuth2AuthorizedClientProvider`,
  397. which is an implementation of a `ReactiveOAuth2AuthorizedClientProvider` for the Refresh Token grant.
  398. ====
  399. The `OAuth2RefreshToken` may optionally be returned in the Access Token Response for the `authorization_code` and `password` grant types.
  400. If the `OAuth2AuthorizedClient.getRefreshToken()` is available and the `OAuth2AuthorizedClient.getAccessToken()` is expired, it will automatically be refreshed by the `RefreshTokenReactiveOAuth2AuthorizedClientProvider`.
  401. [[oauth2-client-client-credentials]]
  402. == [[oauth2Client-client-creds-grant]]Client Credentials
  403. [NOTE]
  404. ====
  405. 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.
  406. ====
  407. [[oauth2-client-client-credentials-access-token]]
  408. === Requesting an Access Token
  409. [NOTE]
  410. ====
  411. Please refer to the https://tools.ietf.org/html/rfc6749#section-4.4.2[Access Token Request/Response] protocol flow for the Client Credentials grant.
  412. ====
  413. The default implementation of `ReactiveOAuth2AccessTokenResponseClient` for the Client Credentials grant is `WebClientReactiveClientCredentialsTokenResponseClient`, which uses a `WebClient` when requesting an access token at the Authorization Server’s Token Endpoint.
  414. :section-id: client-credentials
  415. :grant-type: Client Credentials
  416. :class-name: WebClientReactiveClientCredentialsTokenResponseClient
  417. :grant-request: OAuth2ClientCredentialsGrantRequest
  418. :leveloffset: +1
  419. include::partial$reactive/oauth2/client/web-client-access-token-response-client.adoc[]
  420. :leveloffset: -1
  421. [[oauth2-client-client-credentials-authorized-client-provider-builder]]
  422. === Customize using the Builder
  423. Whether you customize `WebClientReactiveClientCredentialsTokenResponseClient` or provide your own implementation of `ReactiveOAuth2AccessTokenResponseClient`, you can configure it using the `ReactiveOAuth2AuthorizedClientProviderBuilder` (as an alternative to <<oauth2-client-client-credentials-access-token-response-client-bean,publishing a bean>>) as follows:
  424. .Access Token Response Configuration via Builder
  425. [tabs]
  426. ======
  427. Java::
  428. +
  429. [source,java,role="primary"]
  430. ----
  431. // Customize
  432. ReactiveOAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsTokenResponseClient = ...
  433. ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
  434. ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  435. .clientCredentials(configurer -> configurer.accessTokenResponseClient(clientCredentialsTokenResponseClient))
  436. .build();
  437. // ...
  438. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  439. ----
  440. Kotlin::
  441. +
  442. [source,kotlin,role="secondary"]
  443. ----
  444. // Customize
  445. val clientCredentialsTokenResponseClient: ReactiveOAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> = ...
  446. val authorizedClientProvider: ReactiveOAuth2AuthorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  447. .clientCredentials { it.accessTokenResponseClient(clientCredentialsTokenResponseClient) }
  448. .build()
  449. // ...
  450. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  451. ----
  452. ======
  453. [NOTE]
  454. ====
  455. `ReactiveOAuth2AuthorizedClientProviderBuilder.builder().clientCredentials()` configures a `ClientCredentialsReactiveOAuth2AuthorizedClientProvider`, which is an implementation of a `ReactiveOAuth2AuthorizedClientProvider` for the Client Credentials grant.
  456. ====
  457. [[oauth2-client-client-credentials-authorized-client-manager]]
  458. === Using the Access Token
  459. Given the following Spring Boot properties for an OAuth 2.0 Client registration:
  460. [source,yaml]
  461. ----
  462. spring:
  463. security:
  464. oauth2:
  465. client:
  466. registration:
  467. okta:
  468. client-id: okta-client-id
  469. client-secret: okta-client-secret
  470. authorization-grant-type: client_credentials
  471. scope: read, write
  472. provider:
  473. okta:
  474. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  475. ----
  476. ...and the `ReactiveOAuth2AuthorizedClientManager` `@Bean`:
  477. [tabs]
  478. ======
  479. Java::
  480. +
  481. [source,java,role="primary"]
  482. ----
  483. @Bean
  484. public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
  485. ReactiveClientRegistrationRepository clientRegistrationRepository,
  486. ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
  487. ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
  488. ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  489. .clientCredentials()
  490. .build();
  491. DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
  492. new DefaultReactiveOAuth2AuthorizedClientManager(
  493. clientRegistrationRepository, authorizedClientRepository);
  494. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  495. return authorizedClientManager;
  496. }
  497. ----
  498. Kotlin::
  499. +
  500. [source,kotlin,role="secondary"]
  501. ----
  502. @Bean
  503. fun authorizedClientManager(
  504. clientRegistrationRepository: ReactiveClientRegistrationRepository,
  505. authorizedClientRepository: ServerOAuth2AuthorizedClientRepository): ReactiveOAuth2AuthorizedClientManager {
  506. val authorizedClientProvider: ReactiveOAuth2AuthorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  507. .clientCredentials()
  508. .build()
  509. val authorizedClientManager = DefaultReactiveOAuth2AuthorizedClientManager(
  510. clientRegistrationRepository, authorizedClientRepository)
  511. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  512. return authorizedClientManager
  513. }
  514. ----
  515. ======
  516. You may obtain the `OAuth2AccessToken` as follows:
  517. [tabs]
  518. ======
  519. Java::
  520. +
  521. [source,java,role="primary"]
  522. ----
  523. @Controller
  524. public class OAuth2ClientController {
  525. @Autowired
  526. private ReactiveOAuth2AuthorizedClientManager authorizedClientManager;
  527. @GetMapping("/")
  528. public Mono<String> index(Authentication authentication, ServerWebExchange exchange) {
  529. OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  530. .principal(authentication)
  531. .attribute(ServerWebExchange.class.getName(), exchange)
  532. .build();
  533. return this.authorizedClientManager.authorize(authorizeRequest)
  534. .map(OAuth2AuthorizedClient::getAccessToken)
  535. // ...
  536. .thenReturn("index");
  537. }
  538. }
  539. ----
  540. Kotlin::
  541. +
  542. [source,kotlin,role="secondary"]
  543. ----
  544. class OAuth2ClientController {
  545. @Autowired
  546. private lateinit var authorizedClientManager: ReactiveOAuth2AuthorizedClientManager
  547. @GetMapping("/")
  548. fun index(authentication: Authentication, exchange: ServerWebExchange): Mono<String> {
  549. val authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  550. .principal(authentication)
  551. .attribute(ServerWebExchange::class.java.name, exchange)
  552. .build()
  553. return authorizedClientManager.authorize(authorizeRequest)
  554. .map { it.accessToken }
  555. // ...
  556. .thenReturn("index")
  557. }
  558. }
  559. ----
  560. ======
  561. [NOTE]
  562. ====
  563. `ServerWebExchange` is an OPTIONAL attribute.
  564. If not provided, it will be obtained from the https://projectreactor.io/docs/core/release/reference/#context[Reactor's Context] via the key `ServerWebExchange.class`.
  565. ====
  566. [[oauth2-client-password]]
  567. == [[oauth2Client-password-grant]]Resource Owner Password Credentials
  568. [NOTE]
  569. ====
  570. Please refer to the OAuth 2.0 Authorization Framework for further details on the https://tools.ietf.org/html/rfc6749#section-1.3.3[Resource Owner Password Credentials] grant.
  571. ====
  572. [[oauth2-client-password-access-token]]
  573. === Requesting an Access Token
  574. [NOTE]
  575. ====
  576. Please refer to the https://tools.ietf.org/html/rfc6749#section-4.3.2[Access Token Request/Response] protocol flow for the Resource Owner Password Credentials grant.
  577. ====
  578. The default implementation of `ReactiveOAuth2AccessTokenResponseClient` for the Resource Owner Password Credentials grant is `WebClientReactivePasswordTokenResponseClient`, which uses a `WebClient` when requesting an access token at the Authorization Server’s Token Endpoint.
  579. [CAUTION]
  580. ====
  581. The `WebClientReactivePasswordTokenResponseClient` class and support for the Resource Owner Password Credentials grant are deprecated.
  582. This section will be removed in Spring Security 7.
  583. ====
  584. :section-id: password
  585. :grant-type: Password
  586. :class-name: WebClientReactivePasswordTokenResponseClient
  587. :grant-request: OAuth2PasswordGrantRequest
  588. :leveloffset: +1
  589. include::partial$reactive/oauth2/client/web-client-access-token-response-client.adoc[]
  590. :leveloffset: -1
  591. [[oauth2-client-password-authorized-client-provider-builder]]
  592. === Customize using the Builder
  593. Whether you customize `WebClientReactivePasswordTokenResponseClient` or provide your own implementation of `ReactiveOAuth2AccessTokenResponseClient`, you can configure it using the `ReactiveOAuth2AuthorizedClientProviderBuilder` (as an alternative to <<oauth2-client-password-access-token-response-client-bean,publishing a bean>>) as follows:
  594. .Access Token Response Configuration via Builder
  595. [tabs]
  596. ======
  597. Java::
  598. +
  599. [source,java,role="primary"]
  600. ----
  601. // Customize
  602. ReactiveOAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> passwordTokenResponseClient = ...
  603. ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
  604. ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  605. .password(configurer -> configurer.accessTokenResponseClient(passwordTokenResponseClient))
  606. .refreshToken()
  607. .build();
  608. // ...
  609. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  610. ----
  611. Kotlin::
  612. +
  613. [source,kotlin,role="secondary"]
  614. ----
  615. val passwordTokenResponseClient: ReactiveOAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> = ...
  616. val authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  617. .password { it.accessTokenResponseClient(passwordTokenResponseClient) }
  618. .refreshToken()
  619. .build()
  620. // ...
  621. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  622. ----
  623. ======
  624. [NOTE]
  625. ====
  626. `ReactiveOAuth2AuthorizedClientProviderBuilder.builder().password()` configures a `PasswordReactiveOAuth2AuthorizedClientProvider`,
  627. which is an implementation of a `ReactiveOAuth2AuthorizedClientProvider` for the Resource Owner Password Credentials grant.
  628. ====
  629. [[oauth2-client-password-authorized-client-manager]]
  630. === Using the Access Token
  631. Given the following Spring Boot properties for an OAuth 2.0 Client registration:
  632. [source,yaml]
  633. ----
  634. spring:
  635. security:
  636. oauth2:
  637. client:
  638. registration:
  639. okta:
  640. client-id: okta-client-id
  641. client-secret: okta-client-secret
  642. authorization-grant-type: password
  643. scope: read, write
  644. provider:
  645. okta:
  646. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  647. ----
  648. ...and the `ReactiveOAuth2AuthorizedClientManager` `@Bean`:
  649. [tabs]
  650. ======
  651. Java::
  652. +
  653. [source,java,role="primary"]
  654. ----
  655. @Bean
  656. public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
  657. ReactiveClientRegistrationRepository clientRegistrationRepository,
  658. ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
  659. ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
  660. ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  661. .password()
  662. .refreshToken()
  663. .build();
  664. DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
  665. new DefaultReactiveOAuth2AuthorizedClientManager(
  666. clientRegistrationRepository, authorizedClientRepository);
  667. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  668. // Assuming the `username` and `password` are supplied as `ServerHttpRequest` parameters,
  669. // map the `ServerHttpRequest` parameters to `OAuth2AuthorizationContext.getAttributes()`
  670. authorizedClientManager.setContextAttributesMapper(contextAttributesMapper());
  671. return authorizedClientManager;
  672. }
  673. private Function<OAuth2AuthorizeRequest, Mono<Map<String, Object>>> contextAttributesMapper() {
  674. return authorizeRequest -> {
  675. Map<String, Object> contextAttributes = Collections.emptyMap();
  676. ServerWebExchange exchange = authorizeRequest.getAttribute(ServerWebExchange.class.getName());
  677. ServerHttpRequest request = exchange.getRequest();
  678. String username = request.getQueryParams().getFirst(OAuth2ParameterNames.USERNAME);
  679. String password = request.getQueryParams().getFirst(OAuth2ParameterNames.PASSWORD);
  680. if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
  681. contextAttributes = new HashMap<>();
  682. // `PasswordReactiveOAuth2AuthorizedClientProvider` requires both attributes
  683. contextAttributes.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username);
  684. contextAttributes.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password);
  685. }
  686. return Mono.just(contextAttributes);
  687. };
  688. }
  689. ----
  690. Kotlin::
  691. +
  692. [source,kotlin,role="secondary"]
  693. ----
  694. @Bean
  695. fun authorizedClientManager(
  696. clientRegistrationRepository: ReactiveClientRegistrationRepository,
  697. authorizedClientRepository: ServerOAuth2AuthorizedClientRepository): ReactiveOAuth2AuthorizedClientManager {
  698. val authorizedClientProvider: ReactiveOAuth2AuthorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  699. .password()
  700. .refreshToken()
  701. .build()
  702. val authorizedClientManager = DefaultReactiveOAuth2AuthorizedClientManager(
  703. clientRegistrationRepository, authorizedClientRepository)
  704. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  705. // Assuming the `username` and `password` are supplied as `ServerHttpRequest` parameters,
  706. // map the `ServerHttpRequest` parameters to `OAuth2AuthorizationContext.getAttributes()`
  707. authorizedClientManager.setContextAttributesMapper(contextAttributesMapper())
  708. return authorizedClientManager
  709. }
  710. private fun contextAttributesMapper(): Function<OAuth2AuthorizeRequest, Mono<MutableMap<String, Any>>> {
  711. return Function { authorizeRequest ->
  712. var contextAttributes: MutableMap<String, Any> = mutableMapOf()
  713. val exchange: ServerWebExchange = authorizeRequest.getAttribute(ServerWebExchange::class.java.name)!!
  714. val request: ServerHttpRequest = exchange.request
  715. val username: String? = request.queryParams.getFirst(OAuth2ParameterNames.USERNAME)
  716. val password: String? = request.queryParams.getFirst(OAuth2ParameterNames.PASSWORD)
  717. if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
  718. contextAttributes = hashMapOf()
  719. // `PasswordReactiveOAuth2AuthorizedClientProvider` requires both attributes
  720. contextAttributes[OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME] = username!!
  721. contextAttributes[OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME] = password!!
  722. }
  723. Mono.just(contextAttributes)
  724. }
  725. }
  726. ----
  727. ======
  728. You may obtain the `OAuth2AccessToken` as follows:
  729. [tabs]
  730. ======
  731. Java::
  732. +
  733. [source,java,role="primary"]
  734. ----
  735. @Controller
  736. public class OAuth2ClientController {
  737. @Autowired
  738. private ReactiveOAuth2AuthorizedClientManager authorizedClientManager;
  739. @GetMapping("/")
  740. public Mono<String> index(Authentication authentication, ServerWebExchange exchange) {
  741. OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  742. .principal(authentication)
  743. .attribute(ServerWebExchange.class.getName(), exchange)
  744. .build();
  745. return this.authorizedClientManager.authorize(authorizeRequest)
  746. .map(OAuth2AuthorizedClient::getAccessToken)
  747. // ...
  748. .thenReturn("index");
  749. }
  750. }
  751. ----
  752. Kotlin::
  753. +
  754. [source,kotlin,role="secondary"]
  755. ----
  756. @Controller
  757. class OAuth2ClientController {
  758. @Autowired
  759. private lateinit var authorizedClientManager: ReactiveOAuth2AuthorizedClientManager
  760. @GetMapping("/")
  761. fun index(authentication: Authentication, exchange: ServerWebExchange): Mono<String> {
  762. val authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  763. .principal(authentication)
  764. .attribute(ServerWebExchange::class.java.name, exchange)
  765. .build()
  766. return authorizedClientManager.authorize(authorizeRequest)
  767. .map { it.accessToken }
  768. // ...
  769. .thenReturn("index")
  770. }
  771. }
  772. ----
  773. ======
  774. [NOTE]
  775. ====
  776. `ServerWebExchange` is an OPTIONAL attribute.
  777. If not provided, it will be obtained from the https://projectreactor.io/docs/core/release/reference/#context[Reactor's Context] via the key `ServerWebExchange.class`.
  778. ====
  779. [[oauth2-client-jwt-bearer]]
  780. == [[oauth2Client-jwt-bearer-grant]]JWT Bearer
  781. [NOTE]
  782. ====
  783. 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.
  784. ====
  785. [[oauth2-client-jwt-bearer-access-token]]
  786. === Requesting an Access Token
  787. [NOTE]
  788. ====
  789. 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.
  790. ====
  791. The default implementation of `ReactiveOAuth2AccessTokenResponseClient` for the JWT Bearer grant is `WebClientReactiveJwtBearerTokenResponseClient`, which uses a `WebClient` when requesting an access token at the Authorization Server’s Token Endpoint.
  792. :section-id: jwt-bearer
  793. :grant-type: JWT Bearer
  794. :class-name: WebClientReactiveJwtBearerTokenResponseClient
  795. :grant-request: JwtBearerGrantRequest
  796. :leveloffset: +1
  797. include::partial$reactive/oauth2/client/web-client-access-token-response-client.adoc[]
  798. :leveloffset: -1
  799. [[oauth2-client-jwt-bearer-authorized-client-provider-builder]]
  800. === Customize using the Builder
  801. Whether you customize `WebClientReactiveJwtBearerTokenResponseClient` or provide your own implementation of `ReactiveOAuth2AccessTokenResponseClient`, you can configure it using the `ReactiveOAuth2AuthorizedClientProviderBuilder` (as an alternative to <<oauth2-client-jwt-bearer-access-token-response-client-bean,publishing a bean>>) as follows:
  802. .Access Token Response Configuration via Builder
  803. [tabs]
  804. ======
  805. Java::
  806. +
  807. [source,java,role="primary"]
  808. ----
  809. // Customize
  810. ReactiveOAuth2AccessTokenResponseClient<JwtBearerGrantRequest> jwtBearerTokenResponseClient = ...
  811. JwtBearerReactiveOAuth2AuthorizedClientProvider jwtBearerAuthorizedClientProvider = new JwtBearerReactiveOAuth2AuthorizedClientProvider();
  812. jwtBearerAuthorizedClientProvider.setAccessTokenResponseClient(jwtBearerTokenResponseClient);
  813. ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
  814. ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  815. .provider(jwtBearerAuthorizedClientProvider)
  816. .build();
  817. // ...
  818. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  819. ----
  820. Kotlin::
  821. +
  822. [source,kotlin,role="secondary"]
  823. ----
  824. // Customize
  825. val jwtBearerTokenResponseClient: ReactiveOAuth2AccessTokenResponseClient<JwtBearerGrantRequest> = ...
  826. val jwtBearerAuthorizedClientProvider = JwtBearerReactiveOAuth2AuthorizedClientProvider()
  827. jwtBearerAuthorizedClientProvider.setAccessTokenResponseClient(jwtBearerTokenResponseClient)
  828. val authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  829. .provider(jwtBearerAuthorizedClientProvider)
  830. .build()
  831. // ...
  832. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  833. ----
  834. ======
  835. [[oauth2-client-jwt-bearer-authorized-client-manager]]
  836. === Using the Access Token
  837. Given the following Spring Boot properties for an OAuth 2.0 Client registration:
  838. [source,yaml]
  839. ----
  840. spring:
  841. security:
  842. oauth2:
  843. client:
  844. registration:
  845. okta:
  846. client-id: okta-client-id
  847. client-secret: okta-client-secret
  848. authorization-grant-type: urn:ietf:params:oauth:grant-type:jwt-bearer
  849. scope: read
  850. provider:
  851. okta:
  852. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  853. ----
  854. ...and the `OAuth2AuthorizedClientManager` `@Bean`:
  855. [tabs]
  856. ======
  857. Java::
  858. +
  859. [source,java,role="primary"]
  860. ----
  861. @Bean
  862. public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
  863. ReactiveClientRegistrationRepository clientRegistrationRepository,
  864. ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
  865. JwtBearerReactiveOAuth2AuthorizedClientProvider jwtBearerAuthorizedClientProvider =
  866. new JwtBearerReactiveOAuth2AuthorizedClientProvider();
  867. ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
  868. ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  869. .provider(jwtBearerAuthorizedClientProvider)
  870. .build();
  871. DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
  872. new DefaultReactiveOAuth2AuthorizedClientManager(
  873. clientRegistrationRepository, authorizedClientRepository);
  874. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  875. return authorizedClientManager;
  876. }
  877. ----
  878. Kotlin::
  879. +
  880. [source,kotlin,role="secondary"]
  881. ----
  882. @Bean
  883. fun authorizedClientManager(
  884. clientRegistrationRepository: ReactiveClientRegistrationRepository,
  885. authorizedClientRepository: ServerOAuth2AuthorizedClientRepository): ReactiveOAuth2AuthorizedClientManager {
  886. val jwtBearerAuthorizedClientProvider = JwtBearerReactiveOAuth2AuthorizedClientProvider()
  887. val authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  888. .provider(jwtBearerAuthorizedClientProvider)
  889. .build()
  890. val authorizedClientManager = DefaultReactiveOAuth2AuthorizedClientManager(
  891. clientRegistrationRepository, authorizedClientRepository)
  892. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  893. return authorizedClientManager
  894. }
  895. ----
  896. ======
  897. You may obtain the `OAuth2AccessToken` as follows:
  898. [tabs]
  899. ======
  900. Java::
  901. +
  902. [source,java,role="primary"]
  903. ----
  904. @RestController
  905. public class OAuth2ResourceServerController {
  906. @Autowired
  907. private ReactiveOAuth2AuthorizedClientManager authorizedClientManager;
  908. @GetMapping("/resource")
  909. public Mono<String> resource(JwtAuthenticationToken jwtAuthentication, ServerWebExchange exchange) {
  910. OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  911. .principal(jwtAuthentication)
  912. .build();
  913. return this.authorizedClientManager.authorize(authorizeRequest)
  914. .map(OAuth2AuthorizedClient::getAccessToken)
  915. // ...
  916. .thenReturn("index");
  917. }
  918. }
  919. ----
  920. Kotlin::
  921. +
  922. [source,kotlin,role="secondary"]
  923. ----
  924. class OAuth2ResourceServerController {
  925. @Autowired
  926. private lateinit var authorizedClientManager: ReactiveOAuth2AuthorizedClientManager
  927. @GetMapping("/resource")
  928. fun resource(jwtAuthentication: JwtAuthenticationToken, exchange: ServerWebExchange): Mono<String> {
  929. val authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  930. .principal(jwtAuthentication)
  931. .build()
  932. return authorizedClientManager.authorize(authorizeRequest)
  933. .map { it.accessToken }
  934. // ...
  935. .thenReturn("index")
  936. }
  937. }
  938. ----
  939. ======
  940. [NOTE]
  941. ====
  942. `JwtBearerReactiveOAuth2AuthorizedClientProvider` resolves the `Jwt` assertion via `OAuth2AuthorizationContext.getPrincipal().getPrincipal()` by default, hence the use of `JwtAuthenticationToken` in the preceding example.
  943. ====
  944. [TIP]
  945. ====
  946. If you need to resolve the `Jwt` assertion from a different source, you can provide `JwtBearerReactiveOAuth2AuthorizedClientProvider.setJwtAssertionResolver()` with a custom `Function<OAuth2AuthorizationContext, Mono<Jwt>>`.
  947. ====
  948. [[oauth2-client-token-exchange]]
  949. == [[oauth2Client-token-exchange-grant]]Token Exchange
  950. [NOTE]
  951. ====
  952. Please refer to OAuth 2.0 Token Exchange for further details on the https://datatracker.ietf.org/doc/html/rfc8693[Token Exchange] grant.
  953. ====
  954. [[oauth2-client-token-exchange-access-token]]
  955. === Requesting an Access Token
  956. [NOTE]
  957. ====
  958. 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.
  959. ====
  960. The default implementation of `ReactiveOAuth2AccessTokenResponseClient` for the Token Exchange grant is `WebClientReactiveTokenExchangeTokenResponseClient`, which uses a `WebClient` when requesting an access token at the Authorization Server’s Token Endpoint.
  961. :section-id: token-exchange
  962. :grant-type: Token Exchange
  963. :class-name: WebClientReactiveTokenExchangeTokenResponseClient
  964. :grant-request: TokenExchangeGrantRequest
  965. :leveloffset: +1
  966. include::partial$reactive/oauth2/client/web-client-access-token-response-client.adoc[]
  967. :leveloffset: -1
  968. [[oauth2-client-token-exchange-authorized-client-provider-builder]]
  969. === Customize using the Builder
  970. Whether you customize `WebClientReactiveTokenExchangeTokenResponseClient` or provide your own implementation of `ReactiveOAuth2AccessTokenResponseClient`, you can configure it using the `ReactiveOAuth2AuthorizedClientProviderBuilder` (as an alternative to <<oauth2-client-token-exchange-access-token-response-client-bean,publishing a bean>>) as follows:
  971. .Access Token Response Configuration via Builder
  972. [tabs]
  973. ======
  974. Java::
  975. +
  976. [source,java,role="primary"]
  977. ----
  978. // Customize
  979. ReactiveOAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> tokenExchangeTokenResponseClient = ...
  980. TokenExchangeReactiveOAuth2AuthorizedClientProvider tokenExchangeAuthorizedClientProvider = new TokenExchangeReactiveOAuth2AuthorizedClientProvider();
  981. tokenExchangeAuthorizedClientProvider.setAccessTokenResponseClient(tokenExchangeTokenResponseClient);
  982. ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
  983. ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  984. .provider(tokenExchangeAuthorizedClientProvider)
  985. .build();
  986. // ...
  987. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  988. ----
  989. Kotlin::
  990. +
  991. [source,kotlin,role="secondary"]
  992. ----
  993. // Customize
  994. val tokenExchangeTokenResponseClient: ReactiveOAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> = ...
  995. val tokenExchangeAuthorizedClientProvider = TokenExchangeReactiveOAuth2AuthorizedClientProvider()
  996. tokenExchangeAuthorizedClientProvider.setAccessTokenResponseClient(tokenExchangeTokenResponseClient)
  997. val authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  998. .provider(tokenExchangeAuthorizedClientProvider)
  999. .build()
  1000. // ...
  1001. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  1002. ----
  1003. ======
  1004. [[oauth2-client-token-exchange-authorized-client-manager]]
  1005. === Using the Access Token
  1006. Given the following Spring Boot properties for an OAuth 2.0 Client registration:
  1007. [source,yaml]
  1008. ----
  1009. spring:
  1010. security:
  1011. oauth2:
  1012. client:
  1013. registration:
  1014. okta:
  1015. client-id: okta-client-id
  1016. client-secret: okta-client-secret
  1017. authorization-grant-type: urn:ietf:params:oauth:grant-type:token-exchange
  1018. scope: read
  1019. provider:
  1020. okta:
  1021. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  1022. ----
  1023. ...and the `OAuth2AuthorizedClientManager` `@Bean`:
  1024. [tabs]
  1025. ======
  1026. Java::
  1027. +
  1028. [source,java,role="primary"]
  1029. ----
  1030. @Bean
  1031. public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
  1032. ReactiveClientRegistrationRepository clientRegistrationRepository,
  1033. ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
  1034. TokenExchangeReactiveOAuth2AuthorizedClientProvider tokenExchangeAuthorizedClientProvider =
  1035. new TokenExchangeReactiveOAuth2AuthorizedClientProvider();
  1036. ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
  1037. ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  1038. .provider(tokenExchangeAuthorizedClientProvider)
  1039. .build();
  1040. DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
  1041. new DefaultReactiveOAuth2AuthorizedClientManager(
  1042. clientRegistrationRepository, authorizedClientRepository);
  1043. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  1044. return authorizedClientManager;
  1045. }
  1046. ----
  1047. Kotlin::
  1048. +
  1049. [source,kotlin,role="secondary"]
  1050. ----
  1051. @Bean
  1052. fun authorizedClientManager(
  1053. clientRegistrationRepository: ReactiveClientRegistrationRepository,
  1054. authorizedClientRepository: ServerOAuth2AuthorizedClientRepository): ReactiveOAuth2AuthorizedClientManager {
  1055. val tokenExchangeAuthorizedClientProvider = TokenExchangeReactiveOAuth2AuthorizedClientProvider()
  1056. val authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  1057. .provider(tokenExchangeAuthorizedClientProvider)
  1058. .build()
  1059. val authorizedClientManager = DefaultReactiveOAuth2AuthorizedClientManager(
  1060. clientRegistrationRepository, authorizedClientRepository)
  1061. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  1062. return authorizedClientManager
  1063. }
  1064. ----
  1065. ======
  1066. You may obtain the `OAuth2AccessToken` as follows:
  1067. [tabs]
  1068. ======
  1069. Java::
  1070. +
  1071. [source,java,role="primary"]
  1072. ----
  1073. @RestController
  1074. public class OAuth2ResourceServerController {
  1075. @Autowired
  1076. private ReactiveOAuth2AuthorizedClientManager authorizedClientManager;
  1077. @GetMapping("/resource")
  1078. public Mono<String> resource(JwtAuthenticationToken jwtAuthentication) {
  1079. OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  1080. .principal(jwtAuthentication)
  1081. .build();
  1082. return this.authorizedClientManager.authorize(authorizeRequest)
  1083. .map(OAuth2AuthorizedClient::getAccessToken)
  1084. // ...
  1085. .thenReturn("index");
  1086. }
  1087. }
  1088. ----
  1089. Kotlin::
  1090. +
  1091. [source,kotlin,role="secondary"]
  1092. ----
  1093. class OAuth2ResourceServerController {
  1094. @Autowired
  1095. private lateinit var authorizedClientManager: ReactiveOAuth2AuthorizedClientManager
  1096. @GetMapping("/resource")
  1097. fun resource(jwtAuthentication: JwtAuthenticationToken): Mono<String> {
  1098. val authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  1099. .principal(jwtAuthentication)
  1100. .build()
  1101. return authorizedClientManager.authorize(authorizeRequest)
  1102. .map { it.accessToken }
  1103. // ...
  1104. .thenReturn("index")
  1105. }
  1106. }
  1107. ----
  1108. ======
  1109. [NOTE]
  1110. ====
  1111. `TokenExchangeReactiveOAuth2AuthorizedClientProvider` resolves the subject token (as an `OAuth2Token`) via `OAuth2AuthorizationContext.getPrincipal().getPrincipal()` by default, hence the use of `JwtAuthenticationToken` in the preceding example.
  1112. An actor token is not resolved by default.
  1113. ====
  1114. [TIP]
  1115. ====
  1116. If you need to resolve the subject token from a different source, you can provide `TokenExchangeReactiveOAuth2AuthorizedClientProvider.setSubjectTokenResolver()` with a custom `Function<OAuth2AuthorizationContext, Mono<OAuth2Token>>`.
  1117. ====
  1118. [TIP]
  1119. ====
  1120. If you need to resolve an actor token, you can provide `TokenExchangeReactiveOAuth2AuthorizedClientProvider.setActorTokenResolver()` with a custom `Function<OAuth2AuthorizationContext, Mono<OAuth2Token>>`.
  1121. ====