README.adoc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. This resource server is configured to accept JWE-encrypted tokens.
  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 decrypts the token and subsquently verifies it 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=eyJjdHkiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiUlNBLU9BRVAtMjU2In0.IyeWtsTonaiWJdoT13B0M7gpqVxAirVGlfqFI4TOmTRcVHICs_ESezS7fa0ODS9XYdwklTtG7hH39yeeMzr2Zo1Ghh-m36fdoqQrV1Do04rUvuTjqbgyNffeZEGB6rquJ-cyAVjp_Oljy10-Bbnw7CeVGwNBSVo9UCB5j49OlNWhLxFpYARFmOlYpXj-s4Q4JiqV6EvjDAYeohAR4QQmND3qoxR-s2I6SLcIho0sSSpUlhrRiqu2uvWefHDcZJdW2WYWnxLHxhzNu3CfnLiqhhaA_YA_iWXR9FYnPDCf_4q3FgSXcgttXzomFKAx5DwnE_dXvsCvpWxslZMU1UIiLA.MHOrrza2GQ9_5PIv.zU4tfhxT6apWBC3stBwQmGlCQBltWVQe4dFIykybWWBFqxo1bf2BZ37twzoEIFXG9jSYEMH4mvBXPWSvn66t-_jnqLnKTJst2plBjhagGCAoLNWXVKeYNp67o-lKOD_JJQFqsRw4oE05VSgRr14MZeaUBFcU3A_kKxMXOu899DKfXBGJvj75H7lDyd8RUXTb-OSWWfUiJc6Y5AUy1zCZCN9yfDsCXt9heTsZANy92Oou9sMFaXkYzyums5OtkBtLFzyuNMEoNioRehTV-FTuL8tDRB1mNhHObwsBfFbR6M1jJK37pHUXGtko-yZ6NGwxyLtwGh4uU2jzE614rQzuzR8aHaHxOkUs1pBTZ8AcRt41snByOe-KU0adthHxedobFiQQBoQ05DgSU7DO6hsK0uVBDF3eG2KjH4L2lZy-WugloLHhdguUoO9F0zUx15-XZO4EVzmhy8xfH2tSXz98eKzz9Dv0DdGnrBL9cK2MM88N1zoq5u4NdlnE12HvuesB7GKdMwZx1-gTw_pzP81TzcctJWl6ETK09Uc.jk0O8qc4Fvip856stDz05Q
  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=eyJjdHkiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiUlNBLU9BRVAtMjU2In0.CRBAEgvQhpB6pPQhpkTAKpsDai1FDcvkDSRig1R3OI-g18JdTe-qDhzWwP-hV3aCwFwHxQ_g8Z8OIZvhyKpQaPwBb72UeLqqhzSIkm0gEsmmjYg1vEGOrDH5_Fqlm0LnAnXTmsbOIWYIj11ZuenI2lEmMCkVwqth0RlzakdcHRXiuDTEln_trhZpE2j80X-9rS2gZy9Raa9VLir3-F3wC0GKPEL6e3x1OygC03ix9uyXS3vpTsU9zlgoYADZyaLeOF1mCG4mQhvXs7IPmPbwNsElJwKh0xSQCHvNOQShprlvd3cHiUFKYw9fXphY1O-AUYcRzHk4DjoBdkGNQMy_Kw.KtC_z674rYBtDgkN.e8QU50Iq1JHkn-1USSxpjEkbrukb4cobvlQRK40iXGAKVIuOod4bSq5fDpIAPHugqIf-_zGsvr-2OCOdzhtBikL46wU7UdZppxPWtk-X6kl33zH_XObRMaGfe-hLxt3RPxRVn7I1Hp6tGW1Rkxyf_ESq4XlcbbrkhDoIz_G_LKXJhvQ-xahW2e0AUc7RZSucns4XUeq9xX_dd7Ht-o1TmQI9WFoFc1l7oh9GtQ6GZMsghnZ1VrbIS2L7jSYiSsD2JqSv1LLtOGj_FBA0ufhqM3LloGiwflEwAryMD10oNb73WonKEycEj1rBsTIKW7SHkI-VkrQA4-8N-aLWgHwDnzyPZmyNyKpqUMvhjIE_0w6oqU4HpN7J5nfBEIAtpPZ_pDkwAdxCQ7JV3zfiUnF7ZQ3q1PnSId315si02ZN9-wRSrMHcHnboQN1Hs4xCAfGyClVyLpCzfa_fAehjt6v1DjgjbzwSjr_LdNmWTvXYBhNO8Jq9Vb7axksrdwksD3pYNMY8cRZxP-LO0V5Sv1_kT_Hf2yLo2iTwB8n8szzGrJ4QQLb5Znu7Sv-M2x52cnIDMiorP3LNpFk.G85FuMSm-8bGumFAStiFQA
  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 encrypt using the public key available in the sample.
  51. Also it must support JWTs that either use the "scope" or "scp" attribute._
  52. To change the sample to point at your Authorization Server, simply find this property in the `application.yml`:
  53. ```yaml
  54. spring:
  55. security:
  56. oauth2:
  57. resourceserver:
  58. jwt:
  59. jwk-set-uri: ${mockwebserver.url}/.well-known/jwks.json
  60. ```
  61. And change the property to your Authorization Server's JWK set endpoint:
  62. ```yaml
  63. spring:
  64. security:
  65. oauth2:
  66. resourceserver:
  67. jwt:
  68. jwk-set-uri: https://localhost:9031/pf/JWKS
  69. ```
  70. If your Authorization Server does not support RSA_OAEP_256 or AESGCM, then you can change these values in `OAuth2ResourceServerSecurityConfiguration`:
  71. ```java
  72. ```
  73. And then you can run the app the same as before:
  74. ```bash
  75. ./gradlew bootRun
  76. ```
  77. Make sure to obtain valid tokens from your Authorization Server in order to play with the sample Resource Server.
  78. To use the `/` endpoint, any valid token from your Authorization Server will do.
  79. To use the `/message` endpoint, the token should have the `message:read` scope.