README.adoc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. = OAuth 2.0 Resource Server Sample
  2. This sample demonstrates integrating Resource Server with the Spring Authorization Server, though it can be modified to integrate
  3. with a mock server or 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 via the `test` profile.
  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 with Spring Authorization Server
  22. Before running this application with the default configuration, you will need to start up an Authorization Server, such as the https://github.com/spring-projects/spring-security-samples/tree/main/servlet/spring-boot/java/oauth2/authorization-server[authorization-server sample] in this project which is pre-configured to work with this Resource Server sample out of the box.
  23. To run the Authorization Server as a stand-alone application, navigate to the `servlet/spring-boot/java/oauth2/authorization-server` and do:
  24. ```bash
  25. ./gradlew bootRun
  26. ```
  27. Or import the project into your IDE and run `OAuth2AuthorizationServerApplication` from there. Next, you can run this Resource Server.
  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. Once it is up and running, you can issue the following request:
  34. ```bash
  35. curl -X POST messaging-client:secret@localhost:9000/oauth2/token -d "grant_type=client_credentials" -d "scope=message:read"
  36. ```
  37. This returns something like the following:
  38. ```json
  39. {
  40. "access_token": "eyJraWQiOiI4YWY4Zjc2Zi0zMTdkLTQxZmYtYWY5Yi1hZjg5NDg4ODM5YzciLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJtZXNzYWdpbmctY2xpZW50IiwiYXVkIjoibWVzc2FnaW5nLWNsaWVudCIsIm5iZiI6MTYyNzMzNDQ1MCwic2NvcGUiOlsibWVzc2FnZTpyZWFkIl0sImlzcyI6Imh0dHA6XC9cL2xvY2FsaG9zdDo5MDAwIiwiZXhwIjoxNjI3MzM0NzUwLCJpYXQiOjE2MjczMzQ0NTAsImp0aSI6IjBiYjYwZjhkLWIzNjItNDk0MC05MGRmLWZhZDg4N2Q1Yzg1ZSJ9.O8dI67B_feRjOn6pJi5ctPJmUJCNpV77SC4OiWqmpa5UHvf4Ud6L6EFe9LKuPIRrEWi8rMdCdMBOPKQMXvxLoI3LMUPf7Yj973uvZN0E988MsKwhGwxyaa_Wam8wFlk8aQlN8SbW3cKdeH-nKloNMdwjfspovefX521mxouaMjmyXdIFrM5WZ15GZK69NIniACSatE-pc9TAjKYBDbC65jVt_zHEvDQbEkZulF2bjrGOZC8C3IbJWnlKgkcshrY44TtrGPyCp2gIS0TSUUsG00iSBBC8E8zPU-YdfaP8gB9_FwUwK9zfy_hU2Ykf2aU3eulpGDVLn2rCwFeK86Rw1w",
  41. "expires_in": 299,
  42. "scope": "message:read",
  43. "token_type": "Bearer"
  44. }
  45. ```
  46. Then, export the access token from the response:
  47. ```bash
  48. export TOKEN=...
  49. ```
  50. Then issue the following request:
  51. ```bash
  52. curl -H "Authorization: Bearer $TOKEN" localhost:8080
  53. ```
  54. Which will respond with the phrase:
  55. ```
  56. Hello, messaging-client!
  57. ```
  58. where `messaging-client` is the value of the `sub` field in the JWT returned by the Authorization Server.
  59. Or this to make a GET request to /message:
  60. ```bash
  61. curl -H "Authorization: Bearer $TOKEN" localhost:8080/message
  62. ```
  63. Will respond with:
  64. ```bash
  65. secret message
  66. ```
  67. In order to make a POST request to /message, you can use the following request:
  68. ```bash
  69. curl -X POST messaging-client:secret@localhost:9000/oauth2/token -d "grant_type=client_credentials" -d "scope=message:write"
  70. ```
  71. Then, export the access token from the response:
  72. ```bash
  73. export TOKEN=...
  74. ```
  75. Then issue the following request:
  76. ```bash
  77. curl -H "Authorization: Bearer $TOKEN" -d "my message" localhost:8080/message
  78. ```
  79. Which will respond with:
  80. ```bash
  81. Message was created. Content: my message
  82. ```
  83. == 3. Running the app with a mock Authorization Server
  84. To run as a stand-alone application with an embedded mock Authorization Server, do:
  85. ```bash
  86. ./gradlew bootRun --args='--spring.profiles.active=test'
  87. ```
  88. Or import the project into your IDE and run `OAuth2ResourceServerApplication` from there with the `test` profile active.
  89. Once it is up, you can use the following token:
  90. ```bash
  91. export TOKEN=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzdWJqZWN0IiwiZXhwIjoyMTY0MjQ1ODgwLCJhdXRob3JpdGllcyI6WyJST0xFX1VTRVIiXSwianRpIjoiMDFkOThlZWEtNjc0MC00OGRlLTk4ODAtYzM5ZjgyMGZiNzVlIiwiY2xpZW50X2lkIjoibm9zY29wZXMiLCJzY29wZSI6WyJub25lIl19.VOzgGLOUuQ_R2Ur1Ke41VaobddhKgUZgto7Y3AGxst7SuxLQ4LgWwdSSDRx-jRvypjsCgYPbjAYLhn9nCbfwtCitkymUKUNKdebvVAI0y8YvliWTL5S-GiJD9dN8SSsXUla9A4xB_9Mt5JAlRpQotQSCLojVSKQmjhMpQWmYAlKVjnlImoRwQFPI4w3Ijn4G4EMTKWUYRfrD0-WNT9ZYWBeza6QgV6sraP7ToRB3eQLy2p04cU40X-RHLeYCsMBfxsMMh89CJff-9tn7VDKi1hAGc_Lp9yS9ZaItJuFJTjf8S_vsjVB1nBhvdS_6IED_m_fOU52KiGSO2qL6shxHvg
  92. ```
  93. And then make this request:
  94. ```bash
  95. curl -H "Authorization: Bearer $TOKEN" localhost:8080
  96. ```
  97. Which will respond with the phrase:
  98. ```bash
  99. Hello, subject!
  100. ```
  101. where `subject` is the value of the `sub` field in the JWT returned by the Authorization Server.
  102. Or this to make a GET request to /message:
  103. ```bash
  104. export TOKEN=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzdWJqZWN0IiwiZXhwIjoyMTY0MjQ1NjQ4LCJhdXRob3JpdGllcyI6WyJST0xFX1VTRVIiXSwianRpIjoiY2I1ZGMwNDYtMDkyMi00ZGJmLWE5MzAtOGI2M2FhZTYzZjk2IiwiY2xpZW50X2lkIjoicmVhZGVyIiwic2NvcGUiOlsibWVzc2FnZTpyZWFkIl19.Pre2ksnMiOGYWQtuIgHB0i3uTnNzD0SMFM34iyQJHK5RLlSjge08s9qHdx6uv5cZ4gZm_cB1D6f4-fLx76bCblK6mVcabbR74w_eCdSBXNXuqG-HNrOYYmmx5iJtdwx5fXPmF8TyVzsq_LvRm_LN4lWNYquT4y36Tox6ZD3feYxXvHQ3XyZn9mVKnlzv-GCwkBohCR3yPow5uVmr04qh_al52VIwKMrvJBr44igr4fTZmzwRAZmQw5rZeyep0b4nsCjadNcndHtMtYKNVuG5zbDLsB7GGvilcI9TDDnUXtwthB_3iq32DAd9x8wJmJ5K8gmX6GjZFtYzKk_zEboXoQ
  105. curl -H "Authorization: Bearer $TOKEN" localhost:8080/message
  106. ```
  107. Will respond with:
  108. ```bash
  109. secret message
  110. ```
  111. In order to make a POST request to /message, you can use the following request:
  112. ```bash
  113. export TOKEN=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzdWJqZWN0IiwiZXhwIjoyMTY0MjQzOTA0LCJhdXRob3JpdGllcyI6WyJST0xFX1VTRVIiXSwianRpIjoiZGI4ZjgwMzQtM2VlNy00NjBjLTk3NTEtMDJiMDA1OWI5NzA4IiwiY2xpZW50X2lkIjoid3JpdGVyIiwic2NvcGUiOlsibWVzc2FnZTp3cml0ZSJdfQ.USvpx_ntKXtchLmc93auJq0qSav6vLm4B7ItPzhrDH2xmogBP35eKeklwXK5GCb7ck1aKJV5SpguBlTCz0bZC1zAWKB6gyFIqedALPAran5QR-8WpGfl0wFqds7d8Jw3xmpUUBduRLab9hkeAhgoVgxevc8d6ITM7kRnHo5wT3VzvBU8DquedVXm5fbBnRPgG4_jOWJKbqYpqaR2z2TnZRWh3CqL82Orh1Ww1dJYF_fae1dTVV4tvN5iSndYcGxMoBaiw3kRRi6EyNxnXnt1pFtZqc1f6D9x4AHiri8_vpBp2vwG5OfQD5-rrleP_XlIB3rNQT7tu3fiqu4vUzQaEg
  114. curl -H "Authorization: Bearer $TOKEN" -d "my message" localhost:8080/message
  115. ```
  116. Will respond this:
  117. ```bash
  118. Message was created. Content: my message
  119. ```
  120. == 4. Testing against other Authorization Servers
  121. _In order to use this sample, your Authorization Server must support JWTs that either use the "scope" or "scp" attribute._
  122. To change the sample to point at your Authorization Server, simply find this property in the `application.yml`:
  123. ```yaml
  124. spring:
  125. security:
  126. oauth2:
  127. resourceserver:
  128. jwt:
  129. jwk-set-uri: http://localhost:9000/oauth2/jwks
  130. ```
  131. And change the property to your Authorization Server's JWK set endpoint:
  132. ```yaml
  133. spring:
  134. security:
  135. oauth2:
  136. resourceserver:
  137. jwt:
  138. jwk-set-uri: https://dev-123456.oktapreview.com/oauth2/default/v1/keys
  139. ```
  140. And then you can run the app the same as before:
  141. ```bash
  142. ./gradlew bootRun
  143. ```
  144. Make sure to obtain valid tokens from your Authorization Server in order to play with the sample Resource Server.
  145. To use the `/` endpoint, any valid token from your Authorization Server will do.
  146. To use the `/message` endpoint, the token should have the `message:read` scope.