index.adoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. [[oauth2client]]
  2. = OAuth 2.0 Client
  3. :page-section-summary-toc: 1
  4. The OAuth 2.0 Client features provide support for the Client role as defined in the https://tools.ietf.org/html/rfc6749#section-1.1[OAuth 2.0 Authorization Framework].
  5. At a high-level, the core features available are:
  6. .Authorization Grant support
  7. * https://tools.ietf.org/html/rfc6749#section-1.3.1[Authorization Code]
  8. * https://tools.ietf.org/html/rfc6749#section-6[Refresh Token]
  9. * https://tools.ietf.org/html/rfc6749#section-1.3.4[Client Credentials]
  10. * https://tools.ietf.org/html/rfc6749#section-1.3.3[Resource Owner Password Credentials]
  11. * https://datatracker.ietf.org/doc/html/rfc7523#section-2.1[JWT Bearer]
  12. * https://datatracker.ietf.org/doc/html/rfc8693#section-2.1[Token Exchange]
  13. .Client Authentication support
  14. * https://datatracker.ietf.org/doc/html/rfc7523#section-2.2[JWT Bearer]
  15. .HTTP Client support
  16. * xref:servlet/oauth2/client/authorized-clients.adoc#oauth2Client-webclient-servlet[`WebClient` integration for Servlet Environments] (for requesting protected resources)
  17. The `HttpSecurity.oauth2Client()` DSL provides a number of configuration options for customizing the core components used by OAuth 2.0 Client.
  18. In addition, `HttpSecurity.oauth2Client().authorizationCodeGrant()` enables the customization of the Authorization Code grant.
  19. The following code shows the complete configuration options provided by the `HttpSecurity.oauth2Client()` DSL:
  20. .OAuth2 Client Configuration Options
  21. [tabs]
  22. ======
  23. Java::
  24. +
  25. [source,java,role="primary"]
  26. ----
  27. @Configuration
  28. @EnableWebSecurity
  29. public class OAuth2ClientSecurityConfig {
  30. @Bean
  31. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  32. http
  33. .oauth2Client(oauth2 -> oauth2
  34. .clientRegistrationRepository(this.clientRegistrationRepository())
  35. .authorizedClientRepository(this.authorizedClientRepository())
  36. .authorizedClientService(this.authorizedClientService())
  37. .authorizationCodeGrant(codeGrant -> codeGrant
  38. .authorizationRequestRepository(this.authorizationRequestRepository())
  39. .authorizationRequestResolver(this.authorizationRequestResolver())
  40. .accessTokenResponseClient(this.accessTokenResponseClient())
  41. )
  42. );
  43. return http.build();
  44. }
  45. }
  46. ----
  47. Kotlin::
  48. +
  49. [source,kotlin,role="secondary"]
  50. ----
  51. @Configuration
  52. @EnableWebSecurity
  53. class OAuth2ClientSecurityConfig {
  54. @Bean
  55. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  56. http {
  57. oauth2Client {
  58. clientRegistrationRepository = clientRegistrationRepository()
  59. authorizedClientRepository = authorizedClientRepository()
  60. authorizedClientService = authorizedClientService()
  61. authorizationCodeGrant {
  62. authorizationRequestRepository = authorizationRequestRepository()
  63. authorizationRequestResolver = authorizationRequestResolver()
  64. accessTokenResponseClient = accessTokenResponseClient()
  65. }
  66. }
  67. }
  68. return http.build()
  69. }
  70. }
  71. ----
  72. ======
  73. In addition to the `HttpSecurity.oauth2Client()` DSL, XML configuration is also supported.
  74. The following code shows the complete configuration options available in the xref:servlet/appendix/namespace/http.adoc#nsa-oauth2-client[ security namespace]:
  75. .OAuth2 Client XML Configuration Options
  76. [source,xml]
  77. ----
  78. <http>
  79. <oauth2-client client-registration-repository-ref="clientRegistrationRepository"
  80. authorized-client-repository-ref="authorizedClientRepository"
  81. authorized-client-service-ref="authorizedClientService">
  82. <authorization-code-grant
  83. authorization-request-repository-ref="authorizationRequestRepository"
  84. authorization-request-resolver-ref="authorizationRequestResolver"
  85. access-token-response-client-ref="accessTokenResponseClient"/>
  86. </oauth2-client>
  87. </http>
  88. ----
  89. The `OAuth2AuthorizedClientManager` is responsible for managing the authorization (or re-authorization) of an OAuth 2.0 Client, in collaboration with one or more `OAuth2AuthorizedClientProvider`(s).
  90. The following code shows an example of how to register an `OAuth2AuthorizedClientManager` `@Bean` and associate it with an `OAuth2AuthorizedClientProvider` composite that provides support for the `authorization_code`, `refresh_token`, `client_credentials`, and `password` authorization grant types:
  91. [tabs]
  92. ======
  93. Java::
  94. +
  95. [source,java,role="primary"]
  96. ----
  97. @Bean
  98. public OAuth2AuthorizedClientManager authorizedClientManager(
  99. ClientRegistrationRepository clientRegistrationRepository,
  100. OAuth2AuthorizedClientRepository authorizedClientRepository) {
  101. OAuth2AuthorizedClientProvider authorizedClientProvider =
  102. OAuth2AuthorizedClientProviderBuilder.builder()
  103. .authorizationCode()
  104. .refreshToken()
  105. .clientCredentials()
  106. .password()
  107. .build();
  108. DefaultOAuth2AuthorizedClientManager authorizedClientManager =
  109. new DefaultOAuth2AuthorizedClientManager(
  110. clientRegistrationRepository, authorizedClientRepository);
  111. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  112. return authorizedClientManager;
  113. }
  114. ----
  115. Kotlin::
  116. +
  117. [source,kotlin,role="secondary"]
  118. ----
  119. @Bean
  120. fun authorizedClientManager(
  121. clientRegistrationRepository: ClientRegistrationRepository,
  122. authorizedClientRepository: OAuth2AuthorizedClientRepository): OAuth2AuthorizedClientManager {
  123. val authorizedClientProvider: OAuth2AuthorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
  124. .authorizationCode()
  125. .refreshToken()
  126. .clientCredentials()
  127. .password()
  128. .build()
  129. val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
  130. clientRegistrationRepository, authorizedClientRepository)
  131. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  132. return authorizedClientManager
  133. }
  134. ----
  135. ======