README.adoc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. = OAuth 2.0 Resource Server Sample
  2. This sample demonstrates integrating Resource Server with a pre-configured key.
  3. With it, you can run the integration tests or run the application as a stand-alone service to explore how you can
  4. secure your own service with OAuth 2.0 Bearer Tokens using Spring Security.
  5. == 1. Running the tests
  6. To run the tests, do:
  7. ```bash
  8. ./gradlew integrationTest
  9. ```
  10. Or import the project into your IDE and run `OAuth2ResourceServerApplicationITests` from there.
  11. === What is it doing?
  12. By default, the application is configured with an RSA public key that is available in the sample.
  13. The tests are configured with a set of hard-coded tokens that are signed with the corresponding RSA private key.
  14. Each test makes a query to the Resource Server with their corresponding token.
  15. The Resource Server subsequently verifies the token against the public key and authorizes the request, returning the phrase
  16. ```bash
  17. Hello, subject!
  18. ```
  19. where "subject" is the value of the `sub` field in the token.
  20. == 2. Running the app
  21. To run as a stand-alone application, do:
  22. ```bash
  23. ./gradlew bootRun
  24. ```
  25. Or import the project into your IDE and run `OAuth2ResourceServerApplication` from there.
  26. Once it is up, you can use the following token:
  27. ```bash
  28. export TOKEN=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzdWJqZWN0IiwiaWF0IjoxNTE2MjM5MDIyfQ.eB2c9xtg5wcCZxZ-o-sH4Mx1JGkqAZwH4_WS0UcDbj_nen0NPBj6CqOEPhr_LZDagb4mM6HoAPJywWWG8b_Ylnn5r2gWDzib2mb0kxIuAjnvVBrpzusw4ItTVvP_srv2DrwcisKYiKqU5X_3ka7MSVvKtswdLY3RXeCJ_S2W9go
  29. ```
  30. And then make this request:
  31. ```bash
  32. curl -H "Authorization: Bearer $TOKEN" localhost:8080
  33. ```
  34. Which will respond with the phrase:
  35. ```bash
  36. Hello, subject!
  37. ```
  38. where `subject` is the value of the `sub` field in the token.
  39. Or this:
  40. ```bash
  41. export TOKEN=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzdWJqZWN0IiwiaWF0IjoxNTE2MjM5MDIyLCJzY29wZSI6Im1lc3NhZ2U6cmVhZCJ9.bsRCpUEaiWnzX4OqNxTBqwUD4vxxtPp-CHKTw7XcrglrvZ2lvYXaiZZbCp-hcPhuzMEzEAFuH6s4GZZOWVIX-wT47GdTz9cfA-Z4QPjS2RxePKphFXgBI3jHEpQo94Qya2fJdV4LvgBmA1uM_RTnYY1UbmeYuHKnXrZoGyV8QQQ
  42. curl -H "Authorization: Bearer $TOKEN" localhost:8080/message
  43. ```
  44. Will respond with:
  45. ```bash
  46. secret message
  47. ```
  48. == 3. Testing with Other Tokens
  49. You can create your own tokens. Simply edit the public key in `OAuth2ResourceServerSecurityConfiguration` to match the private key you use.
  50. To use the `/` endpoint, any valid token will do.
  51. To use the `/message` endpoint, the token should have the `message:read` scope.