Skip to content

Commit dd60c27

Browse files
authored
Merge pull request #2 from jaiakash/restApi
jwt added
2 parents 70b2efa + 642d4ad commit dd60c27

File tree

5 files changed

+69
-16
lines changed

5 files changed

+69
-16
lines changed

spring-boot-jwt/pom.xml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,9 @@
2727
<url/>
2828
</scm>
2929
<properties>
30-
<java.version>22</java.version>
30+
<java.version>17</java.version>
3131
</properties>
3232
<dependencies>
33-
<dependency>
34-
<groupId>org.springframework.boot</groupId>
35-
<artifactId>spring-boot-starter-security</artifactId>
36-
</dependency>
3733
<dependency>
3834
<groupId>org.springframework.boot</groupId>
3935
<artifactId>spring-boot-starter-web</artifactId>
@@ -45,9 +41,14 @@
4541
<scope>test</scope>
4642
</dependency>
4743
<dependency>
48-
<groupId>org.springframework.security</groupId>
49-
<artifactId>spring-security-test</artifactId>
50-
<scope>test</scope>
44+
<groupId>io.jsonwebtoken</groupId>
45+
<artifactId>jjwt</artifactId>
46+
<version>0.9.1</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>javax.xml.bind</groupId>
50+
<artifactId>jaxb-api</artifactId>
51+
<version>2.3.1</version>
5152
</dependency>
5253
</dependencies>
5354

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

8+
import java.io.UnsupportedEncodingException;
69
import java.util.HashMap;
710
import java.util.Map;
811

@@ -12,18 +15,30 @@ public class UserController {
1215

1316
private Map<String, String> users = new HashMap<>();
1417

18+
@Autowired
19+
private JwtUtil jwtUtil;
20+
21+
@GetMapping("/")
22+
public String index() {
23+
return "Greetings from Spring Boot!";
24+
}
25+
1526
@PostMapping("/login")
1627
public ResponseEntity<?> login(@RequestBody Map<String, String> user) {
1728
String username = user.get("username");
1829
String password = user.get("password");
1930
users.put(username, password);
2031

21-
// TODO: Generate JWT token
32+
try {
33+
String token = jwtUtil.generateToken(username); Map<String, String> response = new HashMap<>();
34+
response.put("token", token);
35+
System.err.println(token);
36+
return new ResponseEntity<>(response, HttpStatus.OK);
37+
} catch (UnsupportedEncodingException e) {
38+
e.printStackTrace();
39+
}
2240

23-
Map<String, String> response = new HashMap<>();
24-
response.put("token", "demo-token");
25-
26-
return ResponseEntity.ok(response);
41+
return new ResponseEntity<>("Invalid user", HttpStatus.UNAUTHORIZED);
2742
}
2843

2944

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

3449
//TODO: Validate token
35-
boolean isValid = true;
50+
boolean isValid = jwtUtil.validateToken(token);
3651

3752
Map<String, Boolean> response = new HashMap<>();
3853
response.put("isValid", isValid);
39-
40-
return ResponseEntity.ok(response);
54+
System.err.println(isValid);
55+
return new ResponseEntity<>(response, HttpStatus.OK);
4156
}
4257
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package com.akash.springboot.jwt;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
server. Port=8080
12
spring.application.name=springboot.jwt

0 commit comments

Comments
 (0)