index.adoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. [[webflux-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. * 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. * <<oauth2Client-webclient-webflux, `WebClient` integration for Reactive Environments>> (for requesting protected resources)
  16. The `ServerHttpSecurity.oauth2Client()` DSL provides a number of configuration options for customizing the core components used by OAuth 2.0 Client.
  17. The following code shows the complete configuration options provided by the `ServerHttpSecurity.oauth2Client()` DSL:
  18. .OAuth2 Client Configuration Options
  19. [tabs]
  20. ======
  21. Java::
  22. +
  23. [source,java,role="primary"]
  24. ----
  25. @Configuration
  26. @EnableWebFluxSecurity
  27. public class OAuth2ClientSecurityConfig {
  28. @Bean
  29. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  30. http
  31. .oauth2Client(oauth2 -> oauth2
  32. .clientRegistrationRepository(this.clientRegistrationRepository())
  33. .authorizedClientRepository(this.authorizedClientRepository())
  34. .authorizationRequestRepository(this.authorizationRequestRepository())
  35. .authenticationConverter(this.authenticationConverter())
  36. .authenticationManager(this.authenticationManager())
  37. );
  38. return http.build();
  39. }
  40. }
  41. ----
  42. Kotlin::
  43. +
  44. [source,kotlin,role="secondary"]
  45. ----
  46. @Configuration
  47. @EnableWebFluxSecurity
  48. class OAuth2ClientSecurityConfig {
  49. @Bean
  50. fun securityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  51. http {
  52. oauth2Client {
  53. clientRegistrationRepository = clientRegistrationRepository()
  54. authorizedClientRepository = authorizedClientRepository()
  55. authorizationRequestRepository = authorizedRequestRepository()
  56. authenticationConverter = authenticationConverter()
  57. authenticationManager = authenticationManager()
  58. }
  59. }
  60. return http.build()
  61. }
  62. }
  63. ----
  64. ======
  65. The `ReactiveOAuth2AuthorizedClientManager` is responsible for managing the authorization (or re-authorization) of an OAuth 2.0 Client, in collaboration with one or more `ReactiveOAuth2AuthorizedClientProvider`(s).
  66. The following code shows an example of how to register a `ReactiveOAuth2AuthorizedClientManager` `@Bean` and associate it with a `ReactiveOAuth2AuthorizedClientProvider` composite that provides support for the `authorization_code`, `refresh_token`, `client_credentials` and `password` authorization grant types:
  67. [tabs]
  68. ======
  69. Java::
  70. +
  71. [source,java,role="primary"]
  72. ----
  73. @Bean
  74. public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
  75. ReactiveClientRegistrationRepository clientRegistrationRepository,
  76. ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
  77. ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
  78. ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  79. .authorizationCode()
  80. .refreshToken()
  81. .clientCredentials()
  82. .password()
  83. .build();
  84. DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
  85. new DefaultReactiveOAuth2AuthorizedClientManager(
  86. clientRegistrationRepository, authorizedClientRepository);
  87. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  88. return authorizedClientManager;
  89. }
  90. ----
  91. Kotlin::
  92. +
  93. [source,kotlin,role="secondary"]
  94. ----
  95. @Bean
  96. fun authorizedClientManager(
  97. clientRegistrationRepository: ReactiveClientRegistrationRepository,
  98. authorizedClientRepository: ServerOAuth2AuthorizedClientRepository): ReactiveOAuth2AuthorizedClientManager {
  99. val authorizedClientProvider: ReactiveOAuth2AuthorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  100. .authorizationCode()
  101. .refreshToken()
  102. .clientCredentials()
  103. .password()
  104. .build()
  105. val authorizedClientManager = DefaultReactiveOAuth2AuthorizedClientManager(
  106. clientRegistrationRepository, authorizedClientRepository)
  107. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  108. return authorizedClientManager
  109. }
  110. ----
  111. ======