1
1
package com .todo .todoapi .security ;
2
2
3
3
import java .nio .charset .StandardCharsets ;
4
- import java .security .Key ;
5
4
import java .time .Duration ;
6
5
import java .time .Instant ;
7
6
import java .util .Date ;
8
7
8
+ import javax .crypto .SecretKey ;
9
+
9
10
import io .jsonwebtoken .Claims ;
10
11
import io .jsonwebtoken .ExpiredJwtException ;
11
- import io .jsonwebtoken .Header ;
12
12
import io .jsonwebtoken .Jws ;
13
13
import io .jsonwebtoken .JwtParser ;
14
14
import io .jsonwebtoken .Jwts ;
15
15
import io .jsonwebtoken .MalformedJwtException ;
16
- import io .jsonwebtoken .SignatureAlgorithm ;
17
16
import io .jsonwebtoken .UnsupportedJwtException ;
18
17
import io .jsonwebtoken .security .Keys ;
19
18
import org .springframework .beans .factory .annotation .Value ;
@@ -36,19 +35,23 @@ public JwtTokenProvider(
36
35
}
37
36
38
37
public final String generateAccessToken (final String username ) {
39
- Key key = initializeKey (jwtSecret );
38
+ SecretKey key = initializeKey (jwtSecret );
40
39
Instant now = Instant .now ();
41
40
Instant expiry = now .plus (Duration .ofHours (12 ));
42
41
43
42
return Jwts .builder ()
44
- .setHeaderParam ("typ" , Header .JWT_TYPE )
45
- .setSubject (username )
46
- .setIssuedAt (Date .from (now ))
47
- .setExpiration (Date .from (expiry ))
48
- .setIssuer (jwtIssuer )
49
- .setAudience (jwtAudience )
50
- .setNotBefore (Date .from (now ))
51
- .signWith (key , SignatureAlgorithm .HS256 )
43
+ .header ()
44
+ .type ("JWT" )
45
+ .and ()
46
+ .subject (username )
47
+ .issuedAt (Date .from (now ))
48
+ .expiration (Date .from (expiry ))
49
+ .issuer (jwtIssuer )
50
+ .audience ()
51
+ .add (jwtAudience )
52
+ .and ()
53
+ .notBefore (Date .from (now ))
54
+ .signWith (key , Jwts .SIG .HS256 )
52
55
.compact ();
53
56
}
54
57
@@ -59,14 +62,14 @@ public final String getUsername(final String jwtToken) {
59
62
60
63
public final boolean validateToken (final String jwtToken ) {
61
64
try {
62
- Key key = initializeKey (jwtSecret );
65
+ SecretKey key = initializeKey (jwtSecret );
63
66
64
- Jwts .parserBuilder ()
67
+ Jwts .parser ()
65
68
.requireIssuer (jwtIssuer )
66
69
.requireAudience (jwtAudience )
67
- .setSigningKey (key )
70
+ .verifyWith (key )
68
71
.build ()
69
- .parseClaimsJws (jwtToken );
72
+ .parseSignedClaims (jwtToken );
70
73
return true ;
71
74
} catch (MalformedJwtException
72
75
| ExpiredJwtException
@@ -76,7 +79,7 @@ public final boolean validateToken(final String jwtToken) {
76
79
}
77
80
}
78
81
79
- private Key initializeKey (String jwtSecret ) {
82
+ private SecretKey initializeKey (String jwtSecret ) {
80
83
try {
81
84
return Keys .hmacShaKeyFor (jwtSecret .getBytes (StandardCharsets .UTF_8 ));
82
85
} catch (Exception e ) {
@@ -85,10 +88,10 @@ private Key initializeKey(String jwtSecret) {
85
88
}
86
89
87
90
private Claims getJwtClaims (final String jwtToken ) {
88
- Key key = initializeKey (jwtSecret );
91
+ SecretKey key = initializeKey (jwtSecret );
89
92
90
- JwtParser parser = Jwts .parserBuilder ().setSigningKey (key ).build ();
91
- Jws <Claims > jwt = parser .parseClaimsJws (jwtToken );
92
- return jwt .getBody ();
93
+ JwtParser parser = Jwts .parser ().verifyWith (key ).build ();
94
+ Jws <Claims > jwt = parser .parseSignedClaims (jwtToken );
95
+ return jwt .getPayload ();
93
96
}
94
97
}
0 commit comments