|
| 1 | +# Keploy Sample Java - JWT Token Verification and Spring Boot |
| 2 | + |
| 3 | +This repository contains a sample project that demonstrates the integration of Keploy with JWT (JSON Web Token) authentication in a Spring Boot application. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Before getting started, make sure you have the following installed: |
| 8 | + |
| 9 | +- Latest version of JDK |
| 10 | +- Install [Keploy](https://keploy.io/docs/server/installation/) |
| 11 | +- Postman for testing APIs |
| 12 | + |
| 13 | +## Getting Started |
| 14 | + |
| 15 | +To get started, clone the repository: |
| 16 | + |
| 17 | +```bash |
| 18 | +git clone https://github.com/jaiakash/samples-java.git |
| 19 | +cd spring-boot-jwt |
| 20 | +``` |
| 21 | + |
| 22 | +## API Endpoints |
| 23 | + |
| 24 | +The following API endpoints are available: |
| 25 | + |
| 26 | +#### Login |
| 27 | + |
| 28 | +- POST `/users/login` |
| 29 | + |
| 30 | + Authenticate a user and receive a JWT token. |
| 31 | + |
| 32 | + Request Body: |
| 33 | + |
| 34 | + ```json |
| 35 | + { |
| 36 | + "username": "your_username", |
| 37 | + "password": "your_password" |
| 38 | + } |
| 39 | + ``` |
| 40 | + |
| 41 | + Response: |
| 42 | + |
| 43 | + ```json |
| 44 | + { |
| 45 | + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." |
| 46 | + } |
| 47 | + ``` |
| 48 | + |
| 49 | +#### Token Verification |
| 50 | + |
| 51 | +- POST `/users/tokenVerification` |
| 52 | + |
| 53 | + Verify the validity of a JWT token. |
| 54 | + |
| 55 | + Request Body: |
| 56 | + |
| 57 | + ```json |
| 58 | + { |
| 59 | + "token": "your_jwt_token_here" |
| 60 | + } |
| 61 | + ``` |
| 62 | + |
| 63 | + Response: |
| 64 | + |
| 65 | + ```json |
| 66 | + { |
| 67 | + "isValid": true |
| 68 | + } |
| 69 | + ``` |
| 70 | + |
| 71 | +## Integration with Keploy |
| 72 | + |
| 73 | +#### RECORD Mode |
| 74 | + |
| 75 | +1. To run the application, run |
| 76 | + |
| 77 | + ``` |
| 78 | + keploy run -c "./mvnw spring-boot:run" --delay 240 |
| 79 | + ``` |
| 80 | + |
| 81 | +2. To generate testcases, you can make API calls using Postman or `curl`: |
| 82 | + |
| 83 | +- Login |
| 84 | + |
| 85 | + ```bash |
| 86 | + curl --location --request POST 'http://localhost:8080/users/login' \ |
| 87 | + --header 'Content-Type: application/json' \ |
| 88 | + --data-raw '{ |
| 89 | + |
| 90 | + "password": "password" |
| 91 | + }' |
| 92 | + ``` |
| 93 | + |
| 94 | +- Verify |
| 95 | + |
| 96 | + ```bash |
| 97 | + curl --location --request POST 'http://localhost:8080/users/verify' \ |
| 98 | + --header 'Content-Type: application/json' \ |
| 99 | + --data-raw '{ |
| 100 | + "token": "your_jwt_token_here" |
| 101 | + }' |
| 102 | + ``` |
| 103 | + |
| 104 | +#### TEST mode |
| 105 | + |
| 106 | +To test the application, start Keploy in test mode. In the root directory, run the following command: |
| 107 | + |
| 108 | +```bash |
| 109 | +keploy test -c "./mvnw spring-boot:run" --delay 240 |
| 110 | +``` |
| 111 | + |
| 112 | +This command will run the tests and generate the report in the `Keploy/reports` directory in the current working directory. |
0 commit comments