index.adoc 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  1. = OAuth2
  2. Spring Security provides comprehensive OAuth 2.0 support.
  3. This section discusses how to integrate OAuth 2.0 into your servlet based application.
  4. [[oauth2-overview]]
  5. == Overview
  6. Spring Security's OAuth 2.0 support consists of two primary feature sets:
  7. * <<oauth2-resource-server>>
  8. * <<oauth2-client>>
  9. [NOTE]
  10. ====
  11. <<oauth2-client-log-users-in,OAuth2 Login>> is a very powerful OAuth2 Client feature that deserves its own section in the reference documentation.
  12. However, it does not exist as a standalone feature and requires OAuth2 Client in order to function.
  13. ====
  14. These feature sets cover the _resource server_ and _client_ roles defined in the https://tools.ietf.org/html/rfc6749#section-1.1[OAuth 2.0 Authorization Framework], while the _authorization server_ role is covered by https://docs.spring.io/spring-authorization-server/reference/index.html[Spring Authorization Server], which is a separate project built on xref:index.adoc[Spring Security].
  15. The _resource server_ and _client_ roles in OAuth2 are typically represented by one or more server-side applications.
  16. Additionally, the _authorization server_ role can be represented by one or more third parties (as is the case when centralizing identity management and/or authentication within an organization) *-or-* it can be represented by an application (as is the case with Spring Authorization Server).
  17. For example, a typical OAuth2-based microservices architecture might consist of a single user-facing client application, several backend resource servers providing REST APIs and a third party authorization server for managing users and authentication concerns.
  18. It is also common to have a single application representing only one of these roles with the need to integrate with one or more third parties that are providing the other roles.
  19. Spring Security handles these scenarios and more.
  20. The following sections cover the roles provided by Spring Security and contain examples for common scenarios.
  21. [[oauth2-resource-server]]
  22. == OAuth2 Resource Server
  23. [NOTE]
  24. ====
  25. This section contains a summary of OAuth2 Resource Server features with examples.
  26. See xref:servlet/oauth2/resource-server/index.adoc[OAuth 2.0 Resource Server] for complete reference documentation.
  27. ====
  28. To get started, add the `spring-security-oauth2-resource-server` dependency to your project.
  29. When using Spring Boot, add the following starter:
  30. .OAuth2 Client with Spring Boot
  31. [tabs]
  32. ======
  33. Gradle::
  34. +
  35. [source,gradle,role="primary"]
  36. ----
  37. implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
  38. ----
  39. Maven::
  40. +
  41. [source,maven,role="secondary"]
  42. ----
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
  46. </dependency>
  47. ----
  48. ======
  49. [TIP]
  50. ====
  51. See xref:getting-spring-security.adoc[] for additional options when not using Spring Boot.
  52. ====
  53. Consider the following use cases for OAuth2 Resource Server:
  54. * <<oauth2-resource-server-access-token,I want to protect access to the API using OAuth2>> (authorization server provides JWT or opaque access token)
  55. * <<oauth2-resource-server-custom-jwt,I want to protect access to the API using a JWT>> (custom token)
  56. [[oauth2-resource-server-access-token]]
  57. === Protect Access with an OAuth2 Access Token
  58. It is very common to protect access to an API using OAuth2 access tokens.
  59. In most cases, Spring Security requires only minimal configuration to secure an application with OAuth2.
  60. There are two types of `Bearer` tokens supported by Spring Security which each use a different component for validation:
  61. * <<oauth2-resource-server-access-token-jwt,JWT support>> uses a `JwtDecoder` bean to validate signatures and decode tokens
  62. * <<oauth2-resource-server-access-token-opaque,Opaque token support>> uses an `OpaqueTokenIntrospector` bean to introspect tokens
  63. [[oauth2-resource-server-access-token-jwt]]
  64. ==== JWT Support
  65. The following example configures a `JwtDecoder` bean using Spring Boot configuration properties:
  66. [source,yaml]
  67. ----
  68. spring:
  69. security:
  70. oauth2:
  71. resourceserver:
  72. jwt:
  73. issuer-uri: https://my-auth-server.com
  74. ----
  75. When using Spring Boot, this is all that is required.
  76. The default arrangement provided by Spring Boot is equivalent to the following:
  77. .Configure Resource Server with JWTs
  78. [tabs]
  79. =====
  80. Java::
  81. +
  82. [source,java,role="primary"]
  83. ----
  84. @Configuration
  85. @EnableWebSecurity
  86. public class SecurityConfig {
  87. @Bean
  88. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  89. http
  90. .authorizeHttpRequests((authorize) -> authorize
  91. .anyRequest().authenticated()
  92. )
  93. .oauth2ResourceServer((oauth2) -> oauth2
  94. .jwt(Customizer.withDefaults())
  95. );
  96. return http.build();
  97. }
  98. @Bean
  99. public JwtDecoder jwtDecoder() {
  100. return JwtDecoders.fromIssuerLocation("https://my-auth-server.com");
  101. }
  102. }
  103. ----
  104. Kotlin::
  105. +
  106. [source,kotlin,role="secondary"]
  107. ----
  108. import org.springframework.security.config.annotation.web.invoke
  109. @Configuration
  110. @EnableWebSecurity
  111. class SecurityConfig {
  112. @Bean
  113. fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
  114. http {
  115. authorizeHttpRequests {
  116. authorize(anyRequest, authenticated)
  117. }
  118. oauth2ResourceServer {
  119. jwt { }
  120. }
  121. }
  122. return http.build()
  123. }
  124. @Bean
  125. fun jwtDecoder(): JwtDecoder {
  126. return JwtDecoders.fromIssuerLocation("https://my-auth-server.com")
  127. }
  128. }
  129. ----
  130. =====
  131. [[oauth2-resource-server-access-token-opaque]]
  132. ==== Opaque Token Support
  133. The following example configures an `OpaqueTokenIntrospector` bean using Spring Boot configuration properties:
  134. [source,yaml]
  135. ----
  136. spring:
  137. security:
  138. oauth2:
  139. resourceserver:
  140. opaquetoken:
  141. introspection-uri: https://my-auth-server.com/oauth2/introspect
  142. client-id: my-client-id
  143. client-secret: my-client-secret
  144. ----
  145. When using Spring Boot, this is all that is required.
  146. The default arrangement provided by Spring Boot is equivalent to the following:
  147. .Configure Resource Server with Opaque Tokens
  148. [tabs]
  149. =====
  150. Java::
  151. +
  152. [source,java,role="primary"]
  153. ----
  154. @Configuration
  155. @EnableWebSecurity
  156. public class SecurityConfig {
  157. @Bean
  158. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  159. http
  160. .authorizeHttpRequests((authorize) -> authorize
  161. .anyRequest().authenticated()
  162. )
  163. .oauth2ResourceServer((oauth2) -> oauth2
  164. .opaqueToken(Customizer.withDefaults())
  165. );
  166. return http.build();
  167. }
  168. @Bean
  169. public OpaqueTokenIntrospector opaqueTokenIntrospector() {
  170. return new SpringOpaqueTokenIntrospector(
  171. "https://my-auth-server.com/oauth2/introspect", "my-client-id", "my-client-secret");
  172. }
  173. }
  174. ----
  175. Kotlin::
  176. +
  177. [source,kotlin,role="secondary"]
  178. ----
  179. import org.springframework.security.config.annotation.web.invoke
  180. @Configuration
  181. @EnableWebSecurity
  182. class SecurityConfig {
  183. @Bean
  184. fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
  185. http {
  186. authorizeHttpRequests {
  187. authorize(anyRequest, authenticated)
  188. }
  189. oauth2ResourceServer {
  190. opaqueToken { }
  191. }
  192. }
  193. return http.build()
  194. }
  195. @Bean
  196. fun opaqueTokenIntrospector(): OpaqueTokenIntrospector {
  197. return SpringOpaqueTokenIntrospector(
  198. "https://my-auth-server.com/oauth2/introspect", "my-client-id", "my-client-secret"
  199. )
  200. }
  201. }
  202. ----
  203. =====
  204. [[oauth2-resource-server-custom-jwt]]
  205. === Protect Access with a custom JWT
  206. It is a fairly common goal to protect access to an API using JWTs, particularly when the frontend is developed as a single-page application.
  207. The OAuth2 Resource Server support in Spring Security can be used for any type of `Bearer` token, including a custom JWT.
  208. All that is required to protect an API using JWTs is a `JwtDecoder` bean, which is used to validate signatures and decode tokens.
  209. Spring Security will automatically use the provided bean to configure protection within the `SecurityFilterChain`.
  210. The following example configures a `JwtDecoder` bean using Spring Boot configuration properties:
  211. [source,yaml]
  212. ----
  213. spring:
  214. security:
  215. oauth2:
  216. resourceserver:
  217. jwt:
  218. public-key-location: classpath:my-public-key.pub
  219. ----
  220. [NOTE]
  221. ====
  222. You can provide the public key as a classpath resource (called `my-public-key.pub` in this example).
  223. ====
  224. When using Spring Boot, this is all that is required.
  225. The default arrangement provided by Spring Boot is equivalent to the following:
  226. .Configure Resource Server with Custom JWTs
  227. [tabs]
  228. =====
  229. Java::
  230. +
  231. [source,java,role="primary"]
  232. ----
  233. @Configuration
  234. @EnableWebSecurity
  235. public class SecurityConfig {
  236. @Bean
  237. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  238. http
  239. .authorizeHttpRequests((authorize) -> authorize
  240. .anyRequest().authenticated()
  241. )
  242. .oauth2ResourceServer((oauth2) -> oauth2
  243. .jwt(Customizer.withDefaults())
  244. );
  245. return http.build();
  246. }
  247. @Bean
  248. public JwtDecoder jwtDecoder() {
  249. return NimbusJwtDecoder.withPublicKey(publicKey()).build();
  250. }
  251. private RSAPublicKey publicKey() {
  252. // ...
  253. }
  254. }
  255. ----
  256. Kotlin::
  257. +
  258. [source,kotlin,role="secondary"]
  259. ----
  260. import org.springframework.security.config.annotation.web.invoke
  261. @Configuration
  262. @EnableWebSecurity
  263. class SecurityConfig {
  264. @Bean
  265. fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
  266. http {
  267. authorizeHttpRequests {
  268. authorize(anyRequest, authenticated)
  269. }
  270. oauth2ResourceServer {
  271. jwt { }
  272. }
  273. }
  274. return http.build()
  275. }
  276. @Bean
  277. fun jwtDecoder(): JwtDecoder {
  278. return NimbusJwtDecoder.withPublicKey(publicKey()).build()
  279. }
  280. private fun publicKey(): RSAPublicKey {
  281. // ...
  282. }
  283. }
  284. ----
  285. =====
  286. [NOTE]
  287. ====
  288. Spring Security does not provide an endpoint for minting tokens.
  289. However, Spring Security does provide the `JwtEncoder` interface along with one implementation, which is `NimbusJwtEncoder`.
  290. ====
  291. [[oauth2-client]]
  292. == OAuth2 Client
  293. [NOTE]
  294. ====
  295. This section contains a summary of OAuth2 Client features with examples.
  296. See xref:servlet/oauth2/client/index.adoc[OAuth 2.0 Client] and xref:servlet/oauth2/login/index.adoc[OAuth 2.0 Login] for complete reference documentation.
  297. ====
  298. To get started, add the `spring-security-oauth2-client` dependency to your project.
  299. When using Spring Boot, add the following starter:
  300. .OAuth2 Client with Spring Boot
  301. [tabs]
  302. ======
  303. Gradle::
  304. +
  305. [source,gradle,role="primary"]
  306. ----
  307. implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
  308. ----
  309. Maven::
  310. +
  311. [source,maven,role="secondary"]
  312. ----
  313. <dependency>
  314. <groupId>org.springframework.boot</groupId>
  315. <artifactId>spring-boot-starter-oauth2-client</artifactId>
  316. </dependency>
  317. ----
  318. ======
  319. [TIP]
  320. ====
  321. See xref:getting-spring-security.adoc[] for additional options when not using Spring Boot.
  322. ====
  323. Consider the following use cases for OAuth2 Client:
  324. * I want to <<oauth2-client-log-users-in,log users in using OAuth 2.0 or OpenID Connect 1.0>>
  325. * I want to <<oauth2-client-access-protected-resources,use `RestClient` to obtain an access token for users in order to access a third-party API>>
  326. * I want to <<oauth2-client-access-protected-resources-current-user,do both>> (log users in _and_ access a third-party API)
  327. * I want to <<oauth2-client-access-protected-resources-webclient,use `WebClient` to obtain an access token for users in order to access a third-party API>>
  328. * I want to <<oauth2-client-enable-extension-grant-type,enable an extension grant type>>
  329. * I want to <<oauth2-client-customize-existing-grant-type,customize an existing grant type>>
  330. * I want to <<oauth2-client-customize-request-parameters,customize token request parameters>>
  331. * I want to <<oauth2-client-customize-rest-operations,customize the `RestOperations` used by OAuth2 Client components>>
  332. [[oauth2-client-log-users-in]]
  333. === Log Users In with OAuth2
  334. It is very common to require users to log in via OAuth2.
  335. https://openid.net/specs/openid-connect-core-1_0.html[OpenID Connect 1.0] provides a special token called the `id_token` which is designed to provide an OAuth2 Client with the ability to perform user identity verification and log users in.
  336. In certain cases, OAuth2 can be used directly to log users in (as is the case with popular social login providers that do not implement OpenID Connect such as GitHub and Facebook).
  337. The following example configures the application to act as an OAuth2 Client capable of logging users in with OAuth2 or OpenID Connect:
  338. .Configure OAuth2 Login
  339. [tabs]
  340. =====
  341. Java::
  342. +
  343. [source,java,role="primary"]
  344. ----
  345. @Configuration
  346. @EnableWebSecurity
  347. public class SecurityConfig {
  348. @Bean
  349. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  350. http
  351. // ...
  352. .oauth2Login(Customizer.withDefaults());
  353. return http.build();
  354. }
  355. }
  356. ----
  357. Kotlin::
  358. +
  359. [source,kotlin,role="secondary"]
  360. ----
  361. import org.springframework.security.config.annotation.web.invoke
  362. @Configuration
  363. @EnableWebSecurity
  364. class SecurityConfig {
  365. @Bean
  366. fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
  367. http {
  368. // ...
  369. oauth2Login { }
  370. }
  371. return http.build()
  372. }
  373. }
  374. ----
  375. =====
  376. In addition to the above configuration, the application requires at least one `ClientRegistration` to be configured through the use of a `ClientRegistrationRepository` bean.
  377. The following example configures an `InMemoryClientRegistrationRepository` bean using Spring Boot configuration properties:
  378. [source,yaml]
  379. ----
  380. spring:
  381. security:
  382. oauth2:
  383. client:
  384. registration:
  385. my-oidc-client:
  386. provider: my-oidc-provider
  387. client-id: my-client-id
  388. client-secret: my-client-secret
  389. authorization-grant-type: authorization_code
  390. scope: openid,profile
  391. provider:
  392. my-oidc-provider:
  393. issuer-uri: https://my-oidc-provider.com
  394. ----
  395. With the above configuration, the application now supports two additional endpoints:
  396. 1. The login endpoint (e.g. `/oauth2/authorization/my-oidc-client`) is used to initiate login and perform a redirect to the third party authorization server.
  397. 2. The redirection endpoint (e.g. `/login/oauth2/code/my-oidc-client`) is used by the authorization server to redirect back to the client application, and will contain a `code` parameter used to obtain an `id_token` and/or `access_token` via the access token request.
  398. [NOTE]
  399. ====
  400. The presence of the `openid` scope in the above configuration indicates that OpenID Connect 1.0 should be used.
  401. This instructs Spring Security to use OIDC-specific components (such as `OidcUserService`) during request processing.
  402. Without this scope, Spring Security will use OAuth2-specific components (such as `DefaultOAuth2UserService`) instead.
  403. ====
  404. [[oauth2-client-access-protected-resources]]
  405. === Access Protected Resources
  406. Making requests to a third party API that is protected by OAuth2 is a core use case of OAuth2 Client.
  407. This is accomplished by authorizing a client (represented by the `OAuth2AuthorizedClient` class in Spring Security) and accessing protected resources by placing a `Bearer` token in the `Authorization` header of an outbound request.
  408. The following example configures the application to act as an OAuth2 Client capable of requesting protected resources from a third party API:
  409. .Configure OAuth2 Client
  410. [tabs]
  411. =====
  412. Java::
  413. +
  414. [source,java,role="primary"]
  415. ----
  416. @Configuration
  417. @EnableWebSecurity
  418. public class SecurityConfig {
  419. @Bean
  420. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  421. http
  422. // ...
  423. .oauth2Client(Customizer.withDefaults());
  424. return http.build();
  425. }
  426. }
  427. ----
  428. Kotlin::
  429. +
  430. [source,kotlin,role="secondary"]
  431. ----
  432. import org.springframework.security.config.annotation.web.invoke
  433. @Configuration
  434. @EnableWebSecurity
  435. class SecurityConfig {
  436. @Bean
  437. fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
  438. http {
  439. // ...
  440. oauth2Client { }
  441. }
  442. return http.build()
  443. }
  444. }
  445. ----
  446. =====
  447. [NOTE]
  448. ====
  449. The above example does not provide a way to log users in.
  450. You can use any other login mechanism (such as `formLogin()`).
  451. See the <<oauth2-client-access-protected-resources-current-user,next section>> for an example combining `oauth2Client()` with `oauth2Login()`.
  452. ====
  453. In addition to the above configuration, the application requires at least one `ClientRegistration` to be configured through the use of a `ClientRegistrationRepository` bean.
  454. The following example configures an `InMemoryClientRegistrationRepository` bean using Spring Boot configuration properties:
  455. [source,yaml]
  456. ----
  457. spring:
  458. security:
  459. oauth2:
  460. client:
  461. registration:
  462. my-oauth2-client:
  463. provider: my-auth-server
  464. client-id: my-client-id
  465. client-secret: my-client-secret
  466. authorization-grant-type: authorization_code
  467. scope: message.read,message.write
  468. provider:
  469. my-auth-server:
  470. issuer-uri: https://my-auth-server.com
  471. ----
  472. In addition to configuring Spring Security to support OAuth2 Client features, you will also need to decide how you will be accessing protected resources and configure your application accordingly.
  473. Spring Security provides implementations of `OAuth2AuthorizedClientManager` for obtaining access tokens that can be used to access protected resources.
  474. [TIP]
  475. ====
  476. Spring Security registers a default `OAuth2AuthorizedClientManager` bean for you when one does not exist.
  477. ====
  478. The easiest way to use an `OAuth2AuthorizedClientManager` is via a `ClientHttpRequestInterceptor` that intercepts requests through a `RestClient`, which is already available when `spring-web` is on the classpath.
  479. The following example uses the default `OAuth2AuthorizedClientManager` to configure a `RestClient` capable of accessing protected resources by placing `Bearer` tokens in the `Authorization` header of each request:
  480. .Configure `RestClient` with `ClientHttpRequestInterceptor`
  481. [tabs]
  482. =====
  483. Java::
  484. +
  485. [source,java,role="primary"]
  486. ----
  487. @Configuration
  488. public class RestClientConfig {
  489. @Bean
  490. public RestClient restClient(OAuth2AuthorizedClientManager authorizedClientManager) {
  491. OAuth2ClientHttpRequestInterceptor requestInterceptor =
  492. new OAuth2ClientHttpRequestInterceptor(authorizedClientManager);
  493. return RestClient.builder()
  494. .requestInterceptor(requestInterceptor)
  495. .build();
  496. }
  497. }
  498. ----
  499. Kotlin::
  500. +
  501. [source,kotlin,role="secondary"]
  502. ----
  503. @Configuration
  504. class RestClientConfig {
  505. @Bean
  506. fun restClient(authorizedClientManager: OAuth2AuthorizedClientManager): RestClient {
  507. val requestInterceptor = OAuth2ClientHttpRequestInterceptor(authorizedClientManager)
  508. return RestClient.builder()
  509. .requestInterceptor(requestInterceptor)
  510. .build()
  511. }
  512. }
  513. ----
  514. =====
  515. This configured `RestClient` can be used as in the following example:
  516. [[oauth2-client-accessing-protected-resources-example]]
  517. .Use `RestClient` to Access Protected Resources
  518. [tabs]
  519. =====
  520. Java::
  521. +
  522. [source,java,role="primary"]
  523. ----
  524. import static org.springframework.security.oauth2.client.web.client.RequestAttributeClientRegistrationIdResolver.clientRegistrationId;
  525. @RestController
  526. public class MessagesController {
  527. private final RestClient restClient;
  528. public MessagesController(RestClient restClient) {
  529. this.restClient = restClient;
  530. }
  531. @GetMapping("/messages")
  532. public ResponseEntity<List<Message>> messages() {
  533. Message[] messages = this.restClient.get()
  534. .uri("http://localhost:8090/messages")
  535. .attributes(clientRegistrationId("my-oauth2-client"))
  536. .retrieve()
  537. .body(Message[].class);
  538. return ResponseEntity.ok(Arrays.asList(messages));
  539. }
  540. public record Message(String message) {
  541. }
  542. }
  543. ----
  544. Kotlin::
  545. +
  546. [source,kotlin,role="secondary"]
  547. ----
  548. import org.springframework.security.oauth2.client.web.client.RequestAttributeClientRegistrationIdResolver.clientRegistrationId
  549. import org.springframework.web.client.body
  550. @RestController
  551. class MessagesController(private val restClient: RestClient) {
  552. @GetMapping("/messages")
  553. fun messages(): ResponseEntity<List<Message>> {
  554. val messages = restClient.get()
  555. .uri("http://localhost:8090/messages")
  556. .attributes(clientRegistrationId("my-oauth2-client"))
  557. .retrieve()
  558. .body<Array<Message>>()!!
  559. .toList()
  560. return ResponseEntity.ok(messages)
  561. }
  562. data class Message(val message: String)
  563. }
  564. ----
  565. =====
  566. [[oauth2-client-access-protected-resources-current-user]]
  567. === Access Protected Resources for the Current User
  568. When a user is logged in via OAuth2 or OpenID Connect, the authorization server may provide an access token that can be used directly to access protected resources.
  569. This is convenient because it only requires a single `ClientRegistration` to be configured for both use cases simultaneously.
  570. [NOTE]
  571. ====
  572. This section combines <<oauth2-client-log-users-in>> and <<oauth2-client-access-protected-resources>> into a single configuration.
  573. Other advanced scenarios exist, such as configuring one `ClientRegistration` for login and another for accessing protected resources.
  574. All such scenarios would use the same basic configuration.
  575. ====
  576. The following example configures the application to act as an OAuth2 Client capable of logging the user in _and_ requesting protected resources from a third party API:
  577. .Configure OAuth2 Login and OAuth2 Client
  578. [tabs]
  579. =====
  580. Java::
  581. +
  582. [source,java,role="primary"]
  583. ----
  584. @Configuration
  585. @EnableWebSecurity
  586. public class SecurityConfig {
  587. @Bean
  588. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  589. http
  590. // ...
  591. .oauth2Login(Customizer.withDefaults())
  592. .oauth2Client(Customizer.withDefaults());
  593. return http.build();
  594. }
  595. }
  596. ----
  597. Kotlin::
  598. +
  599. [source,kotlin,role="secondary"]
  600. ----
  601. import org.springframework.security.config.annotation.web.invoke
  602. @Configuration
  603. @EnableWebSecurity
  604. class SecurityConfig {
  605. @Bean
  606. fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
  607. http {
  608. // ...
  609. oauth2Login { }
  610. oauth2Client { }
  611. }
  612. return http.build()
  613. }
  614. }
  615. ----
  616. =====
  617. In addition to the above configuration, the application requires at least one `ClientRegistration` to be configured through the use of a `ClientRegistrationRepository` bean.
  618. The following example configures an `InMemoryClientRegistrationRepository` bean using Spring Boot configuration properties:
  619. [source,yaml]
  620. ----
  621. spring:
  622. security:
  623. oauth2:
  624. client:
  625. registration:
  626. my-combined-client:
  627. provider: my-auth-server
  628. client-id: my-client-id
  629. client-secret: my-client-secret
  630. authorization-grant-type: authorization_code
  631. scope: openid,profile,message.read,message.write
  632. provider:
  633. my-auth-server:
  634. issuer-uri: https://my-auth-server.com
  635. ----
  636. [NOTE]
  637. ====
  638. The main difference between the previous examples (<<oauth2-client-log-users-in>>, <<oauth2-client-access-protected-resources>>) and this one is what is configured via the `scope` property, which combines the standard scopes `openid` and `profile` with the custom scopes `message.read` and `message.write`.
  639. ====
  640. In addition to configuring Spring Security to support OAuth2 Client features, you will also need to decide how you will be accessing protected resources and configure your application accordingly.
  641. Spring Security provides implementations of `OAuth2AuthorizedClientManager` for obtaining access tokens that can be used to access protected resources.
  642. [TIP]
  643. ====
  644. Spring Security registers a default `OAuth2AuthorizedClientManager` bean for you when one does not exist.
  645. ====
  646. The easiest way to use an `OAuth2AuthorizedClientManager` is via a `ClientHttpRequestInterceptor` that intercepts requests through a `RestClient`, which is already available when `spring-web` is on the classpath.
  647. The following example uses the default `OAuth2AuthorizedClientManager` to configure a `RestClient` capable of accessing protected resources by placing `Bearer` tokens in the `Authorization` header of each request:
  648. .Configure `RestClient` with `ClientHttpRequestInterceptor`
  649. [tabs]
  650. =====
  651. Java::
  652. +
  653. [source,java,role="primary"]
  654. ----
  655. @Configuration
  656. public class RestClientConfig {
  657. @Bean
  658. public RestClient restClient(OAuth2AuthorizedClientManager authorizedClientManager) {
  659. OAuth2ClientHttpRequestInterceptor requestInterceptor =
  660. new OAuth2ClientHttpRequestInterceptor(authorizedClientManager, clientRegistrationIdResolver());
  661. return RestClient.builder()
  662. .requestInterceptor(requestInterceptor)
  663. .build();
  664. }
  665. private static ClientRegistrationIdResolver clientRegistrationIdResolver() {
  666. return (request) -> {
  667. Authentication authentication = SecurityContextHolder.getAuthentication();
  668. return (authentication instanceof OAuth2AuthenticationToken principal)
  669. ? principal.getAuthorizedClientRegistrationId()
  670. : null;
  671. };
  672. }
  673. }
  674. ----
  675. Kotlin::
  676. +
  677. [source,kotlin,role="secondary"]
  678. ----
  679. @Configuration
  680. class RestClientConfig {
  681. @Bean
  682. fun restClient(authorizedClientManager: OAuth2AuthorizedClientManager): RestClient {
  683. val requestInterceptor = OAuth2ClientHttpRequestInterceptor(authorizedClientManager, clientRegistrationIdResolver())
  684. return RestClient.builder()
  685. .requestInterceptor(requestInterceptor)
  686. .build()
  687. }
  688. private fun clientRegistrationIdResolver(): OAuth2ClientHttpRequestInterceptor.ClientRegistrationIdResolver {
  689. return OAuth2ClientHttpRequestInterceptor.ClientRegistrationIdResolver { request ->
  690. val authentication = SecurityContextHolder.getContext().authentication
  691. if (authentication is OAuth2AuthenticationToken) {
  692. authentication.authorizedClientRegistrationId
  693. } else {
  694. null
  695. }
  696. }
  697. }
  698. }
  699. ----
  700. =====
  701. This configured `RestClient` can be used as in the following example:
  702. [[oauth2-client-accessing-protected-resources-current-user-example]]
  703. .Use `RestClient` to Access Protected Resources (Current User)
  704. [tabs]
  705. =====
  706. Java::
  707. +
  708. [source,java,role="primary"]
  709. ----
  710. @RestController
  711. public class MessagesController {
  712. private final RestClient restClient;
  713. public MessagesController(RestClient restClient) {
  714. this.restClient = restClient;
  715. }
  716. @GetMapping("/messages")
  717. public ResponseEntity<List<Message>> messages() {
  718. Message[] messages = this.restClient.get()
  719. .uri("http://localhost:8090/messages")
  720. .retrieve()
  721. .body(Message[].class);
  722. return ResponseEntity.ok(Arrays.asList(messages));
  723. }
  724. public record Message(String message) {
  725. }
  726. }
  727. ----
  728. Kotlin::
  729. +
  730. [source,kotlin,role="secondary"]
  731. ----
  732. import org.springframework.web.client.body
  733. @RestController
  734. class MessagesController(private val restClient: RestClient) {
  735. @GetMapping("/messages")
  736. fun messages(): ResponseEntity<List<Message>> {
  737. val messages = restClient.get()
  738. .uri("http://localhost:8090/messages")
  739. .retrieve()
  740. .body<Array<Message>>()!!
  741. .toList()
  742. return ResponseEntity.ok(messages)
  743. }
  744. data class Message(val message: String)
  745. }
  746. ----
  747. =====
  748. [NOTE]
  749. ====
  750. Unlike the <<oauth2-client-accessing-protected-resources-example,previous example>>, notice that we do not need to tell Spring Security about the `clientRegistrationId` we'd like to use.
  751. This is because it can be derived from the currently logged in user.
  752. ====
  753. [[oauth2-client-access-protected-resources-webclient]]
  754. === Access Protected Resources with `WebClient`
  755. Making requests to a third party API that is protected by OAuth2 is a core use case of OAuth2 Client.
  756. This is accomplished by authorizing a client (represented by the `OAuth2AuthorizedClient` class in Spring Security) and accessing protected resources by placing a `Bearer` token in the `Authorization` header of an outbound request.
  757. The following example configures the application to act as an OAuth2 Client capable of requesting protected resources from a third party API:
  758. .Configure OAuth2 Client
  759. [tabs]
  760. =====
  761. Java::
  762. +
  763. [source,java,role="primary"]
  764. ----
  765. @Configuration
  766. @EnableWebSecurity
  767. public class SecurityConfig {
  768. @Bean
  769. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  770. http
  771. // ...
  772. .oauth2Client(Customizer.withDefaults());
  773. return http.build();
  774. }
  775. }
  776. ----
  777. Kotlin::
  778. +
  779. [source,kotlin,role="secondary"]
  780. ----
  781. import org.springframework.security.config.annotation.web.invoke
  782. @Configuration
  783. @EnableWebSecurity
  784. class SecurityConfig {
  785. @Bean
  786. fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
  787. http {
  788. // ...
  789. oauth2Client { }
  790. }
  791. return http.build()
  792. }
  793. }
  794. ----
  795. =====
  796. [NOTE]
  797. ====
  798. The above example does not provide a way to log users in.
  799. You can use any other login mechanism (such as `formLogin()`).
  800. See the <<oauth2-client-access-protected-resources-current-user,previous section>> for an example combining `oauth2Client()` with `oauth2Login()`.
  801. ====
  802. In addition to the above configuration, the application requires at least one `ClientRegistration` to be configured through the use of a `ClientRegistrationRepository` bean.
  803. The following example configures an `InMemoryClientRegistrationRepository` bean using Spring Boot configuration properties:
  804. [source,yaml]
  805. ----
  806. spring:
  807. security:
  808. oauth2:
  809. client:
  810. registration:
  811. my-oauth2-client:
  812. provider: my-auth-server
  813. client-id: my-client-id
  814. client-secret: my-client-secret
  815. authorization-grant-type: authorization_code
  816. scope: message.read,message.write
  817. provider:
  818. my-auth-server:
  819. issuer-uri: https://my-auth-server.com
  820. ----
  821. In addition to configuring Spring Security to support OAuth2 Client features, you will also need to decide how you will be accessing protected resources and configure your application accordingly.
  822. Spring Security provides implementations of `OAuth2AuthorizedClientManager` for obtaining access tokens that can be used to access protected resources.
  823. [TIP]
  824. ====
  825. Spring Security registers a default `OAuth2AuthorizedClientManager` bean for you when one does not exist.
  826. ====
  827. Another way to use an `OAuth2AuthorizedClientManager` is via an `ExchangeFilterFunction` that intercepts requests through a `WebClient`.
  828. To use `WebClient`, you will need to add the `spring-webflux` dependency along with a reactive client implementation:
  829. .Add Spring WebFlux Dependency
  830. [tabs]
  831. ======
  832. Gradle::
  833. +
  834. [source,gradle,role="primary"]
  835. ----
  836. implementation 'org.springframework:spring-webflux'
  837. implementation 'io.projectreactor.netty:reactor-netty'
  838. ----
  839. Maven::
  840. +
  841. [source,maven,role="secondary"]
  842. ----
  843. <dependency>
  844. <groupId>org.springframework</groupId>
  845. <artifactId>spring-webflux</artifactId>
  846. </dependency>
  847. <dependency>
  848. <groupId>io.projectreactor.netty</groupId>
  849. <artifactId>reactor-netty</artifactId>
  850. </dependency>
  851. ----
  852. ======
  853. The following example uses the default `OAuth2AuthorizedClientManager` to configure a `WebClient` capable of accessing protected resources by placing `Bearer` tokens in the `Authorization` header of each request:
  854. .Configure `WebClient` with `ExchangeFilterFunction`
  855. [tabs]
  856. =====
  857. Java::
  858. +
  859. [source,java,role="primary"]
  860. ----
  861. @Configuration
  862. public class WebClientConfig {
  863. @Bean
  864. public WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
  865. ServletOAuth2AuthorizedClientExchangeFilterFunction filter =
  866. new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
  867. return WebClient.builder()
  868. .apply(filter.oauth2Configuration())
  869. .build();
  870. }
  871. }
  872. ----
  873. Kotlin::
  874. +
  875. [source,kotlin,role="secondary"]
  876. ----
  877. @Configuration
  878. class WebClientConfig {
  879. @Bean
  880. fun webClient(authorizedClientManager: OAuth2AuthorizedClientManager): WebClient {
  881. val filter = ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager)
  882. return WebClient.builder()
  883. .apply(filter.oauth2Configuration())
  884. .build()
  885. }
  886. }
  887. ----
  888. =====
  889. This configured `WebClient` can be used as in the following example:
  890. .Use `WebClient` to Access Protected Resources
  891. [tabs]
  892. =====
  893. Java::
  894. +
  895. [source,java,role="primary"]
  896. ----
  897. import static org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction.clientRegistrationId;
  898. @RestController
  899. public class MessagesController {
  900. private final WebClient webClient;
  901. public MessagesController(WebClient webClient) {
  902. this.webClient = webClient;
  903. }
  904. @GetMapping("/messages")
  905. public ResponseEntity<List<Message>> messages() {
  906. return this.webClient.get()
  907. .uri("http://localhost:8090/messages")
  908. .attributes(clientRegistrationId("my-oauth2-client"))
  909. .retrieve()
  910. .toEntityList(Message.class)
  911. .block();
  912. }
  913. public record Message(String message) {
  914. }
  915. }
  916. ----
  917. Kotlin::
  918. +
  919. [source,kotlin,role="secondary"]
  920. ----
  921. import org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction.clientRegistrationId
  922. @RestController
  923. class MessagesController(private val webClient: WebClient) {
  924. @GetMapping("/messages")
  925. fun messages(): ResponseEntity<List<Message>> {
  926. return webClient.get()
  927. .uri("http://localhost:8090/messages")
  928. .attributes(clientRegistrationId("my-oauth2-client"))
  929. .retrieve()
  930. .toEntityList<Message>()
  931. .block()!!
  932. }
  933. data class Message(val message: String)
  934. }
  935. ----
  936. =====
  937. [[oauth2-client-enable-extension-grant-type]]
  938. === Enable an Extension Grant Type
  939. A common use case involves enabling and/or configuring an extension grant type.
  940. For example, Spring Security provides support for the `jwt-bearer` and `token-exchange` grant types, but does not enable them by default because they are not part of the core OAuth 2.0 specification.
  941. With Spring Security 6.2 and later, we can simply publish a bean for one or more `OAuth2AuthorizedClientProvider` and they will be picked up automatically.
  942. The following example simply enables the `jwt-bearer` grant type:
  943. .Enable `jwt-bearer` Grant Type
  944. [tabs]
  945. =====
  946. Java::
  947. +
  948. [source,java,role="primary"]
  949. ----
  950. @Configuration
  951. public class SecurityConfig {
  952. @Bean
  953. public OAuth2AuthorizedClientProvider jwtBearer() {
  954. return new JwtBearerOAuth2AuthorizedClientProvider();
  955. }
  956. }
  957. ----
  958. Kotlin::
  959. +
  960. [source,kotlin,role="secondary"]
  961. ----
  962. @Configuration
  963. class SecurityConfig {
  964. @Bean
  965. fun jwtBearer(): OAuth2AuthorizedClientProvider {
  966. return JwtBearerOAuth2AuthorizedClientProvider()
  967. }
  968. }
  969. ----
  970. =====
  971. A default `OAuth2AuthorizedClientManager` will be published automatically by Spring Security when one is not already provided.
  972. [TIP]
  973. ====
  974. Any custom `OAuth2AuthorizedClientProvider` bean will also be picked up and applied to the provided `OAuth2AuthorizedClientManager` after the default grant types.
  975. ====
  976. In order to achieve the above configuration prior to Spring Security 6.2, we had to publish this bean ourselves and ensure we re-enabled default grant types as well.
  977. To understand what is being configured behind the scenes, here's what the configuration might have looked like:
  978. .Enable `jwt-bearer` Grant Type (prior to 6.2)
  979. [tabs]
  980. =====
  981. Java::
  982. +
  983. [source,java,role="primary"]
  984. ----
  985. @Configuration
  986. public class SecurityConfig {
  987. @Bean
  988. public OAuth2AuthorizedClientManager authorizedClientManager(
  989. ClientRegistrationRepository clientRegistrationRepository,
  990. OAuth2AuthorizedClientRepository authorizedClientRepository) {
  991. OAuth2AuthorizedClientProvider authorizedClientProvider =
  992. OAuth2AuthorizedClientProviderBuilder.builder()
  993. .authorizationCode()
  994. .refreshToken()
  995. .clientCredentials()
  996. .password()
  997. .provider(new JwtBearerOAuth2AuthorizedClientProvider())
  998. .build();
  999. DefaultOAuth2AuthorizedClientManager authorizedClientManager =
  1000. new DefaultOAuth2AuthorizedClientManager(
  1001. clientRegistrationRepository, authorizedClientRepository);
  1002. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  1003. return authorizedClientManager;
  1004. }
  1005. }
  1006. ----
  1007. Kotlin::
  1008. +
  1009. [source,kotlin,role="secondary"]
  1010. ----
  1011. @Configuration
  1012. class SecurityConfig {
  1013. @Bean
  1014. fun authorizedClientManager(
  1015. clientRegistrationRepository: ClientRegistrationRepository,
  1016. authorizedClientRepository: OAuth2AuthorizedClientRepository
  1017. ): OAuth2AuthorizedClientManager {
  1018. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  1019. .authorizationCode()
  1020. .refreshToken()
  1021. .clientCredentials()
  1022. .password()
  1023. .provider(JwtBearerOAuth2AuthorizedClientProvider())
  1024. .build()
  1025. val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
  1026. clientRegistrationRepository, authorizedClientRepository
  1027. )
  1028. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  1029. return authorizedClientManager
  1030. }
  1031. }
  1032. ----
  1033. =====
  1034. [[oauth2-client-customize-existing-grant-type]]
  1035. === Customize an Existing Grant Type
  1036. The ability to <<oauth2-client-enable-extension-grant-type,enable extension grant types>> by publishing a bean also provides the opportunity for customizing an existing grant type without the need to re-define the defaults.
  1037. For example, if we want to customize the clock skew of the `OAuth2AuthorizedClientProvider` for the `client_credentials` grant, we can simply publish a bean like so:
  1038. .Customize Client Credentials Grant Type
  1039. [tabs]
  1040. =====
  1041. Java::
  1042. +
  1043. [source,java,role="primary"]
  1044. ----
  1045. @Configuration
  1046. public class SecurityConfig {
  1047. @Bean
  1048. public OAuth2AuthorizedClientProvider clientCredentials() {
  1049. ClientCredentialsOAuth2AuthorizedClientProvider authorizedClientProvider =
  1050. new ClientCredentialsOAuth2AuthorizedClientProvider();
  1051. authorizedClientProvider.setClockSkew(Duration.ofMinutes(5));
  1052. return authorizedClientProvider;
  1053. }
  1054. }
  1055. ----
  1056. Kotlin::
  1057. +
  1058. [source,kotlin,role="secondary"]
  1059. ----
  1060. @Configuration
  1061. class SecurityConfig {
  1062. @Bean
  1063. fun clientCredentials(): OAuth2AuthorizedClientProvider {
  1064. val authorizedClientProvider = ClientCredentialsOAuth2AuthorizedClientProvider()
  1065. authorizedClientProvider.setClockSkew(Duration.ofMinutes(5))
  1066. return authorizedClientProvider
  1067. }
  1068. }
  1069. ----
  1070. =====
  1071. [[oauth2-client-customize-request-parameters]]
  1072. === Customize Token Request Parameters
  1073. The need to customize request parameters when obtaining an access token is fairly common.
  1074. For example, let's say we want to add a custom `audience` parameter to the token request because the provider requires this parameter for the `authorization_code` grant.
  1075. With Spring Security 6.2 and later, we can simply publish a bean of type `OAuth2AccessTokenResponseClient` with the generic type `OAuth2AuthorizationCodeGrantRequest` and it will be used by Spring Security to configure OAuth2 Client components.
  1076. The following example customizes token request parameters for the `authorization_code` grant without the DSL:
  1077. .Customize Token Request Parameters for Authorization Code Grant
  1078. [tabs]
  1079. =====
  1080. Java::
  1081. +
  1082. [source,java,role="primary"]
  1083. ----
  1084. @Configuration
  1085. public class SecurityConfig {
  1086. @Bean
  1087. public OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> authorizationCodeAccessTokenResponseClient() {
  1088. OAuth2AuthorizationCodeGrantRequestEntityConverter requestEntityConverter =
  1089. new OAuth2AuthorizationCodeGrantRequestEntityConverter();
  1090. requestEntityConverter.addParametersConverter(parametersConverter());
  1091. DefaultAuthorizationCodeTokenResponseClient accessTokenResponseClient =
  1092. new DefaultAuthorizationCodeTokenResponseClient();
  1093. accessTokenResponseClient.setRequestEntityConverter(requestEntityConverter);
  1094. return accessTokenResponseClient;
  1095. }
  1096. private static Converter<OAuth2AuthorizationCodeGrantRequest, MultiValueMap<String, String>> parametersConverter() {
  1097. return (grantRequest) -> {
  1098. MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
  1099. parameters.set("audience", "xyz_value");
  1100. return parameters;
  1101. };
  1102. }
  1103. }
  1104. ----
  1105. Kotlin::
  1106. +
  1107. [source,kotlin,role="secondary"]
  1108. ----
  1109. @Configuration
  1110. class SecurityConfig {
  1111. @Bean
  1112. fun authorizationCodeAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> {
  1113. val requestEntityConverter = OAuth2AuthorizationCodeGrantRequestEntityConverter()
  1114. requestEntityConverter.addParametersConverter(parametersConverter())
  1115. val accessTokenResponseClient = DefaultAuthorizationCodeTokenResponseClient()
  1116. accessTokenResponseClient.setRequestEntityConverter(requestEntityConverter)
  1117. return accessTokenResponseClient
  1118. }
  1119. private fun parametersConverter(): Converter<OAuth2AuthorizationCodeGrantRequest, MultiValueMap<String, String>> {
  1120. return Converter<OAuth2AuthorizationCodeGrantRequest, MultiValueMap<String, String>> { grantRequest ->
  1121. LinkedMultiValueMap<String, String>().also { parameters ->
  1122. parameters["audience"] = "xyz_value"
  1123. }
  1124. }
  1125. }
  1126. }
  1127. ----
  1128. =====
  1129. [TIP]
  1130. ====
  1131. Notice that we don't need to customize the `SecurityFilterChain` bean in this case, and can stick with the defaults.
  1132. If using Spring Boot with no additional customizations, we can actually omit the `SecurityFilterChain` bean entirely.
  1133. ====
  1134. Prior to Spring Security 6.2, we had to ensure that this customization was applied for both OAuth2 Login (if we are using this feature) and OAuth2 Client components using the Spring Security DSL.
  1135. To understand what is being configured behind the scenes, here's what the configuration might have looked like:
  1136. .Customize Token Request Parameters for Authorization Code Grant (prior to 6.2)
  1137. [tabs]
  1138. =====
  1139. Java::
  1140. +
  1141. [source,java,role="primary"]
  1142. ----
  1143. @Configuration
  1144. @EnableWebSecurity
  1145. public class SecurityConfig {
  1146. @Bean
  1147. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  1148. OAuth2AuthorizationCodeGrantRequestEntityConverter requestEntityConverter =
  1149. new OAuth2AuthorizationCodeGrantRequestEntityConverter();
  1150. requestEntityConverter.addParametersConverter(parametersConverter());
  1151. DefaultAuthorizationCodeTokenResponseClient accessTokenResponseClient =
  1152. new DefaultAuthorizationCodeTokenResponseClient();
  1153. accessTokenResponseClient.setRequestEntityConverter(requestEntityConverter);
  1154. http
  1155. .authorizeHttpRequests((authorize) -> authorize
  1156. .anyRequest().authenticated()
  1157. )
  1158. .oauth2Login((oauth2Login) -> oauth2Login
  1159. .tokenEndpoint((tokenEndpoint) -> tokenEndpoint
  1160. .accessTokenResponseClient(accessTokenResponseClient)
  1161. )
  1162. )
  1163. .oauth2Client((oauth2Client) -> oauth2Client
  1164. .authorizationCodeGrant((authorizationCode) -> authorizationCode
  1165. .accessTokenResponseClient(accessTokenResponseClient)
  1166. )
  1167. );
  1168. return http.build();
  1169. }
  1170. private static Converter<OAuth2AuthorizationCodeGrantRequest, MultiValueMap<String, String>> parametersConverter() {
  1171. // ...
  1172. }
  1173. }
  1174. ----
  1175. Kotlin::
  1176. +
  1177. [source,kotlin,role="secondary"]
  1178. ----
  1179. import org.springframework.security.config.annotation.web.invoke
  1180. @Configuration
  1181. @EnableWebSecurity
  1182. class SecurityConfig {
  1183. @Bean
  1184. fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
  1185. val requestEntityConverter = OAuth2AuthorizationCodeGrantRequestEntityConverter()
  1186. requestEntityConverter.addParametersConverter(parametersConverter())
  1187. val tokenResponseClient = DefaultAuthorizationCodeTokenResponseClient()
  1188. tokenResponseClient.setRequestEntityConverter(requestEntityConverter)
  1189. http {
  1190. authorizeHttpRequests {
  1191. authorize(anyRequest, authenticated)
  1192. }
  1193. oauth2Login {
  1194. tokenEndpoint {
  1195. accessTokenResponseClient = tokenResponseClient
  1196. }
  1197. }
  1198. oauth2Client {
  1199. authorizationCodeGrant {
  1200. accessTokenResponseClient = tokenResponseClient
  1201. }
  1202. }
  1203. }
  1204. return http.build()
  1205. }
  1206. private fun parametersConverter(): Converter<OAuth2AuthorizationCodeGrantRequest, MultiValueMap<String, String>> {
  1207. // ...
  1208. }
  1209. }
  1210. ----
  1211. =====
  1212. For other grant types we can publish additional `OAuth2AccessTokenResponseClient` beans to override the defaults.
  1213. For example, to customize token requests for the `client_credentials` grant we can publish the following bean:
  1214. .Customize Token Request Parameters for Client Credentials Grant
  1215. [tabs]
  1216. =====
  1217. Java::
  1218. +
  1219. [source,java,role="primary"]
  1220. ----
  1221. @Configuration
  1222. public class SecurityConfig {
  1223. @Bean
  1224. public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsAccessTokenResponseClient() {
  1225. OAuth2ClientCredentialsGrantRequestEntityConverter requestEntityConverter =
  1226. new OAuth2ClientCredentialsGrantRequestEntityConverter();
  1227. requestEntityConverter.addParametersConverter(parametersConverter());
  1228. DefaultClientCredentialsTokenResponseClient accessTokenResponseClient =
  1229. new DefaultClientCredentialsTokenResponseClient();
  1230. accessTokenResponseClient.setRequestEntityConverter(requestEntityConverter);
  1231. return accessTokenResponseClient;
  1232. }
  1233. private static Converter<OAuth2ClientCredentialsGrantRequest, MultiValueMap<String, String>> parametersConverter() {
  1234. // ...
  1235. }
  1236. }
  1237. ----
  1238. Kotlin::
  1239. +
  1240. [source,kotlin,role="secondary"]
  1241. ----
  1242. @Configuration
  1243. class SecurityConfig {
  1244. @Bean
  1245. fun clientCredentialsAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> {
  1246. val requestEntityConverter = OAuth2ClientCredentialsGrantRequestEntityConverter()
  1247. requestEntityConverter.addParametersConverter(parametersConverter())
  1248. val accessTokenResponseClient = DefaultClientCredentialsTokenResponseClient()
  1249. accessTokenResponseClient.setRequestEntityConverter(requestEntityConverter)
  1250. return accessTokenResponseClient
  1251. }
  1252. private fun parametersConverter(): Converter<OAuth2ClientCredentialsGrantRequest, MultiValueMap<String, String>> {
  1253. // ...
  1254. }
  1255. }
  1256. ----
  1257. =====
  1258. Spring Security automatically resolves the following generic types of `OAuth2AccessTokenResponseClient` beans:
  1259. * `OAuth2AuthorizationCodeGrantRequest` (see `DefaultAuthorizationCodeTokenResponseClient`)
  1260. * `OAuth2RefreshTokenGrantRequest` (see `DefaultRefreshTokenTokenResponseClient`)
  1261. * `OAuth2ClientCredentialsGrantRequest` (see `DefaultClientCredentialsTokenResponseClient`)
  1262. * `OAuth2PasswordGrantRequest` (see `DefaultPasswordTokenResponseClient`)
  1263. * `JwtBearerGrantRequest` (see `DefaultJwtBearerTokenResponseClient`)
  1264. * `TokenExchangeGrantRequest` (see `DefaultTokenExchangeTokenResponseClient`)
  1265. [TIP]
  1266. ====
  1267. Publishing a bean of type `OAuth2AccessTokenResponseClient<JwtBearerGrantRequest>` will automatically enable the `jwt-bearer` grant type without the need to <<oauth2-client-enable-extension-grant-type,configure it separately>>.
  1268. ====
  1269. [TIP]
  1270. ====
  1271. Publishing a bean of type `OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest>` will automatically enable the `token-exchange` grant type without the need to <<oauth2-client-enable-extension-grant-type,configure it separately>>.
  1272. ====
  1273. [[oauth2-client-customize-rest-operations]]
  1274. === Customize the `RestOperations` used by OAuth2 Client Components
  1275. Another common use case is the need to customize the `RestOperations` used when obtaining an access token.
  1276. We might need to do this to customize processing of the response (via a custom `HttpMessageConverter`) or to apply proxy settings for a corporate network (via a customized `ClientHttpRequestFactory`).
  1277. With Spring Security 6.2 and later, we can simply publish beans of type `OAuth2AccessTokenResponseClient` and Spring Security will configure and publish an `OAuth2AuthorizedClientManager` bean for us.
  1278. The following example customizes the `RestOperations` for all of the supported grant types:
  1279. .Customize `RestOperations` for OAuth2 Client
  1280. [tabs]
  1281. =====
  1282. Java::
  1283. +
  1284. [source,java,role="primary"]
  1285. ----
  1286. @Configuration
  1287. public class SecurityConfig {
  1288. @Bean
  1289. public OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> authorizationCodeAccessTokenResponseClient() {
  1290. DefaultAuthorizationCodeTokenResponseClient accessTokenResponseClient =
  1291. new DefaultAuthorizationCodeTokenResponseClient();
  1292. accessTokenResponseClient.setRestOperations(restTemplate());
  1293. return accessTokenResponseClient;
  1294. }
  1295. @Bean
  1296. public OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> refreshTokenAccessTokenResponseClient() {
  1297. DefaultRefreshTokenTokenResponseClient accessTokenResponseClient =
  1298. new DefaultRefreshTokenTokenResponseClient();
  1299. accessTokenResponseClient.setRestOperations(restTemplate());
  1300. return accessTokenResponseClient;
  1301. }
  1302. @Bean
  1303. public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsAccessTokenResponseClient() {
  1304. DefaultClientCredentialsTokenResponseClient accessTokenResponseClient =
  1305. new DefaultClientCredentialsTokenResponseClient();
  1306. accessTokenResponseClient.setRestOperations(restTemplate());
  1307. return accessTokenResponseClient;
  1308. }
  1309. @Bean
  1310. public OAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> passwordAccessTokenResponseClient() {
  1311. DefaultPasswordTokenResponseClient accessTokenResponseClient =
  1312. new DefaultPasswordTokenResponseClient();
  1313. accessTokenResponseClient.setRestOperations(restTemplate());
  1314. return accessTokenResponseClient;
  1315. }
  1316. @Bean
  1317. public OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> jwtBearerAccessTokenResponseClient() {
  1318. DefaultJwtBearerTokenResponseClient accessTokenResponseClient =
  1319. new DefaultJwtBearerTokenResponseClient();
  1320. accessTokenResponseClient.setRestOperations(restTemplate());
  1321. return accessTokenResponseClient;
  1322. }
  1323. @Bean
  1324. public OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> tokenExchangeAccessTokenResponseClient() {
  1325. DefaultTokenExchangeTokenResponseClient accessTokenResponseClient =
  1326. new DefaultTokenExchangeTokenResponseClient();
  1327. accessTokenResponseClient.setRestOperations(restTemplate());
  1328. return accessTokenResponseClient;
  1329. }
  1330. @Bean
  1331. public RestTemplate restTemplate() {
  1332. // ...
  1333. }
  1334. }
  1335. ----
  1336. Kotlin::
  1337. +
  1338. [source,kotlin,role="secondary"]
  1339. ----
  1340. @Configuration
  1341. class SecurityConfig {
  1342. @Bean
  1343. fun authorizationCodeAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> {
  1344. val accessTokenResponseClient = DefaultAuthorizationCodeTokenResponseClient()
  1345. accessTokenResponseClient.setRestOperations(restTemplate())
  1346. return accessTokenResponseClient
  1347. }
  1348. @Bean
  1349. fun refreshTokenAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> {
  1350. val accessTokenResponseClient = DefaultRefreshTokenTokenResponseClient()
  1351. accessTokenResponseClient.setRestOperations(restTemplate())
  1352. return accessTokenResponseClient
  1353. }
  1354. @Bean
  1355. fun clientCredentialsAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> {
  1356. val accessTokenResponseClient = DefaultClientCredentialsTokenResponseClient()
  1357. accessTokenResponseClient.setRestOperations(restTemplate())
  1358. return accessTokenResponseClient
  1359. }
  1360. @Bean
  1361. fun passwordAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> {
  1362. val accessTokenResponseClient = DefaultPasswordTokenResponseClient()
  1363. accessTokenResponseClient.setRestOperations(restTemplate())
  1364. return accessTokenResponseClient
  1365. }
  1366. @Bean
  1367. fun jwtBearerAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> {
  1368. val accessTokenResponseClient = DefaultJwtBearerTokenResponseClient()
  1369. accessTokenResponseClient.setRestOperations(restTemplate())
  1370. return accessTokenResponseClient
  1371. }
  1372. @Bean
  1373. fun tokenExchangeAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> {
  1374. val accessTokenResponseClient = DefaultTokenExchangeTokenResponseClient()
  1375. accessTokenResponseClient.setRestOperations(restTemplate())
  1376. return accessTokenResponseClient
  1377. }
  1378. @Bean
  1379. fun restTemplate(): RestTemplate {
  1380. // ...
  1381. }
  1382. }
  1383. ----
  1384. =====
  1385. A default `OAuth2AuthorizedClientManager` will be published automatically by Spring Security when one is not already provided.
  1386. [TIP]
  1387. ====
  1388. Notice that we don't need to customize the `SecurityFilterChain` bean in this case, and can stick with the defaults.
  1389. If using Spring Boot with no additional customizations, we can actually omit the `SecurityFilterChain` bean entirely.
  1390. ====
  1391. Prior to Spring Security 6.2, we had to ensure this customization was applied to both OAuth2 Login (if we are using this feature) and OAuth2 Client components.
  1392. We had to use both the Spring Security DSL (for the `authorization_code` grant) and publish a bean of type `OAuth2AuthorizedClientManager` for other grant types.
  1393. To understand what is being configured behind the scenes, here's what the configuration might have looked like:
  1394. .Customize `RestOperations` for OAuth2 Client (prior to 6.2)
  1395. [tabs]
  1396. =====
  1397. Java::
  1398. +
  1399. [source,java,role="primary"]
  1400. ----
  1401. @Configuration
  1402. @EnableWebSecurity
  1403. public class SecurityConfig {
  1404. @Bean
  1405. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  1406. DefaultAuthorizationCodeTokenResponseClient accessTokenResponseClient =
  1407. new DefaultAuthorizationCodeTokenResponseClient();
  1408. accessTokenResponseClient.setRestOperations(restTemplate());
  1409. http
  1410. // ...
  1411. .oauth2Login((oauth2Login) -> oauth2Login
  1412. .tokenEndpoint((tokenEndpoint) -> tokenEndpoint
  1413. .accessTokenResponseClient(accessTokenResponseClient)
  1414. )
  1415. )
  1416. .oauth2Client((oauth2Client) -> oauth2Client
  1417. .authorizationCodeGrant((authorizationCode) -> authorizationCode
  1418. .accessTokenResponseClient(accessTokenResponseClient)
  1419. )
  1420. );
  1421. return http.build();
  1422. }
  1423. @Bean
  1424. public OAuth2AuthorizedClientManager authorizedClientManager(
  1425. ClientRegistrationRepository clientRegistrationRepository,
  1426. OAuth2AuthorizedClientRepository authorizedClientRepository) {
  1427. DefaultRefreshTokenTokenResponseClient refreshTokenAccessTokenResponseClient =
  1428. new DefaultRefreshTokenTokenResponseClient();
  1429. refreshTokenAccessTokenResponseClient.setRestOperations(restTemplate());
  1430. DefaultClientCredentialsTokenResponseClient clientCredentialsAccessTokenResponseClient =
  1431. new DefaultClientCredentialsTokenResponseClient();
  1432. clientCredentialsAccessTokenResponseClient.setRestOperations(restTemplate());
  1433. DefaultPasswordTokenResponseClient passwordAccessTokenResponseClient =
  1434. new DefaultPasswordTokenResponseClient();
  1435. passwordAccessTokenResponseClient.setRestOperations(restTemplate());
  1436. DefaultJwtBearerTokenResponseClient jwtBearerAccessTokenResponseClient =
  1437. new DefaultJwtBearerTokenResponseClient();
  1438. jwtBearerAccessTokenResponseClient.setRestOperations(restTemplate());
  1439. JwtBearerOAuth2AuthorizedClientProvider jwtBearerAuthorizedClientProvider =
  1440. new JwtBearerOAuth2AuthorizedClientProvider();
  1441. jwtBearerAuthorizedClientProvider.setAccessTokenResponseClient(jwtBearerAccessTokenResponseClient);
  1442. DefaultTokenExchangeTokenResponseClient tokenExchangeAccessTokenResponseClient =
  1443. new DefaultTokenExchangeTokenResponseClient();
  1444. tokenExchangeAccessTokenResponseClient.setRestOperations(restTemplate());
  1445. TokenExchangeOAuth2AuthorizedClientProvider tokenExchangeAuthorizedClientProvider =
  1446. new TokenExchangeOAuth2AuthorizedClientProvider();
  1447. tokenExchangeAuthorizedClientProvider.setAccessTokenResponseClient(tokenExchangeAccessTokenResponseClient);
  1448. OAuth2AuthorizedClientProvider authorizedClientProvider =
  1449. OAuth2AuthorizedClientProviderBuilder.builder()
  1450. .authorizationCode()
  1451. .refreshToken((refreshToken) -> refreshToken
  1452. .accessTokenResponseClient(refreshTokenAccessTokenResponseClient)
  1453. )
  1454. .clientCredentials((clientCredentials) -> clientCredentials
  1455. .accessTokenResponseClient(clientCredentialsAccessTokenResponseClient)
  1456. )
  1457. .password((password) -> password
  1458. .accessTokenResponseClient(passwordAccessTokenResponseClient)
  1459. )
  1460. .provider(jwtBearerAuthorizedClientProvider)
  1461. .provider(tokenExchangeAuthorizedClientProvider)
  1462. .build();
  1463. DefaultOAuth2AuthorizedClientManager authorizedClientManager =
  1464. new DefaultOAuth2AuthorizedClientManager(
  1465. clientRegistrationRepository, authorizedClientRepository);
  1466. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  1467. return authorizedClientManager;
  1468. }
  1469. @Bean
  1470. public RestTemplate restTemplate() {
  1471. // ...
  1472. }
  1473. }
  1474. ----
  1475. Kotlin::
  1476. +
  1477. [source,kotlin,role="secondary"]
  1478. ----
  1479. import org.springframework.security.config.annotation.web.invoke
  1480. @Configuration
  1481. @EnableWebSecurity
  1482. class SecurityConfig {
  1483. @Bean
  1484. fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
  1485. val tokenResponseClient = DefaultAuthorizationCodeTokenResponseClient()
  1486. tokenResponseClient.setRestOperations(restTemplate())
  1487. http {
  1488. // ...
  1489. oauth2Login {
  1490. tokenEndpoint {
  1491. accessTokenResponseClient = tokenResponseClient
  1492. }
  1493. }
  1494. oauth2Client {
  1495. authorizationCodeGrant {
  1496. accessTokenResponseClient = tokenResponseClient
  1497. }
  1498. }
  1499. }
  1500. return http.build()
  1501. }
  1502. @Bean
  1503. fun authorizedClientManager(
  1504. clientRegistrationRepository: ClientRegistrationRepository?,
  1505. authorizedClientRepository: OAuth2AuthorizedClientRepository?
  1506. ): OAuth2AuthorizedClientManager {
  1507. val refreshTokenAccessTokenResponseClient = DefaultRefreshTokenTokenResponseClient()
  1508. refreshTokenAccessTokenResponseClient.setRestOperations(restTemplate())
  1509. val clientCredentialsAccessTokenResponseClient = DefaultClientCredentialsTokenResponseClient()
  1510. clientCredentialsAccessTokenResponseClient.setRestOperations(restTemplate())
  1511. val passwordAccessTokenResponseClient = DefaultPasswordTokenResponseClient()
  1512. passwordAccessTokenResponseClient.setRestOperations(restTemplate())
  1513. val jwtBearerAccessTokenResponseClient = DefaultJwtBearerTokenResponseClient()
  1514. jwtBearerAccessTokenResponseClient.setRestOperations(restTemplate())
  1515. val jwtBearerAuthorizedClientProvider = JwtBearerOAuth2AuthorizedClientProvider()
  1516. jwtBearerAuthorizedClientProvider.setAccessTokenResponseClient(jwtBearerAccessTokenResponseClient)
  1517. val tokenExchangeAccessTokenResponseClient = DefaultTokenExchangeTokenResponseClient()
  1518. tokenExchangeAccessTokenResponseClient.setRestOperations(restTemplate())
  1519. val tokenExchangeAuthorizedClientProvider = TokenExchangeOAuth2AuthorizedClientProvider()
  1520. tokenExchangeAuthorizedClientProvider.setAccessTokenResponseClient(tokenExchangeAccessTokenResponseClient)
  1521. val authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  1522. .authorizationCode()
  1523. .refreshToken { refreshToken ->
  1524. refreshToken.accessTokenResponseClient(refreshTokenAccessTokenResponseClient)
  1525. }
  1526. .clientCredentials { clientCredentials ->
  1527. clientCredentials.accessTokenResponseClient(clientCredentialsAccessTokenResponseClient)
  1528. }
  1529. .password { password ->
  1530. password.accessTokenResponseClient(passwordAccessTokenResponseClient)
  1531. }
  1532. .provider(jwtBearerAuthorizedClientProvider)
  1533. .provider(tokenExchangeAuthorizedClientProvider)
  1534. .build()
  1535. val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
  1536. clientRegistrationRepository, authorizedClientRepository
  1537. )
  1538. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  1539. return authorizedClientManager
  1540. }
  1541. @Bean
  1542. fun restTemplate(): RestTemplate {
  1543. // ...
  1544. }
  1545. }
  1546. ----
  1547. =====
  1548. [[further-reading]]
  1549. == Further Reading
  1550. This preceding sections introduced Spring Security's support for OAuth2 with examples for common scenarios.
  1551. You can read more about OAuth2 Client and Resource Server in the following sections of the reference documentation:
  1552. * xref:servlet/oauth2/login/index.adoc[]
  1553. * xref:servlet/oauth2/client/index.adoc[]
  1554. * xref:servlet/oauth2/resource-server/index.adoc[]