README.adoc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. = OAuth 2.0 Resource Server Sample
  2. This sample demonstrates integrations with a handful of different authorization servers.
  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 `OAuth2ResourceServerApplicationTests` from there.
  11. === What is it doing?
  12. By default, the tests are pointing at a demonstration Okta instance. The test that performs a valid round trip does so
  13. by querying the Okta Authorization Server using the client_credentials grant type to get a valid JWT token. Then, the test
  14. makes a query to the Resource Server with that token. The Resource Server subsquently verifies with Okta and
  15. authorizes the request, returning the phrase
  16. ```bash
  17. Hello, {subject}!
  18. ```
  19. where subject is the value of the `sub` field in the JWT returned by the Authorization Server.
  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 retreive a valid JWT token from the authorization server, and then hit the endpoint:
  27. ```bash
  28. curl -H "Authorization: Bearer {token}" localhost:8081
  29. ```
  30. Which will respond with the phrase:
  31. ```bash
  32. Hello, {subject}!
  33. ```
  34. where `subject` is the value of the `sub` field in the JWT returned by the Authorization Server.
  35. === How do I obtain a valid JWT token?
  36. Getting a valid JWT token from an Authorization Server will vary, depending on your setup. However, it will typically
  37. look something like this:
  38. ```bash
  39. curl --user {client id}:{client password} -d "grant_type=client_credentials" {auth server endpoint}/token
  40. ```
  41. which will respond with a JSON payload containing the `access_token` among other things:
  42. ```bash
  43. { "access_token" : "{the access token}", "token_type" : "Bearer", "expires_in" : "{an expiry}", "scope" : "{a list of scopes}" }
  44. ```
  45. For example, the following can be used to hit the sample Okta endpoint for a valid JWT token:
  46. ```bash
  47. curl --user 0oaf5u5g4m6CW4x6z0h7:HR7edRoo3glhF06HTxonOKZvO4I2BWYcC_ocOHlv -d "grant_type=client_credentials" https://dev-805262.oktapreview.com/oauth2/default/v1/token
  48. ```
  49. Which will give a response similar to this (formatting mine):
  50. ```json
  51. {
  52. "access_token": "eyJraWQiOiJFRjBFWDFFWHZGc1hGaDhuYkRGazNJN0hMUDBsZnJnc0JKMVdBWmkwRmI0IiwiYWxnIjoiUlMyNTYifQ.eyJ2ZXIiOjEsImp0aSI6IkFULmtQSUdfMEVMQmM3NVFMN3c4ZHBMVFRtNXZFVFd3d1R2dzJ3aXNISGRMbjgiLCJpc3MiOiJodHRwczovL2Rldi04MDUyNjIub2t0YXByZXZpZXcuY29tL29hdXRoMi9kZWZhdWx0IiwiYXVkIjoicmVzb3VyY2Utc2VydmVyIiwiaWF0IjoxNTI4ODYwMTkxLCJleHAiOjE1Mjg4NjM3OTEsImNpZCI6IjBvYWY1dTVnNG02Q1c0eDZ6MGg3Iiwic2NwIjpbIm9rIl0sInN1YiI6IjBvYWY1dTVnNG02Q1c0eDZ6MGg3In0.G_F9MQ3pqCy-YwfcNhryoPG5E1q4tQ7gV8OIDizR3QouUgrqT7MQsLQCTtGGLF2Fi0qq0Pr-V-wWa2MkyvcboEAhnfYi4rd3UmMrRTrNana6pVZjVWB_uj88-mZ57lFRnoYMCFbepmCxmY6D6p354H964xXWdtY7d6fw7F88DRDWMGQE0iQjMuUDg4izptVcK9db7uMonYTT1PFvOBQfwcn1zCeDVQgZFe7gjQA71CV9M6CIAXYDrpzp_hs95xco7Q3ncN3J7ZkCebLcUL6MdJS2nVuX6D6eC9PrtmCj06mb0-ydlzBSIUCPMaMQk9EhlEM_qK3d1iimCQnwo6KsIQ",
  53. "token_type": "Bearer",
  54. "expires_in": 3600,
  55. "scope": "ok"
  56. }
  57. ```
  58. Then, using that access token:
  59. ```bash
  60. curl -H "Authorization: Bearer eyJraWQiOiJFRjBFWDFFWHZGc1hGaDhuYkRGazNJN0hMUDBsZnJnc0JKMVdBWmkwRmI0IiwiYWxnIjoiUlMyNTYifQ.eyJ2ZXIiOjEsImp0aSI6IkFULmtQSUdfMEVMQmM3NVFMN3c4ZHBMVFRtNXZFVFd3d1R2dzJ3aXNISGRMbjgiLCJpc3MiOiJodHRwczovL2Rldi04MDUyNjIub2t0YXByZXZpZXcuY29tL29hdXRoMi9kZWZhdWx0IiwiYXVkIjoicmVzb3VyY2Utc2VydmVyIiwiaWF0IjoxNTI4ODYwMTkxLCJleHAiOjE1Mjg4NjM3OTEsImNpZCI6IjBvYWY1dTVnNG02Q1c0eDZ6MGg3Iiwic2NwIjpbIm9rIl0sInN1YiI6IjBvYWY1dTVnNG02Q1c0eDZ6MGg3In0.G_F9MQ3pqCy-YwfcNhryoPG5E1q4tQ7gV8OIDizR3QouUgrqT7MQsLQCTtGGLF2Fi0qq0Pr-V-wWa2MkyvcboEAhnfYi4rd3UmMrRTrNana6pVZjVWB_uj88-mZ57lFRnoYMCFbepmCxmY6D6p354H964xXWdtY7d6fw7F88DRDWMGQE0iQjMuUDg4izptVcK9db7uMonYTT1PFvOBQfwcn1zCeDVQgZFe7gjQA71CV9M6CIAXYDrpzp_hs95xco7Q3ncN3J7ZkCebLcUL6MdJS2nVuX6D6eC9PrtmCj06mb0-ydlzBSIUCPMaMQk9EhlEM_qK3d1iimCQnwo6KsIQ" \
  61. localhost:8081
  62. ```
  63. I get:
  64. ```bash
  65. Hello, 0oaf5u5g4m6CW4x6z0h7!
  66. ```
  67. == 3. Testing against other Authorization Servers
  68. The sample is already prepared to demonstrate integrations with a handful of other Authorization Servers. Do exercise
  69. one, simply uncomment two commented out sections, both in the application.yml file:
  70. ```yaml
  71. spring:
  72. security:
  73. oauth2:
  74. resourceserver:
  75. issuer:
  76. ```
  77. First, find the above section in the application.yml. Beneath it, you will see sections for each Authorization Server
  78. already prepared with the one for Okta commented out:
  79. ```yaml
  80. # master: #keycloak
  81. # issuer: http://localhost:8080/auth/realms/master
  82. # jwk-set-uri: http://localhost:8080/auth/realms/master/protocol/openid-connect/certs
  83. okta:
  84. issuer: https://dev-805262.oktapreview.com/oauth2/default
  85. jwk-set-uri: https://dev-805262.oktapreview.com/oauth2/default/v1/keys
  86. ```
  87. Comment out the `okta` section and uncomment the desired section.
  88. Second, find the following section, which the sample needs in order to retreive a valid token from the Authorization
  89. Server:
  90. ```yaml
  91. # ### keycloak
  92. # token-uri: http://localhost:8080/auth/realms/master/protocol/openid-connect/token
  93. # token-body:
  94. # grant_type: client_credentials
  95. # client-id: service
  96. # client-password: 9114712b-be55-4dab-b270-04734abda1c4
  97. # container:
  98. # config-file-name: keycloak.config
  99. # docker-file-name: keycloak.docker
  100. ### okta
  101. token-uri: https://dev-805262.oktapreview.com/oauth2/default/v1/token
  102. token-body:
  103. grant_type: client_credentials
  104. client-id: 0oaf5u5g4m6CW4x6z0h7
  105. client-password: HR7edRoo3glhF06HTxonOKZvO4I2BWYcC_ocOHlv
  106. ```
  107. Comment out the `okta` section and uncomment the desired section.
  108. === How can I test with my own Authorization Server instance?
  109. To test with your own Okta or other Authorization Server instance, simply provide the following information:
  110. ```yaml
  111. spring.security.oauth2.resourceserver.issuer.name.uri: the issuer uri
  112. spring.security.oauth2.resourceserver.issuer.name.jwk-set-uri: the jwk key uri
  113. ```
  114. And indicate, using the sample.provider properties, how the sample should generate a valid JWT token:
  115. ```yaml
  116. sample.provider.token-uri: the token endpoint
  117. sample.provider.token-body.grant_type: the grant to use
  118. sample.provider.token-body.another_property: another_value
  119. sample.provider.client-id: the client id
  120. sample.provider.client-password: the client password, only required for confidential clients
  121. ```
  122. You can provide values for any OAuth 2.0-compliant Authorization Server.