index.adoc 5.7 KB

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