1
1
package com .appsdeveloperblog .app .ws .shared ;
2
2
3
- import java .security .SecureRandom ;
4
- import java .util .Date ;
5
- import java .util .Random ;
6
- import org .springframework .stereotype .Service ;
7
-
8
3
import com .appsdeveloperblog .app .ws .security .SecurityConstants ;
9
4
import com .appsdeveloperblog .app .ws .security .TokenUtil ;
10
-
11
5
import io .jsonwebtoken .Claims ;
6
+ import io .jsonwebtoken .JwtException ;
12
7
import io .jsonwebtoken .Jwts ;
8
+ import java .security .SecureRandom ;
9
+ import java .util .Date ;
10
+ import java .util .Random ;
11
+ import org .springframework .stereotype .Service ;
13
12
14
13
@ Service
15
14
public class Utils {
@@ -35,40 +34,49 @@ private String generateRandomString(int length) {
35
34
}
36
35
37
36
public static boolean hasTokenExpired (String token ) {
38
-
39
- Claims claims = Jwts
40
- .parser ()
41
- .setSigningKey (TokenUtil .getSecretKey ())
42
- .parseClaimsJws (token )
43
- .getBody ();
37
+ boolean returnValue = false ;
38
+
39
+ try {
40
+ Claims claims = Jwts
41
+ .parser ()
42
+ .setSigningKey (TokenUtil .getSecretKey ())
43
+ .parseClaimsJws (token )
44
+ .getBody ();
44
45
45
- Date tokenExpirationDate = claims .getExpiration ();
46
- Date todayDate = new Date ();
47
-
48
- return tokenExpirationDate .before (todayDate );
46
+ Date tokenExpirationDate = claims .getExpiration ();
47
+ Date todayDate = new Date ();
48
+
49
+ return tokenExpirationDate .before (todayDate );
50
+ } catch (JwtException ex ) {
51
+ returnValue = true ;
52
+ }
53
+
54
+ return returnValue ;
49
55
}
50
56
51
57
public String generateEmailVerificationToken (String userId ) {
52
-
53
- String token = Jwts
58
+ String token = Jwts
54
59
.builder ()
55
60
.setId (userId )
56
- .setExpiration (new Date (System .currentTimeMillis () + SecurityConstants .EXPIRATION_TIME ))
61
+ .setExpiration (
62
+ new Date (System .currentTimeMillis () + SecurityConstants .EXPIRATION_TIME )
63
+ )
57
64
.signWith (TokenUtil .getSecretKey ())
58
65
.compact ();
59
66
60
- return token ;
67
+ return token ;
61
68
}
62
69
63
70
public String generatePasswordResetToken (String userId ) {
64
-
65
71
String token = Jwts
66
- .builder ()
67
- .setId (userId )
68
- .setExpiration (new Date (System .currentTimeMillis () + SecurityConstants .EXPIRATION_TIME ))
69
- .signWith (TokenUtil .getSecretKey ())
70
- .compact ();
71
-
72
+ .builder ()
73
+ .setId (userId )
74
+ .setExpiration (
75
+ new Date (System .currentTimeMillis () + SecurityConstants .EXPIRATION_TIME )
76
+ )
77
+ .signWith (TokenUtil .getSecretKey ())
78
+ .compact ();
79
+
72
80
return token ;
73
81
}
74
82
}
0 commit comments