index.adoc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. [[oauth2-client]]
  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. * xref:servlet/oauth2/client/authorization-grants.adoc#oauth2-client-authorization-code[Authorization Code]
  8. * xref:servlet/oauth2/client/authorization-grants.adoc#oauth2-client-refresh-token[Refresh Token]
  9. * xref:servlet/oauth2/client/authorization-grants.adoc#oauth2-client-client-credentials[Client Credentials]
  10. * xref:servlet/oauth2/client/authorization-grants.adoc#oauth2-client-jwt-bearer[JWT Bearer]
  11. * xref:servlet/oauth2/client/authorization-grants.adoc#oauth2-client-token-exchange[Token Exchange]
  12. .Client Authentication support
  13. * xref:servlet/oauth2/client/client-authentication.adoc#oauth2-client-authentication-jwt-bearer[JWT Bearer]
  14. .HTTP Client support (for requesting protected resources)
  15. * xref:servlet/oauth2/client/authorized-clients.adoc#oauth2-client-rest-client[`RestClient` integration]
  16. * xref:servlet/oauth2/client/authorized-clients.adoc#oauth2-client-web-client[`WebClient` integration for Servlet Environments]
  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` and `client_credentials` 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. .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. .build()
  127. val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
  128. clientRegistrationRepository, authorizedClientRepository)
  129. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  130. return authorizedClientManager
  131. }
  132. ----
  133. ======