authorization-grants.adoc 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. [[oauth2Client-auth-grant-support]]
  2. = Authorization Grant Support
  3. [[oauth2Client-auth-code-grant]]
  4. == Authorization Code
  5. [NOTE]
  6. 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.
  7. === Obtaining Authorization
  8. [NOTE]
  9. Please refer to the https://tools.ietf.org/html/rfc6749#section-4.1.1[Authorization Request/Response] protocol flow for the Authorization Code grant.
  10. === Initiating the Authorization Request
  11. 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.
  12. The primary role of the `OAuth2AuthorizationRequestResolver` is to resolve an `OAuth2AuthorizationRequest` from the provided web request.
  13. 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`.
  14. Given the following Spring Boot 2.x properties for an OAuth 2.0 Client registration:
  15. [source,yaml,attrs="-attributes"]
  16. ----
  17. spring:
  18. security:
  19. oauth2:
  20. client:
  21. registration:
  22. okta:
  23. client-id: okta-client-id
  24. client-secret: okta-client-secret
  25. authorization-grant-type: authorization_code
  26. redirect-uri: "{baseUrl}/authorized/okta"
  27. scope: read, write
  28. provider:
  29. okta:
  30. authorization-uri: https://dev-1234.oktapreview.com/oauth2/v1/authorize
  31. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  32. ----
  33. A request with the base path `/oauth2/authorization/okta` will initiate the Authorization Request redirect by the `OAuth2AuthorizationRequestRedirectFilter` and ultimately start the Authorization Code grant flow.
  34. [NOTE]
  35. The `AuthorizationCodeOAuth2AuthorizedClientProvider` is an implementation of `OAuth2AuthorizedClientProvider` for the Authorization Code grant,
  36. which also initiates the Authorization Request redirect by the `OAuth2AuthorizationRequestRedirectFilter`.
  37. 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:
  38. [source,yaml,attrs="-attributes"]
  39. ----
  40. spring:
  41. security:
  42. oauth2:
  43. client:
  44. registration:
  45. okta:
  46. client-id: okta-client-id
  47. client-authentication-method: none
  48. authorization-grant-type: authorization_code
  49. redirect-uri: "{baseUrl}/authorized/okta"
  50. ...
  51. ----
  52. Public Clients are supported using https://tools.ietf.org/html/rfc7636[Proof Key for Code Exchange] (PKCE).
  53. 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:
  54. . `client-secret` is omitted (or empty)
  55. . `client-authentication-method` is set to "none" (`ClientAuthenticationMethod.NONE`)
  56. [TIP]
  57. 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())`.
  58. [[oauth2Client-auth-code-redirect-uri]]
  59. The `DefaultOAuth2AuthorizationRequestResolver` also supports `URI` template variables for the `redirect-uri` using `UriComponentsBuilder`.
  60. The following configuration uses all the supported `URI` template variables:
  61. [source,yaml,attrs="-attributes"]
  62. ----
  63. spring:
  64. security:
  65. oauth2:
  66. client:
  67. registration:
  68. okta:
  69. ...
  70. redirect-uri: "{baseScheme}://{baseHost}{basePort}{basePath}/authorized/{registrationId}"
  71. ...
  72. ----
  73. [NOTE]
  74. `+{baseUrl}+` resolves to `+{baseScheme}://{baseHost}{basePort}{basePath}+`
  75. 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].
  76. This ensures that the `X-Forwarded-*` headers are used when expanding the `redirect-uri`.
  77. === Customizing the Authorization Request
  78. 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.
  79. 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].
  80. One of those extended parameters is the `prompt` parameter.
  81. [NOTE]
  82. OPTIONAL. Space delimited, case sensitive list of ASCII string values that specifies whether the Authorization Server prompts the End-User for reauthentication and consent. The defined values are: none, login, consent, select_account
  83. 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`.
  84. ====
  85. .Java
  86. [source,java,role="primary"]
  87. ----
  88. @EnableWebSecurity
  89. public class OAuth2LoginSecurityConfig {
  90. @Autowired
  91. private ClientRegistrationRepository clientRegistrationRepository;
  92. @Bean
  93. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  94. http
  95. .authorizeHttpRequests(authorize -> authorize
  96. .anyRequest().authenticated()
  97. )
  98. .oauth2Login(oauth2 -> oauth2
  99. .authorizationEndpoint(authorization -> authorization
  100. .authorizationRequestResolver(
  101. authorizationRequestResolver(this.clientRegistrationRepository)
  102. )
  103. )
  104. );
  105. return http.build();
  106. }
  107. private OAuth2AuthorizationRequestResolver authorizationRequestResolver(
  108. ClientRegistrationRepository clientRegistrationRepository) {
  109. DefaultOAuth2AuthorizationRequestResolver authorizationRequestResolver =
  110. new DefaultOAuth2AuthorizationRequestResolver(
  111. clientRegistrationRepository, "/oauth2/authorization");
  112. authorizationRequestResolver.setAuthorizationRequestCustomizer(
  113. authorizationRequestCustomizer());
  114. return authorizationRequestResolver;
  115. }
  116. private Consumer<OAuth2AuthorizationRequest.Builder> authorizationRequestCustomizer() {
  117. return customizer -> customizer
  118. .additionalParameters(params -> params.put("prompt", "consent"));
  119. }
  120. }
  121. ----
  122. .Kotlin
  123. [source,kotlin,role="secondary"]
  124. ----
  125. @EnableWebSecurity
  126. class SecurityConfig {
  127. @Autowired
  128. private lateinit var customClientRegistrationRepository: ClientRegistrationRepository
  129. @Bean
  130. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  131. http {
  132. authorizeRequests {
  133. authorize(anyRequest, authenticated)
  134. }
  135. oauth2Login {
  136. authorizationEndpoint {
  137. authorizationRequestResolver = authorizationRequestResolver(customClientRegistrationRepository)
  138. }
  139. }
  140. }
  141. return http.build()
  142. }
  143. private fun authorizationRequestResolver(
  144. clientRegistrationRepository: ClientRegistrationRepository?): OAuth2AuthorizationRequestResolver? {
  145. val authorizationRequestResolver = DefaultOAuth2AuthorizationRequestResolver(
  146. clientRegistrationRepository, "/oauth2/authorization")
  147. authorizationRequestResolver.setAuthorizationRequestCustomizer(
  148. authorizationRequestCustomizer())
  149. return authorizationRequestResolver
  150. }
  151. private fun authorizationRequestCustomizer(): Consumer<OAuth2AuthorizationRequest.Builder> {
  152. return Consumer { customizer ->
  153. customizer
  154. .additionalParameters { params -> params["prompt"] = "consent" }
  155. }
  156. }
  157. }
  158. ----
  159. ====
  160. 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.
  161. For example, if the value for the request parameter `prompt` is always `consent` for the provider `okta`, than simply configure as follows:
  162. [source,yaml]
  163. ----
  164. spring:
  165. security:
  166. oauth2:
  167. client:
  168. provider:
  169. okta:
  170. authorization-uri: https://dev-1234.oktapreview.com/oauth2/v1/authorize?prompt=consent
  171. ----
  172. The preceding example shows the common use case of adding a custom parameter on top of the standard parameters.
  173. 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.
  174. [TIP]
  175. `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.
  176. The following example shows a variation of `authorizationRequestCustomizer()` from the preceding example, and instead overrides the `OAuth2AuthorizationRequest.authorizationRequestUri` property.
  177. ====
  178. .Java
  179. [source,java,role="primary"]
  180. ----
  181. private Consumer<OAuth2AuthorizationRequest.Builder> authorizationRequestCustomizer() {
  182. return customizer -> customizer
  183. .authorizationRequestUri(uriBuilder -> uriBuilder
  184. .queryParam("prompt", "consent").build());
  185. }
  186. ----
  187. .Kotlin
  188. [source,kotlin,role="secondary"]
  189. ----
  190. private fun authorizationRequestCustomizer(): Consumer<OAuth2AuthorizationRequest.Builder> {
  191. return Consumer { customizer: OAuth2AuthorizationRequest.Builder ->
  192. customizer
  193. .authorizationRequestUri { uriBuilder: UriBuilder ->
  194. uriBuilder
  195. .queryParam("prompt", "consent").build()
  196. }
  197. }
  198. }
  199. ----
  200. ====
  201. === Storing the Authorization Request
  202. 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).
  203. [TIP]
  204. The `OAuth2AuthorizationRequest` is used to correlate and validate the Authorization Response.
  205. The default implementation of `AuthorizationRequestRepository` is `HttpSessionOAuth2AuthorizationRequestRepository`, which stores the `OAuth2AuthorizationRequest` in the `HttpSession`.
  206. If you have a custom implementation of `AuthorizationRequestRepository`, you may configure it as shown in the following example:
  207. .AuthorizationRequestRepository Configuration
  208. ====
  209. .Java
  210. [source,java,role="primary"]
  211. ----
  212. @EnableWebSecurity
  213. public class OAuth2ClientSecurityConfig {
  214. @Bean
  215. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  216. http
  217. .oauth2Client(oauth2 -> oauth2
  218. .authorizationCodeGrant(codeGrant -> codeGrant
  219. .authorizationRequestRepository(this.authorizationRequestRepository())
  220. ...
  221. )
  222. );
  223. return http.build();
  224. }
  225. }
  226. ----
  227. .Kotlin
  228. [source,kotlin,role="secondary"]
  229. ----
  230. @EnableWebSecurity
  231. class OAuth2ClientSecurityConfig {
  232. @Bean
  233. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  234. http {
  235. oauth2Client {
  236. authorizationCodeGrant {
  237. authorizationRequestRepository = authorizationRequestRepository()
  238. }
  239. }
  240. }
  241. return http.build()
  242. }
  243. }
  244. ----
  245. .Xml
  246. [source,xml,role="secondary"]
  247. ----
  248. <http>
  249. <oauth2-client>
  250. <authorization-code-grant authorization-request-repository-ref="authorizationRequestRepository"/>
  251. </oauth2-client>
  252. </http>
  253. ----
  254. ====
  255. === Requesting an Access Token
  256. [NOTE]
  257. 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.
  258. The default implementation of `OAuth2AccessTokenResponseClient` for the Authorization Code grant is `DefaultAuthorizationCodeTokenResponseClient`, which uses a `RestOperations` for exchanging an authorization code for an access token at the Authorization Server’s Token Endpoint.
  259. The `DefaultAuthorizationCodeTokenResponseClient` is quite flexible as it allows you to customize the pre-processing of the Token Request and/or post-handling of the Token Response.
  260. === Customizing the Access Token Request
  261. If you need to customize the pre-processing of the Token Request, you can provide `DefaultAuthorizationCodeTokenResponseClient.setRequestEntityConverter()` with a custom `Converter<OAuth2AuthorizationCodeGrantRequest, RequestEntity<?>>`.
  262. The default implementation `OAuth2AuthorizationCodeGrantRequestEntityConverter` builds a `RequestEntity` representation of a standard https://tools.ietf.org/html/rfc6749#section-4.1.3[OAuth 2.0 Access Token Request].
  263. However, providing a custom `Converter`, would allow you to extend the standard Token Request and add custom parameter(s).
  264. To customize only the parameters of the request, you can provide `OAuth2AuthorizationCodeGrantRequestEntityConverter.setParametersConverter()` with a custom `Converter<OAuth2AuthorizationCodeGrantRequest, MultiValueMap<String, String>>` to completely override the parameters sent with the request. This is often simpler than constructing a `RequestEntity` directly.
  265. [TIP]
  266. If you prefer to only add additional parameters, you can provide `OAuth2AuthorizationCodeGrantRequestEntityConverter.addParametersConverter()` with a custom `Converter<OAuth2AuthorizationCodeGrantRequest, MultiValueMap<String, String>>` which constructs an aggregate `Converter`.
  267. IMPORTANT: 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.
  268. === Customizing the Access Token Response
  269. On the other end, if you need to customize the post-handling of the Token Response, you will need to provide `DefaultAuthorizationCodeTokenResponseClient.setRestOperations()` with a custom configured `RestOperations`.
  270. The default `RestOperations` is configured as follows:
  271. ====
  272. .Java
  273. [source,java,role="primary"]
  274. ----
  275. RestTemplate restTemplate = new RestTemplate(Arrays.asList(
  276. new FormHttpMessageConverter(),
  277. new OAuth2AccessTokenResponseHttpMessageConverter()));
  278. restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
  279. ----
  280. .Kotlin
  281. [source,kotlin,role="secondary"]
  282. ----
  283. val restTemplate = RestTemplate(listOf(
  284. FormHttpMessageConverter(),
  285. OAuth2AccessTokenResponseHttpMessageConverter()))
  286. restTemplate.errorHandler = OAuth2ErrorResponseErrorHandler()
  287. ----
  288. ====
  289. TIP: Spring MVC `FormHttpMessageConverter` is required as it's used when sending the OAuth 2.0 Access Token Request.
  290. `OAuth2AccessTokenResponseHttpMessageConverter` is a `HttpMessageConverter` for an OAuth 2.0 Access Token Response.
  291. You can provide `OAuth2AccessTokenResponseHttpMessageConverter.setAccessTokenResponseConverter()` with a custom `Converter<Map<String, Object>, OAuth2AccessTokenResponse>` that is used for converting the OAuth 2.0 Access Token Response parameters to an `OAuth2AccessTokenResponse`.
  292. `OAuth2ErrorResponseErrorHandler` is a `ResponseErrorHandler` that can handle an OAuth 2.0 Error, eg. 400 Bad Request.
  293. It uses an `OAuth2ErrorHttpMessageConverter` for converting the OAuth 2.0 Error parameters to an `OAuth2Error`.
  294. Whether you customize `DefaultAuthorizationCodeTokenResponseClient` or provide your own implementation of `OAuth2AccessTokenResponseClient`, you'll need to configure it as shown in the following example:
  295. .Access Token Response Configuration
  296. ====
  297. .Java
  298. [source,java,role="primary"]
  299. ----
  300. @EnableWebSecurity
  301. public class OAuth2ClientSecurityConfig {
  302. @Bean
  303. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  304. http
  305. .oauth2Client(oauth2 -> oauth2
  306. .authorizationCodeGrant(codeGrant -> codeGrant
  307. .accessTokenResponseClient(this.accessTokenResponseClient())
  308. ...
  309. )
  310. );
  311. return http.build();
  312. }
  313. }
  314. ----
  315. .Kotlin
  316. [source,kotlin,role="secondary"]
  317. ----
  318. @EnableWebSecurity
  319. class OAuth2ClientSecurityConfig {
  320. @Bean
  321. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  322. http {
  323. oauth2Client {
  324. authorizationCodeGrant {
  325. accessTokenResponseClient = accessTokenResponseClient()
  326. }
  327. }
  328. }
  329. return http.build()
  330. }
  331. }
  332. ----
  333. .Xml
  334. [source,xml,role="secondary"]
  335. ----
  336. <http>
  337. <oauth2-client>
  338. <authorization-code-grant access-token-response-client-ref="accessTokenResponseClient"/>
  339. </oauth2-client>
  340. </http>
  341. ----
  342. ====
  343. [[oauth2Client-refresh-token-grant]]
  344. == Refresh Token
  345. [NOTE]
  346. 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].
  347. === Refreshing an Access Token
  348. [NOTE]
  349. Please refer to the https://tools.ietf.org/html/rfc6749#section-6[Access Token Request/Response] protocol flow for the Refresh Token grant.
  350. The default implementation of `OAuth2AccessTokenResponseClient` for the Refresh Token grant is `DefaultRefreshTokenTokenResponseClient`, which uses a `RestOperations` when refreshing an access token at the Authorization Server’s Token Endpoint.
  351. The `DefaultRefreshTokenTokenResponseClient` is quite flexible as it allows you to customize the pre-processing of the Token Request and/or post-handling of the Token Response.
  352. === Customizing the Access Token Request
  353. If you need to customize the pre-processing of the Token Request, you can provide `DefaultRefreshTokenTokenResponseClient.setRequestEntityConverter()` with a custom `Converter<OAuth2RefreshTokenGrantRequest, RequestEntity<?>>`.
  354. The default implementation `OAuth2RefreshTokenGrantRequestEntityConverter` builds a `RequestEntity` representation of a standard https://tools.ietf.org/html/rfc6749#section-6[OAuth 2.0 Access Token Request].
  355. However, providing a custom `Converter`, would allow you to extend the standard Token Request and add custom parameter(s).
  356. To customize only the parameters of the request, you can provide `OAuth2RefreshTokenGrantRequestEntityConverter.setParametersConverter()` with a custom `Converter<OAuth2RefreshTokenGrantRequest, MultiValueMap<String, String>>` to completely override the parameters sent with the request. This is often simpler than constructing a `RequestEntity` directly.
  357. [TIP]
  358. If you prefer to only add additional parameters, you can provide `OAuth2RefreshTokenGrantRequestEntityConverter.addParametersConverter()` with a custom `Converter<OAuth2RefreshTokenGrantRequest, MultiValueMap<String, String>>` which constructs an aggregate `Converter`.
  359. IMPORTANT: 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.
  360. === Customizing the Access Token Response
  361. On the other end, if you need to customize the post-handling of the Token Response, you will need to provide `DefaultRefreshTokenTokenResponseClient.setRestOperations()` with a custom configured `RestOperations`.
  362. The default `RestOperations` is configured as follows:
  363. ====
  364. .Java
  365. [source,java,role="primary"]
  366. ----
  367. RestTemplate restTemplate = new RestTemplate(Arrays.asList(
  368. new FormHttpMessageConverter(),
  369. new OAuth2AccessTokenResponseHttpMessageConverter()));
  370. restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
  371. ----
  372. .Kotlin
  373. [source,kotlin,role="secondary"]
  374. ----
  375. val restTemplate = RestTemplate(listOf(
  376. FormHttpMessageConverter(),
  377. OAuth2AccessTokenResponseHttpMessageConverter()))
  378. restTemplate.errorHandler = OAuth2ErrorResponseErrorHandler()
  379. ----
  380. ====
  381. TIP: Spring MVC `FormHttpMessageConverter` is required as it's used when sending the OAuth 2.0 Access Token Request.
  382. `OAuth2AccessTokenResponseHttpMessageConverter` is a `HttpMessageConverter` for an OAuth 2.0 Access Token Response.
  383. You can provide `OAuth2AccessTokenResponseHttpMessageConverter.setAccessTokenResponseConverter()` with a custom `Converter<Map<String, Object>, OAuth2AccessTokenResponse>` that is used for converting the OAuth 2.0 Access Token Response parameters to an `OAuth2AccessTokenResponse`.
  384. `OAuth2ErrorResponseErrorHandler` is a `ResponseErrorHandler` that can handle an OAuth 2.0 Error, eg. 400 Bad Request.
  385. It uses an `OAuth2ErrorHttpMessageConverter` for converting the OAuth 2.0 Error parameters to an `OAuth2Error`.
  386. Whether you customize `DefaultRefreshTokenTokenResponseClient` or provide your own implementation of `OAuth2AccessTokenResponseClient`, you'll need to configure it as shown in the following example:
  387. ====
  388. .Java
  389. [source,java,role="primary"]
  390. ----
  391. // Customize
  392. OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> refreshTokenTokenResponseClient = ...
  393. OAuth2AuthorizedClientProvider authorizedClientProvider =
  394. OAuth2AuthorizedClientProviderBuilder.builder()
  395. .authorizationCode()
  396. .refreshToken(configurer -> configurer.accessTokenResponseClient(refreshTokenTokenResponseClient))
  397. .build();
  398. ...
  399. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  400. ----
  401. .Kotlin
  402. [source,kotlin,role="secondary"]
  403. ----
  404. // Customize
  405. val refreshTokenTokenResponseClient: OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> = ...
  406. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  407. .authorizationCode()
  408. .refreshToken { it.accessTokenResponseClient(refreshTokenTokenResponseClient) }
  409. .build()
  410. ...
  411. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  412. ----
  413. ====
  414. [NOTE]
  415. `OAuth2AuthorizedClientProviderBuilder.builder().refreshToken()` configures a `RefreshTokenOAuth2AuthorizedClientProvider`,
  416. which is an implementation of an `OAuth2AuthorizedClientProvider` for the Refresh Token grant.
  417. The `OAuth2RefreshToken` may optionally be returned in the Access Token Response for the `authorization_code` and `password` grant types.
  418. If the `OAuth2AuthorizedClient.getRefreshToken()` is available and the `OAuth2AuthorizedClient.getAccessToken()` is expired, it will automatically be refreshed by the `RefreshTokenOAuth2AuthorizedClientProvider`.
  419. [[oauth2Client-client-creds-grant]]
  420. == Client Credentials
  421. [NOTE]
  422. 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.
  423. === Requesting an Access Token
  424. [NOTE]
  425. 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.
  426. The default implementation of `OAuth2AccessTokenResponseClient` for the Client Credentials grant is `DefaultClientCredentialsTokenResponseClient`, which uses a `RestOperations` when requesting an access token at the Authorization Server’s Token Endpoint.
  427. The `DefaultClientCredentialsTokenResponseClient` is quite flexible as it allows you to customize the pre-processing of the Token Request and/or post-handling of the Token Response.
  428. === Customizing the Access Token Request
  429. If you need to customize the pre-processing of the Token Request, you can provide `DefaultClientCredentialsTokenResponseClient.setRequestEntityConverter()` with a custom `Converter<OAuth2ClientCredentialsGrantRequest, RequestEntity<?>>`.
  430. The default implementation `OAuth2ClientCredentialsGrantRequestEntityConverter` builds a `RequestEntity` representation of a standard https://tools.ietf.org/html/rfc6749#section-4.4.2[OAuth 2.0 Access Token Request].
  431. However, providing a custom `Converter`, would allow you to extend the standard Token Request and add custom parameter(s).
  432. To customize only the parameters of the request, you can provide `OAuth2ClientCredentialsGrantRequestEntityConverter.setParametersConverter()` with a custom `Converter<OAuth2ClientCredentialsGrantRequest, MultiValueMap<String, String>>` to completely override the parameters sent with the request. This is often simpler than constructing a `RequestEntity` directly.
  433. [TIP]
  434. If you prefer to only add additional parameters, you can provide `OAuth2ClientCredentialsGrantRequestEntityConverter.addParametersConverter()` with a custom `Converter<OAuth2ClientCredentialsGrantRequest, MultiValueMap<String, String>>` which constructs an aggregate `Converter`.
  435. IMPORTANT: 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.
  436. === Customizing the Access Token Response
  437. On the other end, if you need to customize the post-handling of the Token Response, you will need to provide `DefaultClientCredentialsTokenResponseClient.setRestOperations()` with a custom configured `RestOperations`.
  438. The default `RestOperations` is configured as follows:
  439. ====
  440. .Java
  441. [source,java,role="primary"]
  442. ----
  443. RestTemplate restTemplate = new RestTemplate(Arrays.asList(
  444. new FormHttpMessageConverter(),
  445. new OAuth2AccessTokenResponseHttpMessageConverter()));
  446. restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
  447. ----
  448. .Kotlin
  449. [source,kotlin,role="secondary"]
  450. ----
  451. val restTemplate = RestTemplate(listOf(
  452. FormHttpMessageConverter(),
  453. OAuth2AccessTokenResponseHttpMessageConverter()))
  454. restTemplate.errorHandler = OAuth2ErrorResponseErrorHandler()
  455. ----
  456. ====
  457. TIP: Spring MVC `FormHttpMessageConverter` is required as it's used when sending the OAuth 2.0 Access Token Request.
  458. `OAuth2AccessTokenResponseHttpMessageConverter` is a `HttpMessageConverter` for an OAuth 2.0 Access Token Response.
  459. You can provide `OAuth2AccessTokenResponseHttpMessageConverter.setAccessTokenResponseConverter()` with a custom `Converter<Map<String, Object>, OAuth2AccessTokenResponse>` that is used for converting the OAuth 2.0 Access Token Response parameters to an `OAuth2AccessTokenResponse`.
  460. `OAuth2ErrorResponseErrorHandler` is a `ResponseErrorHandler` that can handle an OAuth 2.0 Error, eg. 400 Bad Request.
  461. It uses an `OAuth2ErrorHttpMessageConverter` for converting the OAuth 2.0 Error parameters to an `OAuth2Error`.
  462. Whether you customize `DefaultClientCredentialsTokenResponseClient` or provide your own implementation of `OAuth2AccessTokenResponseClient`, you'll need to configure it as shown in the following example:
  463. ====
  464. .Java
  465. [source,java,role="primary"]
  466. ----
  467. // Customize
  468. OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsTokenResponseClient = ...
  469. OAuth2AuthorizedClientProvider authorizedClientProvider =
  470. OAuth2AuthorizedClientProviderBuilder.builder()
  471. .clientCredentials(configurer -> configurer.accessTokenResponseClient(clientCredentialsTokenResponseClient))
  472. .build();
  473. ...
  474. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  475. ----
  476. .Kotlin
  477. [source,kotlin,role="secondary"]
  478. ----
  479. // Customize
  480. val clientCredentialsTokenResponseClient: OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> = ...
  481. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  482. .clientCredentials { it.accessTokenResponseClient(clientCredentialsTokenResponseClient) }
  483. .build()
  484. ...
  485. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  486. ----
  487. ====
  488. [NOTE]
  489. `OAuth2AuthorizedClientProviderBuilder.builder().clientCredentials()` configures a `ClientCredentialsOAuth2AuthorizedClientProvider`,
  490. which is an implementation of an `OAuth2AuthorizedClientProvider` for the Client Credentials grant.
  491. === Using the Access Token
  492. Given the following Spring Boot 2.x properties for an OAuth 2.0 Client registration:
  493. [source,yaml]
  494. ----
  495. spring:
  496. security:
  497. oauth2:
  498. client:
  499. registration:
  500. okta:
  501. client-id: okta-client-id
  502. client-secret: okta-client-secret
  503. authorization-grant-type: client_credentials
  504. scope: read, write
  505. provider:
  506. okta:
  507. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  508. ----
  509. ...and the `OAuth2AuthorizedClientManager` `@Bean`:
  510. ====
  511. .Java
  512. [source,java,role="primary"]
  513. ----
  514. @Bean
  515. public OAuth2AuthorizedClientManager authorizedClientManager(
  516. ClientRegistrationRepository clientRegistrationRepository,
  517. OAuth2AuthorizedClientRepository authorizedClientRepository) {
  518. OAuth2AuthorizedClientProvider authorizedClientProvider =
  519. OAuth2AuthorizedClientProviderBuilder.builder()
  520. .clientCredentials()
  521. .build();
  522. DefaultOAuth2AuthorizedClientManager authorizedClientManager =
  523. new DefaultOAuth2AuthorizedClientManager(
  524. clientRegistrationRepository, authorizedClientRepository);
  525. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  526. return authorizedClientManager;
  527. }
  528. ----
  529. .Kotlin
  530. [source,kotlin,role="secondary"]
  531. ----
  532. @Bean
  533. fun authorizedClientManager(
  534. clientRegistrationRepository: ClientRegistrationRepository,
  535. authorizedClientRepository: OAuth2AuthorizedClientRepository): OAuth2AuthorizedClientManager {
  536. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  537. .clientCredentials()
  538. .build()
  539. val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
  540. clientRegistrationRepository, authorizedClientRepository)
  541. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  542. return authorizedClientManager
  543. }
  544. ----
  545. ====
  546. You may obtain the `OAuth2AccessToken` as follows:
  547. ====
  548. .Java
  549. [source,java,role="primary"]
  550. ----
  551. @Controller
  552. public class OAuth2ClientController {
  553. @Autowired
  554. private OAuth2AuthorizedClientManager authorizedClientManager;
  555. @GetMapping("/")
  556. public String index(Authentication authentication,
  557. HttpServletRequest servletRequest,
  558. HttpServletResponse servletResponse) {
  559. OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  560. .principal(authentication)
  561. .attributes(attrs -> {
  562. attrs.put(HttpServletRequest.class.getName(), servletRequest);
  563. attrs.put(HttpServletResponse.class.getName(), servletResponse);
  564. })
  565. .build();
  566. OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest);
  567. OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
  568. ...
  569. return "index";
  570. }
  571. }
  572. ----
  573. .Kotlin
  574. [source,kotlin,role="secondary"]
  575. ----
  576. class OAuth2ClientController {
  577. @Autowired
  578. private lateinit var authorizedClientManager: OAuth2AuthorizedClientManager
  579. @GetMapping("/")
  580. fun index(authentication: Authentication?,
  581. servletRequest: HttpServletRequest,
  582. servletResponse: HttpServletResponse): String {
  583. val authorizeRequest: OAuth2AuthorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  584. .principal(authentication)
  585. .attributes(Consumer { attrs: MutableMap<String, Any> ->
  586. attrs[HttpServletRequest::class.java.name] = servletRequest
  587. attrs[HttpServletResponse::class.java.name] = servletResponse
  588. })
  589. .build()
  590. val authorizedClient = authorizedClientManager.authorize(authorizeRequest)
  591. val accessToken: OAuth2AccessToken = authorizedClient.accessToken
  592. ...
  593. return "index"
  594. }
  595. }
  596. ----
  597. ====
  598. [NOTE]
  599. `HttpServletRequest` and `HttpServletResponse` are both OPTIONAL attributes.
  600. If not provided, it will default to `ServletRequestAttributes` using `RequestContextHolder.getRequestAttributes()`.
  601. [[oauth2Client-password-grant]]
  602. == Resource Owner Password Credentials
  603. [NOTE]
  604. 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.
  605. === Requesting an Access Token
  606. [NOTE]
  607. 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.
  608. 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.
  609. The `DefaultPasswordTokenResponseClient` is quite flexible as it allows you to customize the pre-processing of the Token Request and/or post-handling of the Token Response.
  610. === Customizing the Access Token Request
  611. If you need to customize the pre-processing of the Token Request, you can provide `DefaultPasswordTokenResponseClient.setRequestEntityConverter()` with a custom `Converter<OAuth2PasswordGrantRequest, RequestEntity<?>>`.
  612. 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].
  613. However, providing a custom `Converter`, would allow you to extend the standard Token Request and add custom parameter(s).
  614. 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.
  615. [TIP]
  616. 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`.
  617. IMPORTANT: 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.
  618. === Customizing the Access Token Response
  619. On the other end, if you need to customize the post-handling of the Token Response, you will need to provide `DefaultPasswordTokenResponseClient.setRestOperations()` with a custom configured `RestOperations`.
  620. The default `RestOperations` is configured as follows:
  621. ====
  622. .Java
  623. [source,java,role="primary"]
  624. ----
  625. RestTemplate restTemplate = new RestTemplate(Arrays.asList(
  626. new FormHttpMessageConverter(),
  627. new OAuth2AccessTokenResponseHttpMessageConverter()));
  628. restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
  629. ----
  630. .Kotlin
  631. [source,kotlin,role="secondary"]
  632. ----
  633. val restTemplate = RestTemplate(listOf(
  634. FormHttpMessageConverter(),
  635. OAuth2AccessTokenResponseHttpMessageConverter()))
  636. restTemplate.errorHandler = OAuth2ErrorResponseErrorHandler()
  637. ----
  638. ====
  639. TIP: Spring MVC `FormHttpMessageConverter` is required as it's used when sending the OAuth 2.0 Access Token Request.
  640. `OAuth2AccessTokenResponseHttpMessageConverter` is a `HttpMessageConverter` for an OAuth 2.0 Access Token Response.
  641. You can provide `OAuth2AccessTokenResponseHttpMessageConverter.setAccessTokenResponseConverter()` with a custom `Converter<Map<String, Object>, OAuth2AccessTokenResponse>` that is used for converting the OAuth 2.0 Access Token Response parameters to an `OAuth2AccessTokenResponse`.
  642. `OAuth2ErrorResponseErrorHandler` is a `ResponseErrorHandler` that can handle an OAuth 2.0 Error, eg. 400 Bad Request.
  643. It uses an `OAuth2ErrorHttpMessageConverter` for converting the OAuth 2.0 Error parameters to an `OAuth2Error`.
  644. Whether you customize `DefaultPasswordTokenResponseClient` or provide your own implementation of `OAuth2AccessTokenResponseClient`, you'll need to configure it as shown in the following example:
  645. ====
  646. .Java
  647. [source,java,role="primary"]
  648. ----
  649. // Customize
  650. OAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> passwordTokenResponseClient = ...
  651. OAuth2AuthorizedClientProvider authorizedClientProvider =
  652. OAuth2AuthorizedClientProviderBuilder.builder()
  653. .password(configurer -> configurer.accessTokenResponseClient(passwordTokenResponseClient))
  654. .refreshToken()
  655. .build();
  656. ...
  657. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  658. ----
  659. .Kotlin
  660. [source,kotlin,role="secondary"]
  661. ----
  662. val passwordTokenResponseClient: OAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> = ...
  663. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  664. .password { it.accessTokenResponseClient(passwordTokenResponseClient) }
  665. .refreshToken()
  666. .build()
  667. ...
  668. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  669. ----
  670. ====
  671. [NOTE]
  672. `OAuth2AuthorizedClientProviderBuilder.builder().password()` configures a `PasswordOAuth2AuthorizedClientProvider`,
  673. which is an implementation of an `OAuth2AuthorizedClientProvider` for the Resource Owner Password Credentials grant.
  674. === Using the Access Token
  675. Given the following Spring Boot 2.x properties for an OAuth 2.0 Client registration:
  676. [source,yaml]
  677. ----
  678. spring:
  679. security:
  680. oauth2:
  681. client:
  682. registration:
  683. okta:
  684. client-id: okta-client-id
  685. client-secret: okta-client-secret
  686. authorization-grant-type: password
  687. scope: read, write
  688. provider:
  689. okta:
  690. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  691. ----
  692. ...and the `OAuth2AuthorizedClientManager` `@Bean`:
  693. ====
  694. .Java
  695. [source,java,role="primary"]
  696. ----
  697. @Bean
  698. public OAuth2AuthorizedClientManager authorizedClientManager(
  699. ClientRegistrationRepository clientRegistrationRepository,
  700. OAuth2AuthorizedClientRepository authorizedClientRepository) {
  701. OAuth2AuthorizedClientProvider authorizedClientProvider =
  702. OAuth2AuthorizedClientProviderBuilder.builder()
  703. .password()
  704. .refreshToken()
  705. .build();
  706. DefaultOAuth2AuthorizedClientManager authorizedClientManager =
  707. new DefaultOAuth2AuthorizedClientManager(
  708. clientRegistrationRepository, authorizedClientRepository);
  709. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  710. // Assuming the `username` and `password` are supplied as `HttpServletRequest` parameters,
  711. // map the `HttpServletRequest` parameters to `OAuth2AuthorizationContext.getAttributes()`
  712. authorizedClientManager.setContextAttributesMapper(contextAttributesMapper());
  713. return authorizedClientManager;
  714. }
  715. private Function<OAuth2AuthorizeRequest, Map<String, Object>> contextAttributesMapper() {
  716. return authorizeRequest -> {
  717. Map<String, Object> contextAttributes = Collections.emptyMap();
  718. HttpServletRequest servletRequest = authorizeRequest.getAttribute(HttpServletRequest.class.getName());
  719. String username = servletRequest.getParameter(OAuth2ParameterNames.USERNAME);
  720. String password = servletRequest.getParameter(OAuth2ParameterNames.PASSWORD);
  721. if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
  722. contextAttributes = new HashMap<>();
  723. // `PasswordOAuth2AuthorizedClientProvider` requires both attributes
  724. contextAttributes.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username);
  725. contextAttributes.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password);
  726. }
  727. return contextAttributes;
  728. };
  729. }
  730. ----
  731. .Kotlin
  732. [source,kotlin,role="secondary"]
  733. ----
  734. @Bean
  735. fun authorizedClientManager(
  736. clientRegistrationRepository: ClientRegistrationRepository,
  737. authorizedClientRepository: OAuth2AuthorizedClientRepository): OAuth2AuthorizedClientManager {
  738. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  739. .password()
  740. .refreshToken()
  741. .build()
  742. val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
  743. clientRegistrationRepository, authorizedClientRepository)
  744. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  745. // Assuming the `username` and `password` are supplied as `HttpServletRequest` parameters,
  746. // map the `HttpServletRequest` parameters to `OAuth2AuthorizationContext.getAttributes()`
  747. authorizedClientManager.setContextAttributesMapper(contextAttributesMapper())
  748. return authorizedClientManager
  749. }
  750. private fun contextAttributesMapper(): Function<OAuth2AuthorizeRequest, MutableMap<String, Any>> {
  751. return Function { authorizeRequest ->
  752. var contextAttributes: MutableMap<String, Any> = mutableMapOf()
  753. val servletRequest: HttpServletRequest = authorizeRequest.getAttribute(HttpServletRequest::class.java.name)
  754. val username = servletRequest.getParameter(OAuth2ParameterNames.USERNAME)
  755. val password = servletRequest.getParameter(OAuth2ParameterNames.PASSWORD)
  756. if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
  757. contextAttributes = hashMapOf()
  758. // `PasswordOAuth2AuthorizedClientProvider` requires both attributes
  759. contextAttributes[OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME] = username
  760. contextAttributes[OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME] = password
  761. }
  762. contextAttributes
  763. }
  764. }
  765. ----
  766. ====
  767. You may obtain the `OAuth2AccessToken` as follows:
  768. ====
  769. .Java
  770. [source,java,role="primary"]
  771. ----
  772. @Controller
  773. public class OAuth2ClientController {
  774. @Autowired
  775. private OAuth2AuthorizedClientManager authorizedClientManager;
  776. @GetMapping("/")
  777. public String index(Authentication authentication,
  778. HttpServletRequest servletRequest,
  779. HttpServletResponse servletResponse) {
  780. OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  781. .principal(authentication)
  782. .attributes(attrs -> {
  783. attrs.put(HttpServletRequest.class.getName(), servletRequest);
  784. attrs.put(HttpServletResponse.class.getName(), servletResponse);
  785. })
  786. .build();
  787. OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest);
  788. OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
  789. ...
  790. return "index";
  791. }
  792. }
  793. ----
  794. .Kotlin
  795. [source,kotlin,role="secondary"]
  796. ----
  797. @Controller
  798. class OAuth2ClientController {
  799. @Autowired
  800. private lateinit var authorizedClientManager: OAuth2AuthorizedClientManager
  801. @GetMapping("/")
  802. fun index(authentication: Authentication?,
  803. servletRequest: HttpServletRequest,
  804. servletResponse: HttpServletResponse): String {
  805. val authorizeRequest: OAuth2AuthorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  806. .principal(authentication)
  807. .attributes(Consumer {
  808. it[HttpServletRequest::class.java.name] = servletRequest
  809. it[HttpServletResponse::class.java.name] = servletResponse
  810. })
  811. .build()
  812. val authorizedClient = authorizedClientManager.authorize(authorizeRequest)
  813. val accessToken: OAuth2AccessToken = authorizedClient.accessToken
  814. ...
  815. return "index"
  816. }
  817. }
  818. ----
  819. ====
  820. [NOTE]
  821. `HttpServletRequest` and `HttpServletResponse` are both OPTIONAL attributes.
  822. If not provided, it will default to `ServletRequestAttributes` using `RequestContextHolder.getRequestAttributes()`.
  823. [[oauth2Client-jwt-bearer-grant]]
  824. == JWT Bearer
  825. [NOTE]
  826. 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.
  827. === Requesting an Access Token
  828. [NOTE]
  829. 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.
  830. The default implementation of `OAuth2AccessTokenResponseClient` for the JWT Bearer grant is `DefaultJwtBearerTokenResponseClient`, which uses a `RestOperations` when requesting an access token at the Authorization Server’s Token Endpoint.
  831. The `DefaultJwtBearerTokenResponseClient` is quite flexible as it allows you to customize the pre-processing of the Token Request and/or post-handling of the Token Response.
  832. === Customizing the Access Token Request
  833. If you need to customize the pre-processing of the Token Request, you can provide `DefaultJwtBearerTokenResponseClient.setRequestEntityConverter()` with a custom `Converter<JwtBearerGrantRequest, RequestEntity<?>>`.
  834. The default implementation `JwtBearerGrantRequestEntityConverter` builds a `RequestEntity` representation of a https://datatracker.ietf.org/doc/html/rfc7523#section-2.1[OAuth 2.0 Access Token Request].
  835. However, providing a custom `Converter`, would allow you to extend the Token Request and add custom parameter(s).
  836. To customize only the parameters of the request, you can provide `JwtBearerGrantRequestEntityConverter.setParametersConverter()` with a custom `Converter<JwtBearerGrantRequest, MultiValueMap<String, String>>` to completely override the parameters sent with the request. This is often simpler than constructing a `RequestEntity` directly.
  837. [TIP]
  838. If you prefer to only add additional parameters, you can provide `JwtBearerGrantRequestEntityConverter.addParametersConverter()` with a custom `Converter<JwtBearerGrantRequest, MultiValueMap<String, String>>` which constructs an aggregate `Converter`.
  839. === Customizing the Access Token Response
  840. On the other end, if you need to customize the post-handling of the Token Response, you will need to provide `DefaultJwtBearerTokenResponseClient.setRestOperations()` with a custom configured `RestOperations`.
  841. The default `RestOperations` is configured as follows:
  842. ====
  843. .Java
  844. [source,java,role="primary"]
  845. ----
  846. RestTemplate restTemplate = new RestTemplate(Arrays.asList(
  847. new FormHttpMessageConverter(),
  848. new OAuth2AccessTokenResponseHttpMessageConverter()));
  849. restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
  850. ----
  851. .Kotlin
  852. [source,kotlin,role="secondary"]
  853. ----
  854. val restTemplate = RestTemplate(listOf(
  855. FormHttpMessageConverter(),
  856. OAuth2AccessTokenResponseHttpMessageConverter()))
  857. restTemplate.errorHandler = OAuth2ErrorResponseErrorHandler()
  858. ----
  859. ====
  860. TIP: Spring MVC `FormHttpMessageConverter` is required as it's used when sending the OAuth 2.0 Access Token Request.
  861. `OAuth2AccessTokenResponseHttpMessageConverter` is a `HttpMessageConverter` for an OAuth 2.0 Access Token Response.
  862. You can provide `OAuth2AccessTokenResponseHttpMessageConverter.setAccessTokenResponseConverter()` with a custom `Converter<Map<String, Object>, OAuth2AccessTokenResponse>` that is used for converting the OAuth 2.0 Access Token Response parameters to an `OAuth2AccessTokenResponse`.
  863. `OAuth2ErrorResponseErrorHandler` is a `ResponseErrorHandler` that can handle an OAuth 2.0 Error, eg. 400 Bad Request.
  864. It uses an `OAuth2ErrorHttpMessageConverter` for converting the OAuth 2.0 Error parameters to an `OAuth2Error`.
  865. Whether you customize `DefaultJwtBearerTokenResponseClient` or provide your own implementation of `OAuth2AccessTokenResponseClient`, you'll need to configure it as shown in the following example:
  866. ====
  867. .Java
  868. [source,java,role="primary"]
  869. ----
  870. // Customize
  871. OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> jwtBearerTokenResponseClient = ...
  872. JwtBearerOAuth2AuthorizedClientProvider jwtBearerAuthorizedClientProvider = new JwtBearerOAuth2AuthorizedClientProvider();
  873. jwtBearerAuthorizedClientProvider.setAccessTokenResponseClient(jwtBearerTokenResponseClient);
  874. OAuth2AuthorizedClientProvider authorizedClientProvider =
  875. OAuth2AuthorizedClientProviderBuilder.builder()
  876. .provider(jwtBearerAuthorizedClientProvider)
  877. .build();
  878. ...
  879. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  880. ----
  881. .Kotlin
  882. [source,kotlin,role="secondary"]
  883. ----
  884. // Customize
  885. val jwtBearerTokenResponseClient: OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> = ...
  886. val jwtBearerAuthorizedClientProvider = JwtBearerOAuth2AuthorizedClientProvider()
  887. jwtBearerAuthorizedClientProvider.setAccessTokenResponseClient(jwtBearerTokenResponseClient);
  888. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  889. .provider(jwtBearerAuthorizedClientProvider)
  890. .build()
  891. ...
  892. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  893. ----
  894. ====
  895. === Using the Access Token
  896. Given the following Spring Boot 2.x properties for an OAuth 2.0 Client registration:
  897. [source,yaml]
  898. ----
  899. spring:
  900. security:
  901. oauth2:
  902. client:
  903. registration:
  904. okta:
  905. client-id: okta-client-id
  906. client-secret: okta-client-secret
  907. authorization-grant-type: urn:ietf:params:oauth:grant-type:jwt-bearer
  908. scope: read
  909. provider:
  910. okta:
  911. token-uri: https://dev-1234.oktapreview.com/oauth2/v1/token
  912. ----
  913. ...and the `OAuth2AuthorizedClientManager` `@Bean`:
  914. ====
  915. .Java
  916. [source,java,role="primary"]
  917. ----
  918. @Bean
  919. public OAuth2AuthorizedClientManager authorizedClientManager(
  920. ClientRegistrationRepository clientRegistrationRepository,
  921. OAuth2AuthorizedClientRepository authorizedClientRepository) {
  922. JwtBearerOAuth2AuthorizedClientProvider jwtBearerAuthorizedClientProvider =
  923. new JwtBearerOAuth2AuthorizedClientProvider();
  924. OAuth2AuthorizedClientProvider authorizedClientProvider =
  925. OAuth2AuthorizedClientProviderBuilder.builder()
  926. .provider(jwtBearerAuthorizedClientProvider)
  927. .build();
  928. DefaultOAuth2AuthorizedClientManager authorizedClientManager =
  929. new DefaultOAuth2AuthorizedClientManager(
  930. clientRegistrationRepository, authorizedClientRepository);
  931. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  932. return authorizedClientManager;
  933. }
  934. ----
  935. .Kotlin
  936. [source,kotlin,role="secondary"]
  937. ----
  938. @Bean
  939. fun authorizedClientManager(
  940. clientRegistrationRepository: ClientRegistrationRepository,
  941. authorizedClientRepository: OAuth2AuthorizedClientRepository): OAuth2AuthorizedClientManager {
  942. val jwtBearerAuthorizedClientProvider = JwtBearerOAuth2AuthorizedClientProvider()
  943. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  944. .provider(jwtBearerAuthorizedClientProvider)
  945. .build()
  946. val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
  947. clientRegistrationRepository, authorizedClientRepository)
  948. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  949. return authorizedClientManager
  950. }
  951. ----
  952. ====
  953. You may obtain the `OAuth2AccessToken` as follows:
  954. ====
  955. .Java
  956. [source,java,role="primary"]
  957. ----
  958. @RestController
  959. public class OAuth2ResourceServerController {
  960. @Autowired
  961. private OAuth2AuthorizedClientManager authorizedClientManager;
  962. @GetMapping("/resource")
  963. public String resource(JwtAuthenticationToken jwtAuthentication) {
  964. OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  965. .principal(jwtAuthentication)
  966. .build();
  967. OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest);
  968. OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
  969. ...
  970. }
  971. }
  972. ----
  973. .Kotlin
  974. [source,kotlin,role="secondary"]
  975. ----
  976. class OAuth2ResourceServerController {
  977. @Autowired
  978. private lateinit var authorizedClientManager: OAuth2AuthorizedClientManager
  979. @GetMapping("/resource")
  980. fun resource(jwtAuthentication: JwtAuthenticationToken?): String {
  981. val authorizeRequest: OAuth2AuthorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId("okta")
  982. .principal(jwtAuthentication)
  983. .build()
  984. val authorizedClient = authorizedClientManager.authorize(authorizeRequest)
  985. val accessToken: OAuth2AccessToken = authorizedClient.accessToken
  986. ...
  987. }
  988. }
  989. ----
  990. ====
  991. [NOTE]
  992. `JwtBearerOAuth2AuthorizedClientProvider` resolves the `Jwt` assertion via `OAuth2AuthorizationContext.getPrincipal().getPrincipal()` by default, hence the use of `JwtAuthenticationToken` in the preceding example.
  993. [TIP]
  994. If you need to resolve the `Jwt` assertion from a different source, you can provide `JwtBearerOAuth2AuthorizedClientProvider.setJwtAssertionResolver()` with a custom `Function<OAuth2AuthorizationContext, Jwt>`.