index.adoc 60 KB

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