authorization-grants.adoc 52 KB

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