index.adoc 4.6 KB

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