Skip to content

gregwhitaker/ratpack-jwt-example

Repository files navigation

ratpack-jwt-example

Build Status

An example of using JSON Web tokens to authenticate with Ratpack.

This example starts a Ratpack application that hosts three endpoints: one secured with JWT as a url parameter, one secured with JWT as an Authorization header, and one that is not secured.

Prerequisites

This example requires that you have installed the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.

Running the Example

  1. Generate a READ_WRITE role JWT token by running the following Gradle command:

     $ ./gradlew generateReadWrite
    
  2. Copy the generated token to the clipboard (It will look similar to the one below):

     eyJjdHkiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiZGlyIn0..rYmzSzVwurf_tJRk.VL_IzEqi9bihjSd44W8HCTIm2tfVqMTCAHklSxbsteO67mZ0nqxXfeo7h7D0tWy0Wk4Hg7jJngBu5Thd2m5S27xUlS9YsakkROmIOkdnsCs5j4mv5Qd9PgPRxSWAH2qC_K7fywpGlUoPojGO03MA0odbR9PPS4WpuBlealgKF2gAbcfADokyNy897AehcmEJ6ZRwzP--vKcCP59Z1CSqHEEgwLGgXOOQl7PAMgvhOHJwJJl5OvhHcQRg9vG89CegONXFypZNWDGv0LtcCjJ-H8_ZH-wWtkqjXyC2m9QipORc_Of-uLov-kJeuIRkT-M3DqwIyqofaO7MDv7xAuCtk_jpl0uN.UNev8gOwcbEhr9fJOCA5BQ
    
  3. Start the example service by running the following Gradle command:

     $ ./gradlew run
    
  4. Curl the JWT protected endpoint using the following command:

     $ curl -v http://localhost:5050/secure
    
  5. Notice that you receive a 401 - Unauthorized error.

  6. Curl the JWT protected endpoint and supply the JWT token:

    If you wish to supply the JWT token as a URL parameter use the following command:

     $ curl -v http://localhost:5050/secure?token={your token}
    

    If you wish to supply the JWT token in the Authorization header use the following command:

     $ curl -v -H "Authorization: Bearer {your token}" http://localhost:5050/secureheader
    
  7. Notice that you now have access to the secure resource and your user information.

     {
       "message": "This endpoint is protected by JWT",
       "user": {
         "displayName": "jdoe",
         "roles": [
           "READ_WRITE"
         ],
         "email": "john.doe@netifi.com"
       }
     }
    

Bugs and Feedback

For bugs, questions and discussions please use the Github Issues.

License

MIT License

Copyright (c) 2017 Greg Whitaker

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Example of using JSON Web Tokens to authenticate with Ratpack

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages