Skip to content

Commit 639f0e3

Browse files
committed
Removed deprecated JWT methods from JwtTokenProvider
1 parent 7046621 commit 639f0e3

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@
8686
<dependency>
8787
<groupId>org.springdoc</groupId>
8888
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
89-
<version>2.0.4</version>
89+
<version>2.3.0</version>
9090
</dependency>
91+
9192
</dependencies>
9293

9394
<build>

src/main/java/com/springboot/blog/controller/AuthController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.springframework.web.bind.annotation.RestController;
1313

1414
@RestController
15-
@RequestMapping("/api/v1/auth")
15+
@RequestMapping("/api/auth")
1616
public class AuthController {
1717

1818
private AuthService authService;
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.springboot.blog.security;
22

33
import com.springboot.blog.exception.BlogAPIException;
4-
import io.jsonwebtoken.*;
4+
import io.jsonwebtoken.ExpiredJwtException;
5+
import io.jsonwebtoken.Jwts;
6+
import io.jsonwebtoken.MalformedJwtException;
7+
import io.jsonwebtoken.UnsupportedJwtException;
58
import io.jsonwebtoken.io.Decoders;
69
import io.jsonwebtoken.security.Keys;
710
import org.springframework.beans.factory.annotation.Value;
811
import org.springframework.http.HttpStatus;
912
import org.springframework.security.core.Authentication;
1013
import org.springframework.stereotype.Component;
1114

15+
import javax.crypto.SecretKey;
1216
import java.security.Key;
1317
import java.util.Date;
1418

@@ -23,54 +27,54 @@ public class JwtTokenProvider {
2327

2428
// generate JWT token
2529
public String generateToken(Authentication authentication){
30+
2631
String username = authentication.getName();
2732

2833
Date currentDate = new Date();
2934

3035
Date expireDate = new Date(currentDate.getTime() + jwtExpirationDate);
3136

3237
String token = Jwts.builder()
33-
.setSubject(username)
34-
.setIssuedAt(new Date())
35-
.setExpiration(expireDate)
38+
.subject(username)
39+
.issuedAt(new Date())
40+
.expiration(expireDate)
3641
.signWith(key())
3742
.compact();
43+
3844
return token;
3945
}
4046

4147
private Key key(){
42-
return Keys.hmacShaKeyFor(
43-
Decoders.BASE64.decode(jwtSecret)
44-
);
48+
return Keys.hmacShaKeyFor(Decoders.BASE64.decode(jwtSecret));
4549
}
4650

47-
// get username from Jwt token
51+
// get username from JWT token
4852
public String getUsername(String token){
49-
Claims claims = Jwts.parser()
50-
.setSigningKey(key())
53+
54+
return Jwts.parser()
55+
.verifyWith((SecretKey) key())
5156
.build()
52-
.parseClaimsJws(token)
53-
.getBody();
54-
String username = claims.getSubject();
55-
return username;
57+
.parseSignedClaims(token)
58+
.getPayload()
59+
.getSubject();
5660
}
5761

58-
// validate Jwt token
62+
// validate JWT token
5963
public boolean validateToken(String token){
6064
try{
6165
Jwts.parser()
62-
.setSigningKey(key())
66+
.verifyWith((SecretKey) key())
6367
.build()
6468
.parse(token);
6569
return true;
66-
} catch (MalformedJwtException ex) {
67-
throw new BlogAPIException(HttpStatus.BAD_REQUEST, "Invalid JWT token");
68-
} catch (ExpiredJwtException ex) {
70+
}catch (MalformedJwtException malformedJwtException){
71+
throw new BlogAPIException(HttpStatus.BAD_REQUEST, "Invalid JWT Token");
72+
}catch (ExpiredJwtException expiredJwtException){
6973
throw new BlogAPIException(HttpStatus.BAD_REQUEST, "Expired JWT token");
70-
} catch (UnsupportedJwtException ex) {
74+
}catch (UnsupportedJwtException unsupportedJwtException){
7175
throw new BlogAPIException(HttpStatus.BAD_REQUEST, "Unsupported JWT token");
72-
} catch (IllegalArgumentException ex) {
73-
throw new BlogAPIException(HttpStatus.BAD_REQUEST, "JWT claims string is empty.");
76+
}catch (IllegalArgumentException illegalArgumentException){
77+
throw new BlogAPIException(HttpStatus.BAD_REQUEST, "Jwt claims string is null or empty");
7478
}
7579
}
76-
}
80+
}

0 commit comments

Comments
 (0)