index.adoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. @EnableWebFluxSecurity
  26. public class OAuth2ClientSecurityConfig {
  27. @Bean
  28. public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  29. http
  30. .oauth2Client(oauth2 -> oauth2
  31. .clientRegistrationRepository(this.clientRegistrationRepository())
  32. .authorizedClientRepository(this.authorizedClientRepository())
  33. .authorizationRequestRepository(this.authorizationRequestRepository())
  34. .authenticationConverter(this.authenticationConverter())
  35. .authenticationManager(this.authenticationManager())
  36. );
  37. return http.build();
  38. }
  39. }
  40. ----
  41. Kotlin::
  42. +
  43. [source,kotlin,role="secondary"]
  44. ----
  45. @EnableWebFluxSecurity
  46. class OAuth2ClientSecurityConfig {
  47. @Bean
  48. fun securityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  49. return http {
  50. oauth2Client {
  51. clientRegistrationRepository = clientRegistrationRepository()
  52. authorizedClientRepository = authorizedClientRepository()
  53. authorizationRequestRepository = authorizedRequestRepository()
  54. authenticationConverter = authenticationConverter()
  55. authenticationManager = authenticationManager()
  56. }
  57. }
  58. }
  59. }
  60. ----
  61. ======
  62. 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).
  63. 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:
  64. [tabs]
  65. ======
  66. Java::
  67. +
  68. [source,java,role="primary"]
  69. ----
  70. @Bean
  71. public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
  72. ReactiveClientRegistrationRepository clientRegistrationRepository,
  73. ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
  74. ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
  75. ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  76. .authorizationCode()
  77. .refreshToken()
  78. .clientCredentials()
  79. .password()
  80. .build();
  81. DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
  82. new DefaultReactiveOAuth2AuthorizedClientManager(
  83. clientRegistrationRepository, authorizedClientRepository);
  84. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  85. return authorizedClientManager;
  86. }
  87. ----
  88. Kotlin::
  89. +
  90. [source,kotlin,role="secondary"]
  91. ----
  92. @Bean
  93. fun authorizedClientManager(
  94. clientRegistrationRepository: ReactiveClientRegistrationRepository,
  95. authorizedClientRepository: ServerOAuth2AuthorizedClientRepository): ReactiveOAuth2AuthorizedClientManager {
  96. val authorizedClientProvider: ReactiveOAuth2AuthorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  97. .authorizationCode()
  98. .refreshToken()
  99. .clientCredentials()
  100. .password()
  101. .build()
  102. val authorizedClientManager = DefaultReactiveOAuth2AuthorizedClientManager(
  103. clientRegistrationRepository, authorizedClientRepository)
  104. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  105. return authorizedClientManager
  106. }
  107. ----
  108. ======