index.adoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. return http {
  47. oauth2Client {
  48. clientRegistrationRepository = clientRegistrationRepository()
  49. authorizedClientRepository = authorizedClientRepository()
  50. authorizationRequestRepository = authorizedRequestRepository()
  51. authenticationConverter = authenticationConverter()
  52. authenticationManager = authenticationManager()
  53. }
  54. }
  55. }
  56. }
  57. ----
  58. ====
  59. 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).
  60. 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:
  61. ====
  62. .Java
  63. [source,java,role="primary"]
  64. ----
  65. @Bean
  66. public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
  67. ReactiveClientRegistrationRepository clientRegistrationRepository,
  68. ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
  69. ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
  70. ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  71. .authorizationCode()
  72. .refreshToken()
  73. .clientCredentials()
  74. .password()
  75. .build();
  76. DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager =
  77. new DefaultReactiveOAuth2AuthorizedClientManager(
  78. clientRegistrationRepository, authorizedClientRepository);
  79. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  80. return authorizedClientManager;
  81. }
  82. ----
  83. .Kotlin
  84. [source,kotlin,role="secondary"]
  85. ----
  86. @Bean
  87. fun authorizedClientManager(
  88. clientRegistrationRepository: ReactiveClientRegistrationRepository,
  89. authorizedClientRepository: ServerOAuth2AuthorizedClientRepository): ReactiveOAuth2AuthorizedClientManager {
  90. val authorizedClientProvider: ReactiveOAuth2AuthorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
  91. .authorizationCode()
  92. .refreshToken()
  93. .clientCredentials()
  94. .password()
  95. .build()
  96. val authorizedClientManager = DefaultReactiveOAuth2AuthorizedClientManager(
  97. clientRegistrationRepository, authorizedClientRepository)
  98. authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
  99. return authorizedClientManager
  100. }
  101. ----
  102. ====