Skip to content

Commit

Permalink
fix: jwt verfication, add: error mssg, docs:docker instllation
Browse files Browse the repository at this point in the history
Signed-off-by: Akash Jaiswal <[email protected]>
  • Loading branch information
jaiakash committed Jul 26, 2024
1 parent 84f5399 commit 80d3aac
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
29 changes: 22 additions & 7 deletions spring-boot-jwt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`:
Expand All @@ -96,16 +115,12 @@ The following API endpoints are available:
--header 'Authorization: Bearer <your_jwt_token_here>'
```

```
```

#### 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.
44 changes: 44 additions & 0 deletions spring-boot-jwt/keploy.yml
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public ResponseEntity<?> login(@RequestBody Map<String, String> 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<String, Boolean> response = new HashMap<>();
Expand Down

0 comments on commit 80d3aac

Please sign in to comment.