authorization-grants.adoc 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  1. [[oauth2-client-authorization-grants]]
  2. = [[oauth2Client-auth-grant-support]]Authorization Grant Support
  3. :spring-security-reference-base-url: https://docs.spring.io/spring-security/reference
  4. This section describes Spring Security's support for authorization grants.
  5. [[oauth2-client-authorization-code]]
  6. == [[oauth2Client-auth-code-grant]]Authorization Code
  7. [NOTE]
  8. ====
  9. 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.
  10. ====
  11. [[oauth2-client-authorization-code-authorization]]
  12. === Obtaining Authorization
  13. [NOTE]
  14. ====
  15. See the https://tools.ietf.org/html/rfc6749#section-4.1.1[Authorization Request/Response] protocol flow for the Authorization Code grant.
  16. ====
  17. [[oauth2-client-authorization-code-authorization-request]]
  18. === Initiating the Authorization Request
  19. 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.
  20. The primary role of the `OAuth2AuthorizationRequestResolver` is to resolve an `OAuth2AuthorizationRequest` from the provided web request.
  21. 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`.
  22. Consider the following Spring Boot properties for an OAuth 2.0 Client registration:
  23. [source,yaml,attrs="-attributes"]
  24. ----
  25. spring:
  26. security:
  27. oauth2:
  28. client:
  29. registration:
  30. okta:
  31. client-id: okta-client-id
  32. client-secret: okta-client-secret
  33. authorization-grant-type: authorization_code
  34. redirect-uri: "{baseUrl}/authorized/okta"
  35. scope: read, write
  36. provider:
  37. okta:
  38. authorization-uri: https://dev-1234.oktapreview.com/oauth2/v1/authorize
  39. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  40. ----
  41. 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.
  42. [NOTE]
  43. ====
  44. The `AuthorizationCodeOAuth2AuthorizedClientProvider` is an implementation of `OAuth2AuthorizedClientProvider` for the Authorization Code grant,
  45. which also initiates the Authorization Request redirect by the `OAuth2AuthorizationRequestRedirectFilter`.
  46. ====
  47. 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:
  48. [source,yaml,attrs="-attributes"]
  49. ----
  50. spring:
  51. security:
  52. oauth2:
  53. client:
  54. registration:
  55. okta:
  56. client-id: okta-client-id
  57. client-authentication-method: none
  58. authorization-grant-type: authorization_code
  59. redirect-uri: "{baseUrl}/authorized/okta"
  60. # ...
  61. ----
  62. Public Clients are supported by using https://tools.ietf.org/html/rfc7636[Proof Key for Code Exchange] (PKCE).
  63. 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:
  64. . `client-secret` is omitted (or empty)
  65. . `client-authentication-method` is set to `none` (`ClientAuthenticationMethod.NONE`)
  66. [TIP]
  67. ====
  68. 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())`.
  69. ====
  70. [[oauth2-client-authorization-code-redirect-uri]]
  71. [[oauth2Client-auth-code-redirect-uri]]The `DefaultOAuth2AuthorizationRequestResolver` also supports `URI` template variables for the `redirect-uri` by using `UriComponentsBuilder`.
  72. The following configuration uses all the supported `URI` template variables:
  73. [source,yaml,attrs="-attributes"]
  74. ----
  75. spring:
  76. security:
  77. oauth2:
  78. client:
  79. registration:
  80. okta:
  81. # ...
  82. redirect-uri: "{baseScheme}://{baseHost}{basePort}{basePath}/authorized/{registrationId}"
  83. # ...
  84. ----
  85. [NOTE]
  86. ====
  87. `+{baseUrl}+` resolves to `+{baseScheme}://{baseHost}{basePort}{basePath}+`
  88. ====
  89. 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].
  90. Doing so ensures that the `X-Forwarded-*` headers are used when expanding the `redirect-uri`.
  91. [[oauth2-client-authorization-code-authorization-request-resolver]]
  92. === Customizing the Authorization Request
  93. 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.
  94. 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].
  95. One of those extended parameters is the `prompt` parameter.
  96. [NOTE]
  97. ====
  98. 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`.
  99. ====
  100. 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`.
  101. [tabs]
  102. ======
  103. Java::
  104. +
  105. [source,java,role="primary"]
  106. ----
  107. @Configuration
  108. @EnableWebSecurity
  109. public class OAuth2LoginSecurityConfig {
  110. @Autowired
  111. private ClientRegistrationRepository clientRegistrationRepository;
  112. @Bean
  113. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  114. http
  115. .authorizeHttpRequests(authorize -> authorize
  116. .anyRequest().authenticated()
  117. )
  118. .oauth2Login(oauth2 -> oauth2
  119. .authorizationEndpoint(authorization -> authorization
  120. .authorizationRequestResolver(
  121. authorizationRequestResolver(this.clientRegistrationRepository)
  122. )
  123. )
  124. );
  125. return http.build();
  126. }
  127. private OAuth2AuthorizationRequestResolver authorizationRequestResolver(
  128. ClientRegistrationRepository clientRegistrationRepository) {
  129. DefaultOAuth2AuthorizationRequestResolver authorizationRequestResolver =
  130. new DefaultOAuth2AuthorizationRequestResolver(
  131. clientRegistrationRepository, "/oauth2/authorization");
  132. authorizationRequestResolver.setAuthorizationRequestCustomizer(
  133. authorizationRequestCustomizer());
  134. return authorizationRequestResolver;
  135. }
  136. private Consumer<OAuth2AuthorizationRequest.Builder> authorizationRequestCustomizer() {
  137. return customizer -> customizer
  138. .additionalParameters(params -> params.put("prompt", "consent"));
  139. }
  140. }
  141. ----
  142. Kotlin::
  143. +
  144. [source,kotlin,role="secondary"]
  145. ----
  146. @Configuration
  147. @EnableWebSecurity
  148. class SecurityConfig {
  149. @Autowired
  150. private lateinit var customClientRegistrationRepository: ClientRegistrationRepository
  151. @Bean
  152. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  153. http {
  154. authorizeRequests {
  155. authorize(anyRequest, authenticated)
  156. }
  157. oauth2Login {
  158. authorizationEndpoint {
  159. authorizationRequestResolver = authorizationRequestResolver(customClientRegistrationRepository)
  160. }
  161. }
  162. }
  163. return http.build()
  164. }
  165. private fun authorizationRequestResolver(
  166. clientRegistrationRepository: ClientRegistrationRepository?): OAuth2AuthorizationRequestResolver {
  167. val authorizationRequestResolver = DefaultOAuth2AuthorizationRequestResolver(
  168. clientRegistrationRepository, "/oauth2/authorization")
  169. authorizationRequestResolver.setAuthorizationRequestCustomizer(
  170. authorizationRequestCustomizer())
  171. return authorizationRequestResolver
  172. }
  173. private fun authorizationRequestCustomizer(): Consumer<OAuth2AuthorizationRequest.Builder> {
  174. return Consumer { customizer ->
  175. customizer
  176. .additionalParameters { params -> params["prompt"] = "consent" }
  177. }
  178. }
  179. }
  180. ----
  181. ======
  182. 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.
  183. For example, if the value for the request parameter `prompt` is always `consent` for the provider `okta`, you can configure it as follows:
  184. [source,yaml]
  185. ----
  186. spring:
  187. security:
  188. oauth2:
  189. client:
  190. provider:
  191. okta:
  192. authorization-uri: https://dev-1234.oktapreview.com/oauth2/v1/authorize?prompt=consent
  193. ----
  194. The preceding example shows the common use case of adding a custom parameter on top of the standard parameters.
  195. Alternatively, if your requirements are more advanced, you can take full control in building the Authorization Request URI by overriding the `OAuth2AuthorizationRequest.authorizationRequestUri` property.
  196. [TIP]
  197. ====
  198. `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.
  199. ====
  200. The following example shows a variation of `authorizationRequestCustomizer()` from the preceding example and instead overrides the `OAuth2AuthorizationRequest.authorizationRequestUri` property:
  201. [tabs]
  202. ======
  203. Java::
  204. +
  205. [source,java,role="primary"]
  206. ----
  207. private Consumer<OAuth2AuthorizationRequest.Builder> authorizationRequestCustomizer() {
  208. return customizer -> customizer
  209. .authorizationRequestUri(uriBuilder -> uriBuilder
  210. .queryParam("prompt", "consent").build());
  211. }
  212. ----
  213. Kotlin::
  214. +
  215. [source,kotlin,role="secondary"]
  216. ----
  217. private fun authorizationRequestCustomizer(): Consumer<OAuth2AuthorizationRequest.Builder> {
  218. return Consumer { customizer: OAuth2AuthorizationRequest.Builder ->
  219. customizer
  220. .authorizationRequestUri { uriBuilder: UriBuilder ->
  221. uriBuilder
  222. .queryParam("prompt", "consent").build()
  223. }
  224. }
  225. }
  226. ----
  227. ======
  228. [[oauth2-client-authorization-code-authorization-request-repository]]
  229. === Storing the Authorization Request
  230. 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).
  231. [TIP]
  232. ====
  233. The `OAuth2AuthorizationRequest` is used to correlate and validate the Authorization Response.
  234. ====
  235. The default implementation of `AuthorizationRequestRepository` is `HttpSessionOAuth2AuthorizationRequestRepository`, which stores the `OAuth2AuthorizationRequest` in the `HttpSession`.
  236. If you have a custom implementation of `AuthorizationRequestRepository`, you can configure it as follows:
  237. .AuthorizationRequestRepository Configuration
  238. [tabs]
  239. ======
  240. Java::
  241. +
  242. [source,java,role="primary"]
  243. ----
  244. @Configuration
  245. @EnableWebSecurity
  246. public class OAuth2ClientSecurityConfig {
  247. @Bean
  248. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  249. http
  250. .oauth2Client(oauth2 -> oauth2
  251. .authorizationCodeGrant(codeGrant -> codeGrant
  252. .authorizationRequestRepository(this.authorizationRequestRepository())
  253. // ...
  254. )
  255. )
  256. .oauth2Login(oauth2 -> oauth2
  257. .authorizationEndpoint(endpoint -> endpoint
  258. .authorizationRequestRepository(this.authorizationRequestRepository())
  259. // ...
  260. )
  261. );
  262. return http.build();
  263. }
  264. @Bean
  265. public AuthorizationRequestRepository<OAuth2AuthorizationRequest> authorizationRequestRepository() {
  266. return new CustomOAuth2AuthorizationRequestRepository();
  267. }
  268. }
  269. ----
  270. Kotlin::
  271. +
  272. [source,kotlin,role="secondary"]
  273. ----
  274. @Configuration
  275. @EnableWebSecurity
  276. class OAuth2ClientSecurityConfig {
  277. @Bean
  278. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  279. http {
  280. oauth2Client {
  281. authorizationCodeGrant {
  282. authorizationRequestRepository = authorizationRequestRepository()
  283. }
  284. }
  285. }
  286. return http.build()
  287. }
  288. }
  289. ----
  290. Xml::
  291. +
  292. [source,xml,role="secondary"]
  293. ----
  294. <http>
  295. <oauth2-client>
  296. <authorization-code-grant authorization-request-repository-ref="authorizationRequestRepository"/>
  297. </oauth2-client>
  298. </http>
  299. ----
  300. ======
  301. [[oauth2-client-authorization-code-access-token]]
  302. === Requesting an Access Token
  303. [NOTE]
  304. ====
  305. See the https://tools.ietf.org/html/rfc6749#section-4.1.3[Access Token Request/Response] protocol flow for the Authorization Code grant.
  306. ====
  307. There are two implementations of `OAuth2AccessTokenResponseClient` that can be used to make HTTP requests to the Token Endpoint in order to obtain an access token for the Authorization Code grant:
  308. * `DefaultAuthorizationCodeTokenResponseClient` (_default_)
  309. * `RestClientAuthorizationCodeTokenResponseClient`
  310. The default implementation uses a `RestOperations` instance to exchange an authorization code for an access token at the Authorization Server’s Token Endpoint.
  311. Spring Security 6.4 introduces a new implementation based on `RestClient`, which provides similar functionality but is better aligned with the Reactive version of the component (based on `WebClient`) in order to provide consistent configuration for applications on either stack.
  312. [NOTE]
  313. ====
  314. This section focuses on `RestClientAuthorizationCodeTokenResponseClient`.
  315. You can read about {spring-security-reference-base-url}/6.3/servlet/oauth2/client/authorization-grants.html#_requesting_an_access_token[`DefaultAuthorizationCodeTokenResponseClient`] in the Spring Security 6.3 documentation.
  316. ====
  317. :section-id: authorization-code
  318. :grant-type: Authorization Code
  319. :class-name: RestClientAuthorizationCodeTokenResponseClient
  320. :grant-request: OAuth2AuthorizationCodeGrantRequest
  321. :leveloffset: +1
  322. include::partial$servlet/oauth2/client/rest-client-access-token-response-client.adoc[]
  323. :leveloffset: -1
  324. [[oauth2-client-authorization-code-access-token-response-client-dsl]]
  325. === Customize using the DSL
  326. 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:
  327. .Access Token Response Configuration via DSL
  328. [tabs]
  329. ======
  330. Java::
  331. +
  332. [source,java,role="primary"]
  333. ----
  334. @Configuration
  335. @EnableWebSecurity
  336. public class OAuth2ClientSecurityConfig {
  337. @Bean
  338. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  339. http
  340. .oauth2Client(oauth2 -> oauth2
  341. .authorizationCodeGrant(codeGrant -> codeGrant
  342. .accessTokenResponseClient(this.accessTokenResponseClient())
  343. // ...
  344. )
  345. );
  346. return http.build();
  347. }
  348. }
  349. ----
  350. Kotlin::
  351. +
  352. [source,kotlin,role="secondary"]
  353. ----
  354. @Configuration
  355. @EnableWebSecurity
  356. class OAuth2ClientSecurityConfig {
  357. @Bean
  358. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  359. http {
  360. oauth2Client {
  361. authorizationCodeGrant {
  362. accessTokenResponseClient = accessTokenResponseClient()
  363. }
  364. }
  365. }
  366. return http.build()
  367. }
  368. }
  369. ----
  370. Xml::
  371. +
  372. [source,xml,role="secondary"]
  373. ----
  374. <http>
  375. <oauth2-client>
  376. <authorization-code-grant access-token-response-client-ref="accessTokenResponseClient"/>
  377. </oauth2-client>
  378. </http>
  379. ----
  380. ======
  381. [[oauth2-client-refresh-token]]
  382. == [[oauth2Client-refresh-token-grant]]Refresh Token
  383. [NOTE]
  384. ====
  385. See the OAuth 2.0 Authorization Framework for further details on the https://tools.ietf.org/html/rfc6749#section-1.5[Refresh Token].
  386. ====
  387. [[oauth2-client-refresh-token-access-token]]
  388. === Refreshing an Access Token
  389. [NOTE]
  390. ====
  391. See the https://tools.ietf.org/html/rfc6749#section-6[Access Token Request/Response] protocol flow for the Refresh Token grant.
  392. ====
  393. There are two implementations of `OAuth2AccessTokenResponseClient` that can be used to make HTTP requests to the Token Endpoint in order to obtain an access token for the Refresh Token grant:
  394. * `DefaultRefreshTokenTokenResponseClient` (_default_)
  395. * `RestClientRefreshTokenTokenResponseClient`
  396. The default implementation uses a `RestOperations` instance to exchange an authorization code for an access token at the Authorization Server’s Token Endpoint.
  397. Spring Security 6.4 introduces a new implementation based on `RestClient`, which provides similar functionality but is better aligned with the Reactive version of the component (based on `WebClient`) in order to provide consistent configuration for applications on either stack.
  398. [NOTE]
  399. ====
  400. This section focuses on `RestClientRefreshTokenTokenResponseClient`.
  401. You can read about {spring-security-reference-base-url}/6.3/servlet/oauth2/client/authorization-grants.html#_refreshing_an_access_token[`DefaultRefreshTokenTokenResponseClient`] in the Spring Security 6.3 documentation.
  402. ====
  403. :section-id: refresh-token
  404. :grant-type: Refresh Token
  405. :class-name: RestClientRefreshTokenTokenResponseClient
  406. :grant-request: OAuth2RefreshTokenGrantRequest
  407. :leveloffset: +1
  408. include::partial$servlet/oauth2/client/rest-client-access-token-response-client.adoc[]
  409. :leveloffset: -1
  410. [[oauth2-client-refresh-token-authorized-client-provider-builder]]
  411. === Customize using the Builder
  412. 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:
  413. .Access Token Response Configuration via Builder
  414. [tabs]
  415. ======
  416. Java::
  417. +
  418. [source,java,role="primary"]
  419. ----
  420. // Customize
  421. OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> refreshTokenTokenResponseClient = ...
  422. OAuth2AuthorizedClientProvider authorizedClientProvider =
  423. OAuth2AuthorizedClientProviderBuilder.builder()
  424. .authorizationCode()
  425. .refreshToken(configurer -> configurer.accessTokenResponseClient(refreshTokenTokenResponseClient))
  426. .build();
  427. // ...
  428. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  429. ----
  430. Kotlin::
  431. +
  432. [source,kotlin,role="secondary"]
  433. ----
  434. // Customize
  435. val refreshTokenTokenResponseClient: OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> = ...
  436. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  437. .authorizationCode()
  438. .refreshToken { it.accessTokenResponseClient(refreshTokenTokenResponseClient) }
  439. .build()
  440. // ...
  441. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  442. ----
  443. ======
  444. [NOTE]
  445. ====
  446. `OAuth2AuthorizedClientProviderBuilder.builder().refreshToken()` configures a `RefreshTokenOAuth2AuthorizedClientProvider`,
  447. which is an implementation of an `OAuth2AuthorizedClientProvider` for the Refresh Token grant.
  448. ====
  449. The `OAuth2RefreshToken` can optionally be returned in the Access Token Response for the `authorization_code` and `password` grant types.
  450. If the `OAuth2AuthorizedClient.getRefreshToken()` is available and the `OAuth2AuthorizedClient.getAccessToken()` is expired, it is automatically refreshed by the `RefreshTokenOAuth2AuthorizedClientProvider`.
  451. [[oauth2-client-client-credentials]]
  452. == [[oauth2Client-client-creds-grant]]Client Credentials
  453. [NOTE]
  454. ====
  455. 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.
  456. ====
  457. [[oauth2-client-client-credentials-access-token]]
  458. === Requesting an Access Token
  459. [NOTE]
  460. ====
  461. See the https://tools.ietf.org/html/rfc6749#section-4.4.2[Access Token Request/Response] protocol flow for the Client Credentials grant.
  462. ====
  463. There are two implementations of `OAuth2AccessTokenResponseClient` that can be used to make HTTP requests to the Token Endpoint in order to obtain an access token for the Client Credentials grant:
  464. * `DefaultClientCredentialsTokenResponseClient` (_default_)
  465. * `RestClientClientCredentialsTokenResponseClient`
  466. The default implementation uses a `RestOperations` instance to exchange an authorization code for an access token at the Authorization Server’s Token Endpoint.
  467. Spring Security 6.4 introduces a new implementation based on `RestClient`, which provides similar functionality but is better aligned with the Reactive version of the component (based on `WebClient`) in order to provide consistent configuration for applications on either stack.
  468. [NOTE]
  469. ====
  470. This section focuses on `RestClientClientCredentialsTokenResponseClient`.
  471. You can read about {spring-security-reference-base-url}/6.3/servlet/oauth2/client/authorization-grants.html#_requesting_an_access_token_2[`DefaultClientCredentialsTokenResponseClient`] in the Spring Security 6.3 documentation.
  472. ====
  473. :section-id: client-credentials
  474. :grant-type: Client Credentials
  475. :class-name: RestClientClientCredentialsTokenResponseClient
  476. :grant-request: OAuth2ClientCredentialsGrantRequest
  477. :leveloffset: +1
  478. include::partial$servlet/oauth2/client/rest-client-access-token-response-client.adoc[]
  479. :leveloffset: -1
  480. [[oauth2-client-client-credentials-authorized-client-provider-builder]]
  481. === Customize using the Builder
  482. 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:
  483. .Access Token Response Configuration via Builder
  484. [tabs]
  485. ======
  486. Java::
  487. +
  488. [source,java,role="primary"]
  489. ----
  490. // Customize
  491. OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsTokenResponseClient = ...
  492. OAuth2AuthorizedClientProvider authorizedClientProvider =
  493. OAuth2AuthorizedClientProviderBuilder.builder()
  494. .clientCredentials(configurer -> configurer.accessTokenResponseClient(clientCredentialsTokenResponseClient))
  495. .build();
  496. // ...
  497. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  498. ----
  499. Kotlin::
  500. +
  501. [source,kotlin,role="secondary"]
  502. ----
  503. // Customize
  504. val clientCredentialsTokenResponseClient: OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> = ...
  505. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  506. .clientCredentials { it.accessTokenResponseClient(clientCredentialsTokenResponseClient) }
  507. .build()
  508. // ...
  509. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  510. ----
  511. ======
  512. [NOTE]
  513. ====
  514. `OAuth2AuthorizedClientProviderBuilder.builder().clientCredentials()` configures a `ClientCredentialsOAuth2AuthorizedClientProvider`,
  515. which is an implementation of an `OAuth2AuthorizedClientProvider` for the Client Credentials grant.
  516. ====
  517. [[oauth2-client-client-credentials-authorized-client-manager]]
  518. === Using the Access Token
  519. Consider the following Spring Boot properties for an OAuth 2.0 Client registration:
  520. [source,yaml]
  521. ----
  522. spring:
  523. security:
  524. oauth2:
  525. client:
  526. registration:
  527. okta:
  528. client-id: okta-client-id
  529. client-secret: okta-client-secret
  530. authorization-grant-type: client_credentials
  531. scope: read, write
  532. provider:
  533. okta:
  534. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  535. ----
  536. Further consider the following `OAuth2AuthorizedClientManager` `@Bean`:
  537. [tabs]
  538. ======
  539. Java::
  540. +
  541. [source,java,role="primary"]
  542. ----
  543. @Bean
  544. public OAuth2AuthorizedClientManager authorizedClientManager(
  545. ClientRegistrationRepository clientRegistrationRepository,
  546. OAuth2AuthorizedClientRepository authorizedClientRepository) {
  547. OAuth2AuthorizedClientProvider authorizedClientProvider =
  548. OAuth2AuthorizedClientProviderBuilder.builder()
  549. .clientCredentials()
  550. .build();
  551. DefaultOAuth2AuthorizedClientManager authorizedClientManager =
  552. new DefaultOAuth2AuthorizedClientManager(
  553. clientRegistrationRepository, authorizedClientRepository);
  554. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  555. return authorizedClientManager;
  556. }
  557. ----
  558. Kotlin::
  559. +
  560. [source,kotlin,role="secondary"]
  561. ----
  562. @Bean
  563. fun authorizedClientManager(
  564. clientRegistrationRepository: ClientRegistrationRepository,
  565. authorizedClientRepository: OAuth2AuthorizedClientRepository): OAuth2AuthorizedClientManager {
  566. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  567. .clientCredentials()
  568. .build()
  569. val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
  570. clientRegistrationRepository, authorizedClientRepository)
  571. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  572. return authorizedClientManager
  573. }
  574. ----
  575. ======
  576. Given the preceding properties and bean, you can obtain the `OAuth2AccessToken` as follows:
  577. [tabs]
  578. ======
  579. Java::
  580. +
  581. [source,java,role="primary"]
  582. ----
  583. @Controller
  584. public class OAuth2ClientController {
  585. @Autowired
  586. private OAuth2AuthorizedClientManager authorizedClientManager;
  587. @GetMapping("/")
  588. public String index(Authentication authentication,
  589. HttpServletRequest servletRequest,
  590. HttpServletResponse servletResponse) {
  591. OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  592. .principal(authentication)
  593. .attributes(attrs -> {
  594. attrs.put(HttpServletRequest.class.getName(), servletRequest);
  595. attrs.put(HttpServletResponse.class.getName(), servletResponse);
  596. })
  597. .build();
  598. OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest);
  599. OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
  600. // ...
  601. return "index";
  602. }
  603. }
  604. ----
  605. Kotlin::
  606. +
  607. [source,kotlin,role="secondary"]
  608. ----
  609. class OAuth2ClientController {
  610. @Autowired
  611. private lateinit var authorizedClientManager: OAuth2AuthorizedClientManager
  612. @GetMapping("/")
  613. fun index(authentication: Authentication?,
  614. servletRequest: HttpServletRequest,
  615. servletResponse: HttpServletResponse): String {
  616. val authorizeRequest: OAuth2AuthorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  617. .principal(authentication)
  618. .attributes(Consumer { attrs: MutableMap<String, Any> ->
  619. attrs[HttpServletRequest::class.java.name] = servletRequest
  620. attrs[HttpServletResponse::class.java.name] = servletResponse
  621. })
  622. .build()
  623. val authorizedClient = authorizedClientManager.authorize(authorizeRequest)
  624. val accessToken: OAuth2AccessToken = authorizedClient.accessToken
  625. // ...
  626. return "index"
  627. }
  628. }
  629. ----
  630. ======
  631. [NOTE]
  632. ====
  633. `HttpServletRequest` and `HttpServletResponse` are both OPTIONAL attributes.
  634. If not provided, they default to `ServletRequestAttributes` by using `RequestContextHolder.getRequestAttributes()`.
  635. ====
  636. [[oauth2-client-password]]
  637. == [[oauth2Client-password-grant]]Resource Owner Password Credentials
  638. [NOTE]
  639. ====
  640. See 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.
  641. ====
  642. [[oauth2-client-password-access-token]]
  643. === Requesting an Access Token
  644. [NOTE]
  645. ====
  646. See the https://tools.ietf.org/html/rfc6749#section-4.3.2[Access Token Request/Response] protocol flow for the Resource Owner Password Credentials grant.
  647. ====
  648. The default implementation of `OAuth2AccessTokenResponseClient` for the Resource Owner Password Credentials grant is `DefaultPasswordTokenResponseClient`, which uses a `RestOperations` when requesting an access token at the Authorization Server’s Token Endpoint.
  649. [CAUTION]
  650. ====
  651. The `DefaultPasswordTokenResponseClient` class and support for the Resource Owner Password Credentials grant are deprecated.
  652. This section will be removed in Spring Security 7.
  653. ====
  654. The `DefaultPasswordTokenResponseClient` is flexible, as it lets you customize the pre-processing of the Token Request or post-handling of the Token Response.
  655. [[oauth2-client-password-access-token-request]]
  656. === Customizing the Access Token Request
  657. If you need to customize the pre-processing of the Token Request, you can provide `DefaultPasswordTokenResponseClient.setRequestEntityConverter()` with a custom `Converter<OAuth2PasswordGrantRequest, RequestEntity<?>>`.
  658. The default implementation (`OAuth2PasswordGrantRequestEntityConverter`) builds a `RequestEntity` representation of a standard https://tools.ietf.org/html/rfc6749#section-4.3.2[OAuth 2.0 Access Token Request].
  659. However, providing a custom `Converter` would let you extend the standard Token Request and add custom parameter(s).
  660. To customize only the parameters of the request, you can provide `OAuth2PasswordGrantRequestEntityConverter.setParametersConverter()` with a custom `Converter<OAuth2PasswordGrantRequest, MultiValueMap<String, String>>` to completely override the parameters sent with the request. This is often simpler than constructing a `RequestEntity` directly.
  661. [TIP]
  662. ====
  663. If you prefer to only add additional parameters, you can provide `OAuth2PasswordGrantRequestEntityConverter.addParametersConverter()` with a custom `Converter<OAuth2PasswordGrantRequest, MultiValueMap<String, String>>` which constructs an aggregate `Converter`.
  664. ====
  665. [IMPORTANT]
  666. ====
  667. The custom `Converter` must return a valid `RequestEntity` representation of an OAuth 2.0 Access Token Request that is understood by the intended OAuth 2.0 Provider.
  668. ====
  669. [[oauth2-client-password-access-token-response]]
  670. === Customizing the Access Token Response
  671. On the other end, if you need to customize the post-handling of the Token Response, you need to provide `DefaultPasswordTokenResponseClient.setRestOperations()` with a custom configured `RestOperations`.
  672. The default `RestOperations` is configured as follows:
  673. [tabs]
  674. ======
  675. Java::
  676. +
  677. [source,java,role="primary"]
  678. ----
  679. RestTemplate restTemplate = new RestTemplate(Arrays.asList(
  680. new FormHttpMessageConverter(),
  681. new OAuth2AccessTokenResponseHttpMessageConverter()));
  682. restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
  683. ----
  684. Kotlin::
  685. +
  686. [source,kotlin,role="secondary"]
  687. ----
  688. val restTemplate = RestTemplate(listOf(
  689. FormHttpMessageConverter(),
  690. OAuth2AccessTokenResponseHttpMessageConverter()))
  691. restTemplate.errorHandler = OAuth2ErrorResponseErrorHandler()
  692. ----
  693. ======
  694. [TIP]
  695. ====
  696. Spring MVC `FormHttpMessageConverter` is required, as it is used when sending the OAuth 2.0 Access Token Request.
  697. ====
  698. `OAuth2AccessTokenResponseHttpMessageConverter` is a `HttpMessageConverter` for an OAuth 2.0 Access Token Response.
  699. You can provide `OAuth2AccessTokenResponseHttpMessageConverter.setTokenResponseConverter()` with a custom `Converter<Map<String, String>, OAuth2AccessTokenResponse>` that is used to convert the OAuth 2.0 Access Token Response parameters to an `OAuth2AccessTokenResponse`.
  700. `OAuth2ErrorResponseErrorHandler` is a `ResponseErrorHandler` that can handle an OAuth 2.0 Error, such as `400 Bad Request`.
  701. It uses an `OAuth2ErrorHttpMessageConverter` to convert the OAuth 2.0 Error parameters to an `OAuth2Error`.
  702. [[oauth2-client-password-authorized-client-provider-builder]]
  703. === Customize using the Builder
  704. Whether you customize `DefaultPasswordTokenResponseClient` or provide your own implementation of `OAuth2AccessTokenResponseClient`, you need to configure it as follows:
  705. .Access Token Response Configuration via Builder
  706. [tabs]
  707. ======
  708. Java::
  709. +
  710. [source,java,role="primary"]
  711. ----
  712. // Customize
  713. OAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> passwordTokenResponseClient = ...
  714. OAuth2AuthorizedClientProvider authorizedClientProvider =
  715. OAuth2AuthorizedClientProviderBuilder.builder()
  716. .password(configurer -> configurer.accessTokenResponseClient(passwordTokenResponseClient))
  717. .refreshToken()
  718. .build();
  719. // ...
  720. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  721. ----
  722. Kotlin::
  723. +
  724. [source,kotlin,role="secondary"]
  725. ----
  726. val passwordTokenResponseClient: OAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> = ...
  727. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  728. .password { it.accessTokenResponseClient(passwordTokenResponseClient) }
  729. .refreshToken()
  730. .build()
  731. // ...
  732. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  733. ----
  734. ======
  735. [NOTE]
  736. ====
  737. `OAuth2AuthorizedClientProviderBuilder.builder().password()` configures a `PasswordOAuth2AuthorizedClientProvider`,
  738. which is an implementation of an `OAuth2AuthorizedClientProvider` for the Resource Owner Password Credentials grant.
  739. ====
  740. [[oauth2-client-password-authorized-client-manager]]
  741. === Using the Access Token
  742. Consider the following Spring Boot properties for an OAuth 2.0 Client registration:
  743. [source,yaml]
  744. ----
  745. spring:
  746. security:
  747. oauth2:
  748. client:
  749. registration:
  750. okta:
  751. client-id: okta-client-id
  752. client-secret: okta-client-secret
  753. authorization-grant-type: password
  754. scope: read, write
  755. provider:
  756. okta:
  757. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  758. ----
  759. Further consider the `OAuth2AuthorizedClientManager` `@Bean`:
  760. [tabs]
  761. ======
  762. Java::
  763. +
  764. [source,java,role="primary"]
  765. ----
  766. @Bean
  767. public OAuth2AuthorizedClientManager authorizedClientManager(
  768. ClientRegistrationRepository clientRegistrationRepository,
  769. OAuth2AuthorizedClientRepository authorizedClientRepository) {
  770. OAuth2AuthorizedClientProvider authorizedClientProvider =
  771. OAuth2AuthorizedClientProviderBuilder.builder()
  772. .password()
  773. .refreshToken()
  774. .build();
  775. DefaultOAuth2AuthorizedClientManager authorizedClientManager =
  776. new DefaultOAuth2AuthorizedClientManager(
  777. clientRegistrationRepository, authorizedClientRepository);
  778. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  779. // Assuming the `username` and `password` are supplied as `HttpServletRequest` parameters,
  780. // map the `HttpServletRequest` parameters to `OAuth2AuthorizationContext.getAttributes()`
  781. authorizedClientManager.setContextAttributesMapper(contextAttributesMapper());
  782. return authorizedClientManager;
  783. }
  784. private Function<OAuth2AuthorizeRequest, Map<String, Object>> contextAttributesMapper() {
  785. return authorizeRequest -> {
  786. Map<String, Object> contextAttributes = Collections.emptyMap();
  787. HttpServletRequest servletRequest = authorizeRequest.getAttribute(HttpServletRequest.class.getName());
  788. String username = servletRequest.getParameter(OAuth2ParameterNames.USERNAME);
  789. String password = servletRequest.getParameter(OAuth2ParameterNames.PASSWORD);
  790. if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
  791. contextAttributes = new HashMap<>();
  792. // `PasswordOAuth2AuthorizedClientProvider` requires both attributes
  793. contextAttributes.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username);
  794. contextAttributes.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password);
  795. }
  796. return contextAttributes;
  797. };
  798. }
  799. ----
  800. Kotlin::
  801. +
  802. [source,kotlin,role="secondary"]
  803. ----
  804. @Bean
  805. fun authorizedClientManager(
  806. clientRegistrationRepository: ClientRegistrationRepository,
  807. authorizedClientRepository: OAuth2AuthorizedClientRepository): OAuth2AuthorizedClientManager {
  808. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  809. .password()
  810. .refreshToken()
  811. .build()
  812. val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
  813. clientRegistrationRepository, authorizedClientRepository)
  814. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  815. // Assuming the `username` and `password` are supplied as `HttpServletRequest` parameters,
  816. // map the `HttpServletRequest` parameters to `OAuth2AuthorizationContext.getAttributes()`
  817. authorizedClientManager.setContextAttributesMapper(contextAttributesMapper())
  818. return authorizedClientManager
  819. }
  820. private fun contextAttributesMapper(): Function<OAuth2AuthorizeRequest, MutableMap<String, Any>> {
  821. return Function { authorizeRequest ->
  822. var contextAttributes: MutableMap<String, Any> = mutableMapOf()
  823. val servletRequest: HttpServletRequest = authorizeRequest.getAttribute(HttpServletRequest::class.java.name)
  824. val username = servletRequest.getParameter(OAuth2ParameterNames.USERNAME)
  825. val password = servletRequest.getParameter(OAuth2ParameterNames.PASSWORD)
  826. if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
  827. contextAttributes = hashMapOf()
  828. // `PasswordOAuth2AuthorizedClientProvider` requires both attributes
  829. contextAttributes[OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME] = username
  830. contextAttributes[OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME] = password
  831. }
  832. contextAttributes
  833. }
  834. }
  835. ----
  836. ======
  837. Given the preceding properties and bean, you can obtain the `OAuth2AccessToken` as follows:
  838. [tabs]
  839. ======
  840. Java::
  841. +
  842. [source,java,role="primary"]
  843. ----
  844. @Controller
  845. public class OAuth2ClientController {
  846. @Autowired
  847. private OAuth2AuthorizedClientManager authorizedClientManager;
  848. @GetMapping("/")
  849. public String index(Authentication authentication,
  850. HttpServletRequest servletRequest,
  851. HttpServletResponse servletResponse) {
  852. OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  853. .principal(authentication)
  854. .attributes(attrs -> {
  855. attrs.put(HttpServletRequest.class.getName(), servletRequest);
  856. attrs.put(HttpServletResponse.class.getName(), servletResponse);
  857. })
  858. .build();
  859. OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest);
  860. OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
  861. // ...
  862. return "index";
  863. }
  864. }
  865. ----
  866. Kotlin::
  867. +
  868. [source,kotlin,role="secondary"]
  869. ----
  870. @Controller
  871. class OAuth2ClientController {
  872. @Autowired
  873. private lateinit var authorizedClientManager: OAuth2AuthorizedClientManager
  874. @GetMapping("/")
  875. fun index(authentication: Authentication?,
  876. servletRequest: HttpServletRequest,
  877. servletResponse: HttpServletResponse): String {
  878. val authorizeRequest: OAuth2AuthorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  879. .principal(authentication)
  880. .attributes(Consumer {
  881. it[HttpServletRequest::class.java.name] = servletRequest
  882. it[HttpServletResponse::class.java.name] = servletResponse
  883. })
  884. .build()
  885. val authorizedClient = authorizedClientManager.authorize(authorizeRequest)
  886. val accessToken: OAuth2AccessToken = authorizedClient.accessToken
  887. // ...
  888. return "index"
  889. }
  890. }
  891. ----
  892. ======
  893. [NOTE]
  894. ====
  895. `HttpServletRequest` and `HttpServletResponse` are both OPTIONAL attributes.
  896. If not provided, they default to `ServletRequestAttributes` using `RequestContextHolder.getRequestAttributes()`.
  897. ====
  898. [[oauth2-client-jwt-bearer]]
  899. == [[oauth2Client-jwt-bearer-grant]]JWT Bearer
  900. [NOTE]
  901. ====
  902. 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.
  903. ====
  904. [[oauth2-client-jwt-bearer-access-token]]
  905. === Requesting an Access Token
  906. [NOTE]
  907. ====
  908. 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.
  909. ====
  910. There are two implementations of `OAuth2AccessTokenResponseClient` that can be used to make HTTP requests to the Token Endpoint in order to obtain an access token for the JWT Bearer grant:
  911. * `DefaultJwtBearerTokenResponseClient` (_default_)
  912. * `RestClientJwtBearerTokenResponseClient`
  913. The default implementation uses a `RestOperations` instance to exchange an authorization code for an access token at the Authorization Server’s Token Endpoint.
  914. Spring Security 6.4 introduces a new implementation based on `RestClient`, which provides similar functionality but is better aligned with the Reactive version of the component (based on `WebClient`) in order to provide consistent configuration for applications on either stack.
  915. [NOTE]
  916. ====
  917. This section focuses on `RestClientJwtBearerTokenResponseClient`.
  918. You can read about {spring-security-reference-base-url}/6.3/servlet/oauth2/client/authorization-grants.html#_requesting_an_access_token_4[`DefaultClientCredentialsTokenResponseClient`] in the Spring Security 6.3 documentation.
  919. ====
  920. :section-id: jwt-bearer
  921. :grant-type: JWT Bearer
  922. :class-name: RestClientJwtBearerTokenResponseClient
  923. :grant-request: JwtBearerGrantRequest
  924. :leveloffset: +1
  925. include::partial$servlet/oauth2/client/rest-client-access-token-response-client.adoc[]
  926. :leveloffset: -1
  927. [[oauth2-client-jwt-bearer-authorized-client-provider-builder]]
  928. === Customize using the Builder
  929. 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:
  930. .Access Token Response Configuration via Builder
  931. [tabs]
  932. ======
  933. Java::
  934. +
  935. [source,java,role="primary"]
  936. ----
  937. // Customize
  938. OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> jwtBearerTokenResponseClient = ...
  939. JwtBearerOAuth2AuthorizedClientProvider jwtBearerAuthorizedClientProvider = new JwtBearerOAuth2AuthorizedClientProvider();
  940. jwtBearerAuthorizedClientProvider.setAccessTokenResponseClient(jwtBearerTokenResponseClient);
  941. OAuth2AuthorizedClientProvider authorizedClientProvider =
  942. OAuth2AuthorizedClientProviderBuilder.builder()
  943. .provider(jwtBearerAuthorizedClientProvider)
  944. .build();
  945. // ...
  946. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  947. ----
  948. Kotlin::
  949. +
  950. [source,kotlin,role="secondary"]
  951. ----
  952. // Customize
  953. val jwtBearerTokenResponseClient: OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> = ...
  954. val jwtBearerAuthorizedClientProvider = JwtBearerOAuth2AuthorizedClientProvider()
  955. jwtBearerAuthorizedClientProvider.setAccessTokenResponseClient(jwtBearerTokenResponseClient)
  956. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  957. .provider(jwtBearerAuthorizedClientProvider)
  958. .build()
  959. // ...
  960. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  961. ----
  962. ======
  963. [[oauth2-client-jwt-bearer-authorized-client-manager]]
  964. === Using the Access Token
  965. Given the following Spring Boot properties for an OAuth 2.0 Client registration:
  966. [source,yaml]
  967. ----
  968. spring:
  969. security:
  970. oauth2:
  971. client:
  972. registration:
  973. okta:
  974. client-id: okta-client-id
  975. client-secret: okta-client-secret
  976. authorization-grant-type: urn:ietf:params:oauth:grant-type:jwt-bearer
  977. scope: read
  978. provider:
  979. okta:
  980. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  981. ----
  982. ...and the `OAuth2AuthorizedClientManager` `@Bean`:
  983. [tabs]
  984. ======
  985. Java::
  986. +
  987. [source,java,role="primary"]
  988. ----
  989. @Bean
  990. public OAuth2AuthorizedClientManager authorizedClientManager(
  991. ClientRegistrationRepository clientRegistrationRepository,
  992. OAuth2AuthorizedClientRepository authorizedClientRepository) {
  993. JwtBearerOAuth2AuthorizedClientProvider jwtBearerAuthorizedClientProvider =
  994. new JwtBearerOAuth2AuthorizedClientProvider();
  995. OAuth2AuthorizedClientProvider authorizedClientProvider =
  996. OAuth2AuthorizedClientProviderBuilder.builder()
  997. .provider(jwtBearerAuthorizedClientProvider)
  998. .build();
  999. DefaultOAuth2AuthorizedClientManager authorizedClientManager =
  1000. new DefaultOAuth2AuthorizedClientManager(
  1001. clientRegistrationRepository, authorizedClientRepository);
  1002. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  1003. return authorizedClientManager;
  1004. }
  1005. ----
  1006. Kotlin::
  1007. +
  1008. [source,kotlin,role="secondary"]
  1009. ----
  1010. @Bean
  1011. fun authorizedClientManager(
  1012. clientRegistrationRepository: ClientRegistrationRepository,
  1013. authorizedClientRepository: OAuth2AuthorizedClientRepository): OAuth2AuthorizedClientManager {
  1014. val jwtBearerAuthorizedClientProvider = JwtBearerOAuth2AuthorizedClientProvider()
  1015. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  1016. .provider(jwtBearerAuthorizedClientProvider)
  1017. .build()
  1018. val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
  1019. clientRegistrationRepository, authorizedClientRepository)
  1020. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  1021. return authorizedClientManager
  1022. }
  1023. ----
  1024. ======
  1025. You may obtain the `OAuth2AccessToken` as follows:
  1026. [tabs]
  1027. ======
  1028. Java::
  1029. +
  1030. [source,java,role="primary"]
  1031. ----
  1032. @RestController
  1033. public class OAuth2ResourceServerController {
  1034. @Autowired
  1035. private OAuth2AuthorizedClientManager authorizedClientManager;
  1036. @GetMapping("/resource")
  1037. public String resource(JwtAuthenticationToken jwtAuthentication) {
  1038. OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  1039. .principal(jwtAuthentication)
  1040. .build();
  1041. OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest);
  1042. OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
  1043. // ...
  1044. }
  1045. }
  1046. ----
  1047. Kotlin::
  1048. +
  1049. [source,kotlin,role="secondary"]
  1050. ----
  1051. class OAuth2ResourceServerController {
  1052. @Autowired
  1053. private lateinit var authorizedClientManager: OAuth2AuthorizedClientManager
  1054. @GetMapping("/resource")
  1055. fun resource(jwtAuthentication: JwtAuthenticationToken?): String {
  1056. val authorizeRequest: OAuth2AuthorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  1057. .principal(jwtAuthentication)
  1058. .build()
  1059. val authorizedClient = authorizedClientManager.authorize(authorizeRequest)
  1060. val accessToken: OAuth2AccessToken = authorizedClient.accessToken
  1061. // ...
  1062. }
  1063. }
  1064. ----
  1065. ======
  1066. [NOTE]
  1067. ====
  1068. `JwtBearerOAuth2AuthorizedClientProvider` resolves the `Jwt` assertion via `OAuth2AuthorizationContext.getPrincipal().getPrincipal()` by default, hence the use of `JwtAuthenticationToken` in the preceding example.
  1069. ====
  1070. [TIP]
  1071. ====
  1072. If you need to resolve the `Jwt` assertion from a different source, you can provide `JwtBearerOAuth2AuthorizedClientProvider.setJwtAssertionResolver()` with a custom `Function<OAuth2AuthorizationContext, Jwt>`.
  1073. ====
  1074. [[oauth2-client-token-exchange]]
  1075. == [[oauth2Client-token-exchange-grant]]Token Exchange
  1076. [NOTE]
  1077. ====
  1078. Please refer to OAuth 2.0 Token Exchange for further details on the https://datatracker.ietf.org/doc/html/rfc8693[Token Exchange] grant.
  1079. ====
  1080. [[oauth2-client-token-exchange-access-token]]
  1081. === Requesting an Access Token
  1082. [NOTE]
  1083. ====
  1084. 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.
  1085. ====
  1086. There are two implementations of `OAuth2AccessTokenResponseClient` that can be used to make HTTP requests to the Token Endpoint in order to obtain an access token for the Token Exchange grant:
  1087. * `DefaultTokenExchangeTokenResponseClient` (_default_)
  1088. * `RestClientTokenExchangeTokenResponseClient`
  1089. The default implementation uses a `RestOperations` instance to exchange an authorization code for an access token at the Authorization Server’s Token Endpoint.
  1090. Spring Security 6.4 introduces a new implementation based on `RestClient`, which provides similar functionality but is better aligned with the Reactive version of the component (based on `WebClient`) in order to provide consistent configuration for applications on either stack.
  1091. [NOTE]
  1092. ====
  1093. This section focuses on `RestClientTokenExchangeTokenResponseClient`.
  1094. You can read about {spring-security-reference-base-url}/6.3/servlet/oauth2/client/authorization-grants.html#_requesting_an_access_token_5[`DefaultTokenExchangeTokenResponseClient`] in the Spring Security 6.3 documentation.
  1095. ====
  1096. :section-id: token-exchange
  1097. :grant-type: Token Exchange
  1098. :class-name: RestClientTokenExchangeTokenResponseClient
  1099. :grant-request: TokenExchangeGrantRequest
  1100. :leveloffset: +1
  1101. include::partial$servlet/oauth2/client/rest-client-access-token-response-client.adoc[]
  1102. :leveloffset: -1
  1103. [[oauth2-client-token-exchange-authorized-client-provider-builder]]
  1104. === Customize using the Builder
  1105. 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:
  1106. .Access Token Response Configuration via Builder
  1107. [tabs]
  1108. ======
  1109. Java::
  1110. +
  1111. [source,java,role="primary"]
  1112. ----
  1113. // Customize
  1114. OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> tokenExchangeTokenResponseClient = ...
  1115. TokenExchangeOAuth2AuthorizedClientProvider tokenExchangeAuthorizedClientProvider = new TokenExchangeOAuth2AuthorizedClientProvider();
  1116. tokenExchangeAuthorizedClientProvider.setAccessTokenResponseClient(tokenExchangeTokenResponseClient);
  1117. OAuth2AuthorizedClientProvider authorizedClientProvider =
  1118. OAuth2AuthorizedClientProviderBuilder.builder()
  1119. .provider(tokenExchangeAuthorizedClientProvider)
  1120. .build();
  1121. // ...
  1122. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  1123. ----
  1124. Kotlin::
  1125. +
  1126. [source,kotlin,role="secondary"]
  1127. ----
  1128. // Customize
  1129. val tokenExchangeTokenResponseClient: OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> = ...
  1130. val tokenExchangeAuthorizedClientProvider = TokenExchangeOAuth2AuthorizedClientProvider()
  1131. tokenExchangeAuthorizedClientProvider.setAccessTokenResponseClient(tokenExchangeTokenResponseClient)
  1132. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  1133. .provider(tokenExchangeAuthorizedClientProvider)
  1134. .build()
  1135. // ...
  1136. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  1137. ----
  1138. ======
  1139. [[oauth2-client-token-exchange-authorized-client-manager]]
  1140. === [[token-exchange-grant-access-token]]Using the Access Token
  1141. Given the following Spring Boot properties for an OAuth 2.0 Client registration:
  1142. [source,yaml]
  1143. ----
  1144. spring:
  1145. security:
  1146. oauth2:
  1147. client:
  1148. registration:
  1149. okta:
  1150. client-id: okta-client-id
  1151. client-secret: okta-client-secret
  1152. authorization-grant-type: urn:ietf:params:oauth:grant-type:token-exchange
  1153. scope: read
  1154. provider:
  1155. okta:
  1156. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  1157. ----
  1158. ...and the `OAuth2AuthorizedClientManager` `@Bean`:
  1159. [tabs]
  1160. ======
  1161. Java::
  1162. +
  1163. [source,java,role="primary"]
  1164. ----
  1165. @Bean
  1166. public OAuth2AuthorizedClientManager authorizedClientManager(
  1167. ClientRegistrationRepository clientRegistrationRepository,
  1168. OAuth2AuthorizedClientRepository authorizedClientRepository) {
  1169. TokenExchangeOAuth2AuthorizedClientProvider tokenExchangeAuthorizedClientProvider =
  1170. new TokenExchangeOAuth2AuthorizedClientProvider();
  1171. OAuth2AuthorizedClientProvider authorizedClientProvider =
  1172. OAuth2AuthorizedClientProviderBuilder.builder()
  1173. .provider(tokenExchangeAuthorizedClientProvider)
  1174. .build();
  1175. DefaultOAuth2AuthorizedClientManager authorizedClientManager =
  1176. new DefaultOAuth2AuthorizedClientManager(
  1177. clientRegistrationRepository, authorizedClientRepository);
  1178. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  1179. return authorizedClientManager;
  1180. }
  1181. ----
  1182. Kotlin::
  1183. +
  1184. [source,kotlin,role="secondary"]
  1185. ----
  1186. @Bean
  1187. fun authorizedClientManager(
  1188. clientRegistrationRepository: ClientRegistrationRepository,
  1189. authorizedClientRepository: OAuth2AuthorizedClientRepository): OAuth2AuthorizedClientManager {
  1190. val tokenExchangeAuthorizedClientProvider = TokenExchangeOAuth2AuthorizedClientProvider()
  1191. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  1192. .provider(tokenExchangeAuthorizedClientProvider)
  1193. .build()
  1194. val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
  1195. clientRegistrationRepository, authorizedClientRepository)
  1196. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  1197. return authorizedClientManager
  1198. }
  1199. ----
  1200. ======
  1201. You may obtain the `OAuth2AccessToken` as follows:
  1202. [tabs]
  1203. ======
  1204. Java::
  1205. +
  1206. [source,java,role="primary"]
  1207. ----
  1208. @RestController
  1209. public class OAuth2ResourceServerController {
  1210. @Autowired
  1211. private OAuth2AuthorizedClientManager authorizedClientManager;
  1212. @GetMapping("/resource")
  1213. public String resource(JwtAuthenticationToken jwtAuthentication) {
  1214. OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  1215. .principal(jwtAuthentication)
  1216. .build();
  1217. OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest);
  1218. OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
  1219. // ...
  1220. }
  1221. }
  1222. ----
  1223. Kotlin::
  1224. +
  1225. [source,kotlin,role="secondary"]
  1226. ----
  1227. class OAuth2ResourceServerController {
  1228. @Autowired
  1229. private lateinit var authorizedClientManager: OAuth2AuthorizedClientManager
  1230. @GetMapping("/resource")
  1231. fun resource(jwtAuthentication: JwtAuthenticationToken?): String {
  1232. val authorizeRequest: OAuth2AuthorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  1233. .principal(jwtAuthentication)
  1234. .build()
  1235. val authorizedClient = authorizedClientManager.authorize(authorizeRequest)
  1236. val accessToken: OAuth2AccessToken = authorizedClient.accessToken
  1237. // ...
  1238. }
  1239. }
  1240. ----
  1241. ======
  1242. [NOTE]
  1243. ====
  1244. `TokenExchangeOAuth2AuthorizedClientProvider` resolves the subject token (as an `OAuth2Token`) via `OAuth2AuthorizationContext.getPrincipal().getPrincipal()` by default, hence the use of `JwtAuthenticationToken` in the preceding example.
  1245. An actor token is not resolved by default.
  1246. ====
  1247. [TIP]
  1248. ====
  1249. If you need to resolve the subject token from a different source, you can provide `TokenExchangeOAuth2AuthorizedClientProvider.setSubjectTokenResolver()` with a custom `Function<OAuth2AuthorizationContext, OAuth2Token>`.
  1250. ====
  1251. [TIP]
  1252. ====
  1253. If you need to resolve an actor token, you can provide `TokenExchangeOAuth2AuthorizedClientProvider.setActorTokenResolver()` with a custom `Function<OAuth2AuthorizationContext, OAuth2Token>`.
  1254. ====