README.adoc 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. = OAuth 2.0 RestClient Sample
  2. This sample demonstrates making protected resources requests with `RestClient` via an interceptor that adds the `Authorization` header to each request.
  3. == 1. Running the tests
  4. To run the tests, do:
  5. [source,bash]
  6. ----
  7. ./gradlew integrationTest
  8. ----
  9. Or import the project into your IDE and run `OAuth2RestClientApplicationITests` from there.
  10. == 2. Running the app with an Authorization Server and Resource Server
  11. Before running this application with the default configuration, you will need to start up an Authorization Server and Resource Server, which are provided as additional samples and pre-configured to work with this OAuth2 RestClient Sample out of the box.
  12. To run the https://github.com/spring-projects/spring-security-samples/tree/main/servlet/spring-boot/java/oauth2/authorization-server[Authorization Server] as a stand-alone application, navigate to `servlet/spring-boot/java/oauth2/authorization-server` and do:
  13. [source,bash]
  14. ----
  15. ./gradlew bootRun
  16. ----
  17. Or import the project into your IDE and run `OAuth2AuthorizationServerApplication` from there.
  18. To run the https://github.com/spring-projects/spring-security-samples/tree/main/servlet/spring-boot/java/oauth2/resource-server/restclient[Resource Server] as a stand-alone application, navigate to `servlet/spring-boot/java/oauth2/resource-server/restclient` and do:
  19. [source,bash]
  20. ----
  21. ./gradlew bootRun
  22. ----
  23. Or import the project into your IDE and run `OAuth2ResourceServerApplication` from there.
  24. Next, you can run this sample.
  25. To run this sample as a stand-alone application, do:
  26. [source,bash]
  27. ----
  28. ./gradlew bootRun
  29. ----
  30. Or import the project into your IDE and run `OAuth2RestClientApplication` from there.
  31. Once the application is running, visit http://127.0.0.1:8080[127.0.0.1:8080] in your browser to try out the sample.
  32. == 3. Alternate Configurations
  33. This sample demonstrates alternate strategies for resolving a `clientRegistrationId` (see https://github.com/spring-projects/spring-security-samples/tree/main/servlet/spring-boot/java/oauth2/restclient/src/main/java/example/ClientRegistrationIdResolverConfiguration.java[ClientRegistrationIdResolverConfiguration] for more information).
  34. Activate one of the following profiles to try them out:
  35. 1. `default` - Demonstrates the default setup with `RequestAttributeClientRegistrationIdResolver`. Uses `login-client` as the `clientRegistrationId` to log in and `messaging-client` for authorization.
  36. 2. `current-user` - Demonstrates a custom `ClientRegistrationIdResolver` that simply resolves the `clientRegistrationId` from the current user. Uses `login-client-with-messaging` to log in.
  37. 3. `composite` - Demonstrates a composite `ClientRegistrationIdResolver` that tries multiple ways of resolving a `clientRegistrationId`. Uses `login-client-with-messaging` to log in.
  38. 4. `authentication-required` - Demonstrates a custom `ClientRegistrationIdResolver` that requires authentication using OAuth 2.0 or Open ID Connect 1.0. Uses `login-client-with-messaging` to log in.
  39. This sample also demonstrates alternate strategies for resolving a `principal` (see https://github.com/spring-projects/spring-security-samples/tree/main/servlet/spring-boot/java/oauth2/restclient/src/main/java/example/PrincipalResolverConfiguration.java[PrincipalResolverConfiguration] for more information).
  40. Activate one of the following profiles to try them out:
  41. 1. `per-request` - Demonstrates an alternate setup with `RequestAttributePrincipalResolver` that resolves the principal using attributes.
  42. 2. `anonymous-user` - Demonstrates a custom `PrincipalResolver` that statically resolves a `principal`. Requires specifying the `principal` via `RequestAttributePrincipalResolver.principal(Authentication)`.
  43. [TIP]
  44. ====
  45. You can activate a profile with the `./gradlew bootRun` command by adding the argument `--args='--spring.profiles.active=xyz'`.
  46. ====