From 80d3aac821b4dc74a1f07d8a4586d8bd31b1e7d2 Mon Sep 17 00:00:00 2001 From: Akash Jaiswal Date: Fri, 26 Jul 2024 17:49:41 +0530 Subject: [PATCH] fix: jwt verfication, add: error mssg, docs:docker instllation Signed-off-by: Akash Jaiswal --- spring-boot-jwt/README.md | 29 +++++++++--- spring-boot-jwt/keploy.yml | 44 +++++++++++++++++++ .../com/akash/springboot/jwt/JwtUtil.java | 9 +++- .../akash/springboot/jwt/UserController.java | 3 +- 4 files changed, 76 insertions(+), 9 deletions(-) create mode 100755 spring-boot-jwt/keploy.yml diff --git a/spring-boot-jwt/README.md b/spring-boot-jwt/README.md index 28e4dab..f0897fe 100644 --- a/spring-boot-jwt/README.md +++ b/spring-boot-jwt/README.md @@ -8,6 +8,7 @@ Before getting started, make sure you have the following installed: - Latest version of JDK - Install [Keploy](https://keploy.io/docs/server/installation/) +- Install [Docker](https://docs.docker.com/engine/install/) - Postman for testing APIs ## Getting Started @@ -66,14 +67,32 @@ The following API endpoints are available: } ``` +## Running with Docker + +To run the application with Docker, follow these steps: + +1. Build the Docker image: + + ```bash + docker build -t spring-boot-jwt . + ``` + +2. Run the Docker container: + + ```bash + docker run -p 8080:8080 spring-boot-jwt + ``` + +The application will be accessible at `http://localhost:8080`. + ## Integration with Keploy #### RECORD Mode 1. To run the application, run - ``` - keploy run -c "./mvnw spring-boot:run" --delay 240 + ```bash + keploy record -c "docker run -p 8080:8080 spring-boot-jwt" ``` 2. To generate testcases, you can make API calls using Postman or `curl`: @@ -96,16 +115,12 @@ The following API endpoints are available: --header 'Authorization: Bearer ' ``` - ``` - - ``` - #### TEST mode To test the application, start Keploy in test mode. In the root directory, run the following command: ```bash -keploy test -c "./mvnw spring-boot:run" --delay 240 +keploy test -c "docker run -p 8080:8080 spring-boot-jwt" --delay 30 ``` This command will run the tests and generate the report in the `Keploy/reports` directory in the current working directory. diff --git a/spring-boot-jwt/keploy.yml b/spring-boot-jwt/keploy.yml new file mode 100755 index 0000000..8bc0e9a --- /dev/null +++ b/spring-boot-jwt/keploy.yml @@ -0,0 +1,44 @@ +path: "" +appId: 0 +appName: "" +command: ./mvnw spring-boot:run +port: 0 +dnsPort: 26789 +proxyPort: 16789 +debug: false +disableTele: false +disableANSI: false +containerName: "" +networkName: "" +buildDelay: 30 +test: + selectedTests: {} + globalNoise: + global: + body: {"token": ["*"]} + test-sets: {} + delay: 5 + apiTimeout: 5 + skipCoverage: false + coverageReportPath: "" + ignoreOrdering: true + mongoPassword: default@123 + language: "" + removeUnusedMocks: false + fallBackOnMiss: false + jacocoAgentPath: "" + basePath: "" + mocking: true + ignoredTests: {} +record: + filters: [] + recordTimer: 0s +configPath: "" +bypassRules: [] +generateGithubActions: true +keployContainer: keploy-v2 +keployNetwork: keploy-network +cmdType: native +inCi: false + +# Visit [https://keploy.io/docs/running-keploy/configuration-file/] to learn about using keploy through configration file. diff --git a/spring-boot-jwt/src/main/java/com/akash/springboot/jwt/JwtUtil.java b/spring-boot-jwt/src/main/java/com/akash/springboot/jwt/JwtUtil.java index 202c62d..02bb70f 100644 --- a/spring-boot-jwt/src/main/java/com/akash/springboot/jwt/JwtUtil.java +++ b/spring-boot-jwt/src/main/java/com/akash/springboot/jwt/JwtUtil.java @@ -1,5 +1,6 @@ package com.akash.springboot.jwt; +import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.SignatureException; @@ -27,11 +28,17 @@ public String generateToken(String username) throws UnsupportedEncodingException public boolean validateToken(String token) { try { Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token); + System.err.println("Valid"); return true; } catch (SignatureException e) { + System.err.println("Signature Exception"); + return false; + } catch (ExpiredJwtException e) { + System.err.println("ExpiredJwtException"); return false; } catch (Exception e) { + System.err.println("Other Exception"); return false; } } -} +} \ No newline at end of file diff --git a/spring-boot-jwt/src/main/java/com/akash/springboot/jwt/UserController.java b/spring-boot-jwt/src/main/java/com/akash/springboot/jwt/UserController.java index a82e792..51b3a40 100644 --- a/spring-boot-jwt/src/main/java/com/akash/springboot/jwt/UserController.java +++ b/spring-boot-jwt/src/main/java/com/akash/springboot/jwt/UserController.java @@ -43,7 +43,8 @@ public ResponseEntity login(@RequestBody Map user) { } @PostMapping("/tokenVerification") - public ResponseEntity tokenAuthentication(@RequestHeader("Authorization") String token) { + public ResponseEntity tokenAuthentication(@RequestHeader("Authorization") String authorizationHeader) { + String token = authorizationHeader.replace("Bearer ", ""); boolean isValid = jwtUtil.validateToken(token); Map response = new HashMap<>();