1
1
package com .springboot .blog .security ;
2
2
3
3
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 ;
5
8
import io .jsonwebtoken .io .Decoders ;
6
9
import io .jsonwebtoken .security .Keys ;
7
10
import org .springframework .beans .factory .annotation .Value ;
8
11
import org .springframework .http .HttpStatus ;
9
12
import org .springframework .security .core .Authentication ;
10
13
import org .springframework .stereotype .Component ;
11
14
15
+ import javax .crypto .SecretKey ;
12
16
import java .security .Key ;
13
17
import java .util .Date ;
14
18
@@ -23,54 +27,54 @@ public class JwtTokenProvider {
23
27
24
28
// generate JWT token
25
29
public String generateToken (Authentication authentication ){
30
+
26
31
String username = authentication .getName ();
27
32
28
33
Date currentDate = new Date ();
29
34
30
35
Date expireDate = new Date (currentDate .getTime () + jwtExpirationDate );
31
36
32
37
String token = Jwts .builder ()
33
- .setSubject (username )
34
- .setIssuedAt (new Date ())
35
- .setExpiration (expireDate )
38
+ .subject (username )
39
+ .issuedAt (new Date ())
40
+ .expiration (expireDate )
36
41
.signWith (key ())
37
42
.compact ();
43
+
38
44
return token ;
39
45
}
40
46
41
47
private Key key (){
42
- return Keys .hmacShaKeyFor (
43
- Decoders .BASE64 .decode (jwtSecret )
44
- );
48
+ return Keys .hmacShaKeyFor (Decoders .BASE64 .decode (jwtSecret ));
45
49
}
46
50
47
- // get username from Jwt token
51
+ // get username from JWT token
48
52
public String getUsername (String token ){
49
- Claims claims = Jwts .parser ()
50
- .setSigningKey (key ())
53
+
54
+ return Jwts .parser ()
55
+ .verifyWith ((SecretKey ) key ())
51
56
.build ()
52
- .parseClaimsJws (token )
53
- .getBody ();
54
- String username = claims .getSubject ();
55
- return username ;
57
+ .parseSignedClaims (token )
58
+ .getPayload ()
59
+ .getSubject ();
56
60
}
57
61
58
- // validate Jwt token
62
+ // validate JWT token
59
63
public boolean validateToken (String token ){
60
64
try {
61
65
Jwts .parser ()
62
- .setSigningKey ( key ())
66
+ .verifyWith (( SecretKey ) key ())
63
67
.build ()
64
68
.parse (token );
65
69
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 ) {
69
73
throw new BlogAPIException (HttpStatus .BAD_REQUEST , "Expired JWT token" );
70
- } catch (UnsupportedJwtException ex ) {
74
+ }catch (UnsupportedJwtException unsupportedJwtException ) {
71
75
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" );
74
78
}
75
79
}
76
- }
80
+ }
0 commit comments