index.adoc 4.6 KB

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