README.adoc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. = OAuth 2.0 Resource Server Sample
  2. This sample demonstrates integrating Resource Server with a mock Authorization Server, though it can be modified to integrate
  3. with your favorite Authorization Server.
  4. With it, you can run the integration tests or run the application as a stand-alone service to explore how you can
  5. secure your own service with OAuth 2.0 Opaque Bearer Tokens using Spring Security.
  6. == 1. Running the tests
  7. To run the tests, do:
  8. ```bash
  9. ./gradlew integrationTest
  10. ```
  11. Or import the project into your IDE and run `OAuth2ResourceServerApplicationTests` from there.
  12. === What is it doing?
  13. By default, the tests are pointing at a mock Authorization Server instance.
  14. The tests are configured with a set of hard-coded tokens originally obtained from the mock Authorization Server,
  15. and each makes a query to the Resource Server with their corresponding token.
  16. The Resource Server subsquently verifies with the Authorization Server and authorizes the request, returning the phrase
  17. ```bash
  18. Hello, subject!
  19. ```
  20. where "subject" is the value of the `sub` field in the JWT returned by the Authorization Server.
  21. == 2. Running the app
  22. To run as a stand-alone application, do:
  23. ```bash
  24. ./gradlew bootRun
  25. ```
  26. Or import the project into your IDE and run `OAuth2ResourceServerApplication` from there.
  27. Once it is up, you can use the following token:
  28. ```bash
  29. export TOKEN=00ed5855-1869-47a0-b0c9-0f3ce520aee7
  30. ```
  31. And then make this request:
  32. ```bash
  33. curl -H "Authorization: Bearer $TOKEN" localhost:8080
  34. ```
  35. Which will respond with the phrase:
  36. ```bash
  37. Hello, subject!
  38. ```
  39. where `subject` is the value of the `sub` field in the JWT returned by the Authorization Server.
  40. Or this:
  41. ```bash
  42. export TOKEN=b43d1500-c405-4dc9-b9c9-6cfd966c34c9
  43. curl -H "Authorization: Bearer $TOKEN" localhost:8080/message
  44. ```
  45. Will respond with:
  46. ```bash
  47. secret message
  48. ```
  49. == 2. Testing against other Authorization Servers
  50. _In order to use this sample, your Authorization Server must support Opaque Tokens and the Introspection Endpoint.
  51. To change the sample to point at your Authorization Server, simply find this property in the `application.yml`:
  52. ```yaml
  53. spring:
  54. security:
  55. oauth2:
  56. resourceserver:
  57. opaque:
  58. introspection-uri: ${mockwebserver.url}/introspect
  59. introspection-client-id: client
  60. introspection-client-secret: secret
  61. ```
  62. And change the property to your Authorization Server's Introspection endpoint, including its client id and secret:
  63. ```yaml
  64. spring:
  65. security:
  66. oauth2:
  67. resourceserver:
  68. opaque:
  69. introspection-uri: ${mockwebserver.url}/introspect
  70. ```
  71. And then you can run the app the same as before:
  72. ```bash
  73. ./gradlew bootRun
  74. ```
  75. Make sure to obtain valid tokens from your Authorization Server in order to play with the sample Resource Server.
  76. To use the `/` endpoint, any valid token from your Authorization Server will do.
  77. To use the `/message` endpoint, the token should have the `message:read` scope.