access-token.adoc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. [[webflux-oauth2-client]]
  2. = OAuth2 Client
  3. Spring Security's OAuth Support allows obtaining an access token without authenticating.
  4. A basic configuration with Spring Boot can be seen below:
  5. [source,yml]
  6. ----
  7. spring:
  8. security:
  9. oauth2:
  10. client:
  11. registration:
  12. github:
  13. client-id: replace-with-client-id
  14. client-secret: replace-with-client-secret
  15. scope: read:user,public_repo
  16. ----
  17. You will need to replace the `client-id` and `client-secret` with values registered with GitHub.
  18. The next step is to instruct Spring Security that you wish to act as an OAuth2 Client so that you can obtain an access token.
  19. .OAuth2 Client
  20. ====
  21. .Java
  22. [source,java,role="primary"]
  23. ----
  24. @Bean
  25. SecurityWebFilterChain configure(ServerHttpSecurity http) throws Exception {
  26. http
  27. // ...
  28. .oauth2Client(withDefaults());
  29. return http.build();
  30. }
  31. ----
  32. .Kotlin
  33. [source,kotlin,role="secondary"]
  34. ----
  35. @Bean
  36. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  37. return http {
  38. // ...
  39. oauth2Client { }
  40. }
  41. }
  42. ----
  43. ====
  44. You can now leverage Spring Security's <<webclient>> or <<webflux-roac,@RegisteredOAuth2AuthorizedClient>> support to obtain and use the access token.