Skip to content

Commit bb2eedc

Browse files
committed
jwt added
1 parent c5599c3 commit bb2eedc

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

spring-boot-jwt/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545
<scope>test</scope>
4646
</dependency>
4747
<dependency>
48-
<groupId>org.springframework.security</groupId>
49-
<artifactId>spring-security-test</artifactId>
50-
<scope>test</scope>
51-
</dependency>
48+
<groupId>io.jsonwebtoken</groupId>
49+
<artifactId>jjwt</artifactId>
50+
<version>0.9.1</version>
51+
</dependency>
5252
</dependencies>
5353

5454
<build>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.akash.springboot.jwt;
2+
3+
import io.jsonwebtoken.Jwts;
4+
import io.jsonwebtoken.SignatureAlgorithm;
5+
import io.jsonwebtoken.SignatureException;
6+
import org.springframework.stereotype.Component;
7+
8+
import java.util.Date;
9+
10+
@Component
11+
public class JwtUtil {
12+
13+
private static final String SECRET_KEY = "your_secret_key";
14+
15+
public String generateToken(String username) {
16+
return Jwts.builder()
17+
.setSubject(username)
18+
.setIssuedAt(new Date())
19+
.setExpiration(new Date(System.currentTimeMillis() + 86400000)) // 1 day expiration
20+
.signWith(SignatureAlgorithm.HS256, SECRET_KEY)
21+
.compact();
22+
}
23+
24+
public boolean validateToken(String token) {
25+
try {
26+
Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token);
27+
return true;
28+
} catch (SignatureException e) {
29+
return false;
30+
}
31+
}
32+
}
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.akash.springboot.jwt;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.HttpStatus;
35
import org.springframework.http.ResponseEntity;
46
import org.springframework.web.bind.annotation.*;
57

@@ -12,18 +14,22 @@ public class UserController {
1214

1315
private Map<String, String> users = new HashMap<>();
1416

17+
@Autowired
18+
private JwtUtil jwtUtil;
19+
1520
@PostMapping("/login")
1621
public ResponseEntity<?> login(@RequestBody Map<String, String> user) {
1722
String username = user.get("username");
1823
String password = user.get("password");
1924
users.put(username, password);
2025

2126
// TODO: Generate JWT token
27+
String token = jwtUtil.generateToken(username);
2228

2329
Map<String, String> response = new HashMap<>();
24-
response.put("token", "demo-token");
25-
26-
return ResponseEntity.ok(response);
30+
response.put("token", token);
31+
System.err.println(token);
32+
return new ResponseEntity<>(response, HttpStatus.OK);
2733
}
2834

2935

@@ -32,11 +38,11 @@ public ResponseEntity<?> tokenAuthentication(@RequestBody Map<String, String> re
3238
String token = request.get("token");
3339

3440
//TODO: Validate token
35-
boolean isValid = true;
41+
boolean isValid = jwtUtil.validateToken(token);
3642

3743
Map<String, Boolean> response = new HashMap<>();
3844
response.put("isValid", isValid);
39-
40-
return ResponseEntity.ok(response);
45+
System.err.println(isValid);
46+
return new ResponseEntity<>(response, HttpStatus.OK);
4147
}
4248
}

0 commit comments

Comments
 (0)