Skip to content

Commit 80d3aac

Browse files
committed
fix: jwt verfication, add: error mssg, docs:docker instllation
Signed-off-by: Akash Jaiswal <akashjaiswal3846@gmail.com>
1 parent 84f5399 commit 80d3aac

4 files changed

Lines changed: 76 additions & 9 deletions

File tree

spring-boot-jwt/README.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Before getting started, make sure you have the following installed:
88

99
- Latest version of JDK
1010
- Install [Keploy](https://keploy.io/docs/server/installation/)
11+
- Install [Docker](https://docs.docker.com/engine/install/)
1112
- Postman for testing APIs
1213

1314
## Getting Started
@@ -66,14 +67,32 @@ The following API endpoints are available:
6667
}
6768
```
6869

70+
## Running with Docker
71+
72+
To run the application with Docker, follow these steps:
73+
74+
1. Build the Docker image:
75+
76+
```bash
77+
docker build -t spring-boot-jwt .
78+
```
79+
80+
2. Run the Docker container:
81+
82+
```bash
83+
docker run -p 8080:8080 spring-boot-jwt
84+
```
85+
86+
The application will be accessible at `http://localhost:8080`.
87+
6988
## Integration with Keploy
7089

7190
#### RECORD Mode
7291

7392
1. To run the application, run
7493

75-
```
76-
keploy run -c "./mvnw spring-boot:run" --delay 240
94+
```bash
95+
keploy record -c "docker run -p 8080:8080 spring-boot-jwt"
7796
```
7897

7998
2. To generate testcases, you can make API calls using Postman or `curl`:
@@ -96,16 +115,12 @@ The following API endpoints are available:
96115
--header 'Authorization: Bearer <your_jwt_token_here>'
97116
```
98117

99-
```
100-
101-
```
102-
103118
#### TEST mode
104119

105120
To test the application, start Keploy in test mode. In the root directory, run the following command:
106121

107122
```bash
108-
keploy test -c "./mvnw spring-boot:run" --delay 240
123+
keploy test -c "docker run -p 8080:8080 spring-boot-jwt" --delay 30
109124
```
110125

111126
This command will run the tests and generate the report in the `Keploy/reports` directory in the current working directory.

spring-boot-jwt/keploy.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
path: ""
2+
appId: 0
3+
appName: ""
4+
command: ./mvnw spring-boot:run
5+
port: 0
6+
dnsPort: 26789
7+
proxyPort: 16789
8+
debug: false
9+
disableTele: false
10+
disableANSI: false
11+
containerName: ""
12+
networkName: ""
13+
buildDelay: 30
14+
test:
15+
selectedTests: {}
16+
globalNoise:
17+
global:
18+
body: {"token": ["*"]}
19+
test-sets: {}
20+
delay: 5
21+
apiTimeout: 5
22+
skipCoverage: false
23+
coverageReportPath: ""
24+
ignoreOrdering: true
25+
mongoPassword: default@123
26+
language: ""
27+
removeUnusedMocks: false
28+
fallBackOnMiss: false
29+
jacocoAgentPath: ""
30+
basePath: ""
31+
mocking: true
32+
ignoredTests: {}
33+
record:
34+
filters: []
35+
recordTimer: 0s
36+
configPath: ""
37+
bypassRules: []
38+
generateGithubActions: true
39+
keployContainer: keploy-v2
40+
keployNetwork: keploy-network
41+
cmdType: native
42+
inCi: false
43+
44+
# Visit [https://keploy.io/docs/running-keploy/configuration-file/] to learn about using keploy through configration file.

spring-boot-jwt/src/main/java/com/akash/springboot/jwt/JwtUtil.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.akash.springboot.jwt;
22

3+
import io.jsonwebtoken.ExpiredJwtException;
34
import io.jsonwebtoken.Jwts;
45
import io.jsonwebtoken.SignatureAlgorithm;
56
import io.jsonwebtoken.SignatureException;
@@ -27,11 +28,17 @@ public String generateToken(String username) throws UnsupportedEncodingException
2728
public boolean validateToken(String token) {
2829
try {
2930
Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token);
31+
System.err.println("Valid");
3032
return true;
3133
} catch (SignatureException e) {
34+
System.err.println("Signature Exception");
35+
return false;
36+
} catch (ExpiredJwtException e) {
37+
System.err.println("ExpiredJwtException");
3238
return false;
3339
} catch (Exception e) {
40+
System.err.println("Other Exception");
3441
return false;
3542
}
3643
}
37-
}
44+
}

spring-boot-jwt/src/main/java/com/akash/springboot/jwt/UserController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public ResponseEntity<?> login(@RequestBody Map<String, String> user) {
4343
}
4444

4545
@PostMapping("/tokenVerification")
46-
public ResponseEntity<?> tokenAuthentication(@RequestHeader("Authorization") String token) {
46+
public ResponseEntity<?> tokenAuthentication(@RequestHeader("Authorization") String authorizationHeader) {
47+
String token = authorizationHeader.replace("Bearer ", "");
4748
boolean isValid = jwtUtil.validateToken(token);
4849

4950
Map<String, Boolean> response = new HashMap<>();

0 commit comments

Comments
 (0)