registered-oauth2-authorized-client.adoc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. [[webflux-roac]]
  2. = @RegisteredOAuth2AuthorizedClient
  3. Spring Security allows resolving an access token using `@RegisteredOAuth2AuthorizedClient`.
  4. [NOTE]
  5. ====
  6. A working example can be found in {gh-samples-url}/reactive/webflux/java/oauth2/webclient[*OAuth 2.0 WebClient WebFlux sample*].
  7. ====
  8. After configuring Spring Security for xref:reactive/oauth2/login.adoc#webflux-oauth2-login[OAuth2 Login] or as an xref:reactive/oauth2/access-token.adoc#webflux-oauth2-client[OAuth2 Client], an `OAuth2AuthorizedClient` can be resolved using the following:
  9. ====
  10. .Java
  11. [source,java,role="primary"]
  12. ----
  13. @GetMapping("/explicit")
  14. Mono<String> explicit(@RegisteredOAuth2AuthorizedClient("client-id") OAuth2AuthorizedClient authorizedClient) {
  15. // ...
  16. }
  17. ----
  18. .Kotlin
  19. [source,kotlin,role="secondary"]
  20. ----
  21. @GetMapping("/explicit")
  22. fun explicit(@RegisteredOAuth2AuthorizedClient("client-id") authorizedClient: OAuth2AuthorizedClient?): Mono<String> {
  23. // ...
  24. }
  25. ----
  26. ====
  27. This integrates into Spring Security to provide the following features:
  28. * Spring Security will automatically refresh expired tokens (if a refresh token is present)
  29. * If an access token is requested and not present, Spring Security will automatically request the access token.
  30. ** For `authorization_code` this involves performing the redirect and then replaying the original request
  31. ** For `client_credentials` the token is simply requested and saved
  32. If the user authenticated using `oauth2Login()`, then the `client-id` is optional.
  33. For example, the following would work:
  34. ====
  35. .Java
  36. [source,java,role="primary"]
  37. ----
  38. @GetMapping("/implicit")
  39. Mono<String> implicit(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient) {
  40. // ...
  41. }
  42. ----
  43. .Kotlin
  44. [source,kotlin,role="secondary"]
  45. ----
  46. @GetMapping("/implicit")
  47. fun implicit(@RegisteredOAuth2AuthorizedClient authorizedClient: OAuth2AuthorizedClient?): Mono<String> {
  48. // ...
  49. }
  50. ----
  51. ====
  52. This is convenient if the user always authenticates with OAuth2 Login and an access token from the same authorization server is needed.