README.adoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 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 subsequently verifies with the Authorization Server and authorizes the request, returning either the
  17. phrase
  18. ```bash
  19. Hello, subject for tenantOne!
  20. ```
  21. where "subject" is the value of the `sub` field in the JWT sent in the `Authorization` header,
  22. or the phrase
  23. ```bash
  24. Hello, subject for tenantTwo!
  25. ```
  26. where "subject" is the value of the `sub` field in the Introspection response from the Authorization Server.
  27. == 2. Running the app
  28. To run as a stand-alone application, do:
  29. ```bash
  30. ./gradlew bootRun
  31. ```
  32. Or import the project into your IDE and run `OAuth2ResourceServerApplication` from there.
  33. === Authorizing with tenantOne (JWT)
  34. Once it is up, you can use the following token:
  35. ```bash
  36. export TOKEN=eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJzdWJqZWN0IiwiZXhwIjo0NjgzODA1MTI4fQ.ULEPdHG-MK5GlrTQMhgqcyug2brTIZaJIrahUeq9zaiwUSdW83fJ7W1IDd2Z3n4a25JY2uhEcoV95lMfccHR6y_2DLrNvfta22SumY9PEDF2pido54LXG6edIGgarnUbJdR4rpRe_5oRGVa8gDx8FnuZsNv6StSZHAzw5OsuevSTJ1UbJm4UfX3wiahFOQ2OI6G-r5TB2rQNdiPHuNyzG5yznUqRIZ7-GCoMqHMaC-1epKxiX8gYXRROuUYTtcMNa86wh7OVDmvwVmFioRcR58UWBRoO1XQexTtOQq_t8KYsrPZhb9gkyW8x2bAQF-d0J0EJY8JslaH6n4RBaZISww
  37. ```
  38. And then make this request:
  39. ```bash
  40. curl -H "Authorization: Bearer $TOKEN" localhost:8080/tenantOne
  41. ```
  42. Which will respond with the phrase:
  43. ```bash
  44. Hello, subject for tenantOne!
  45. ```
  46. where `subject` is the value of the `sub` field in the JWT sent in the `Authorization` header.
  47. Or this:
  48. ```bash
  49. export TOKEN=eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJzdWJqZWN0Iiwic2NvcGUiOiJtZXNzYWdlOnJlYWQiLCJleHAiOjQ2ODM4MDUxNDF9.h-j6FKRFdnTdmAueTZCdep45e6DPwqM68ZQ8doIJ1exi9YxAlbWzOwId6Bd0L5YmCmp63gGQgsBUBLzwnZQ8kLUgUOBEC3UzSWGRqMskCY9_k9pX0iomX6IfF3N0PaYs0WPC4hO1s8wfZQ-6hKQ4KigFi13G9LMLdH58PRMK0pKEvs3gCbHJuEPw-K5ORlpdnleUTQIwINafU57cmK3KocTeknPAM_L716sCuSYGvDl6xUTXO7oPdrXhS_EhxLP6KxrpI1uD4Ea_5OWTh7S0Wx5LLDfU6wBG1DowN20d374zepOIEkR-Jnmr_QlR44vmRqS5ncrF-1R0EGcPX49U6A
  50. curl -H "Authorization: Bearer $TOKEN" localhost:8080/tenantOne/message
  51. ```
  52. Will respond with:
  53. ```bash
  54. secret message for tenantOne
  55. ```
  56. === Authorizing with tenantTwo (Opaque token)
  57. Once it is up, you can use the following token:
  58. ```bash
  59. export TOKEN=00ed5855-1869-47a0-b0c9-0f3ce520aee7
  60. ```
  61. And then make this request:
  62. ```bash
  63. curl -H "Authorization: Bearer $TOKEN" localhost:8080/tenantTwo
  64. ```
  65. Which will respond with the phrase:
  66. ```bash
  67. Hello, subject for tenantTwo!
  68. ```
  69. where `subject` is the value of the `sub` field in the Introspection response from the Authorization Server.
  70. Or this:
  71. ```bash
  72. export TOKEN=b43d1500-c405-4dc9-b9c9-6cfd966c34c9
  73. curl -H "Authorization: Bearer $TOKEN" localhost:8080/tenantTwo/message
  74. ```
  75. Will respond with:
  76. ```bash
  77. secret message for tenantTwo
  78. ```
  79. == 2. Testing against other Authorization Servers
  80. _In order to use this sample, your Authorization Server must support JWTs that either use the "scope" or "scp" attribute._
  81. To change the sample to point at your Authorization Server, simply find these properties in the `application.yml`:
  82. ```yaml
  83. tenantOne.jwk-set-uri: ${mockwebserver.url}/.well-known/jwks.json
  84. tenantTwo.introspection-uri: ${mockwebserver.url}/introspect
  85. tenantTwo.introspection-client-id: client
  86. tenantTwo.introspection-client-secret: secret
  87. ```
  88. And change the properties to your Authorization Server's JWK set endpoint and
  89. introspection endpoint, including its client id and secret
  90. ```yaml
  91. tenantOne.jwk-set-uri: https://dev-123456.oktapreview.com/oauth2/default/v1/keys
  92. tenantTwo.introspection-uri: https://dev-123456.oktapreview.com/oauth2/default/v1/introspect
  93. tenantTwo.introspection-client-id: client
  94. tenantTwo.introspection-client-secret: secret
  95. ```
  96. And then you can run the app the same as before:
  97. ```bash
  98. ./gradlew bootRun
  99. ```
  100. Make sure to obtain valid tokens from your Authorization Server in order to play with the sample Resource Server.
  101. To use the `/` endpoint, any valid token from your Authorization Server will do.
  102. To use the `/message` endpoint, the token should have the `message:read` scope.