index.adoc 6.1 KB

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