-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJwtService.java
More file actions
108 lines (84 loc) Β· 3.23 KB
/
JwtService.java
File metadata and controls
108 lines (84 loc) Β· 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.runnect.server.config.jwt;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Header;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Base64;
import java.util.Date;
import javax.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.runnect.server.common.constant.TokenStatus;
import org.runnect.server.config.redis.RedisService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class JwtService {
@Value("${jwt.secret}")
private String jwtSecret;
private final long accessTokenExpiryTime = 1000L * 60 * 60 * 2; // 2μκ°
// private final long accessTokenExpiryTime = 1 * 60 * 1000L; // μλλ‘μ΄λ ν
μ€νΈμ©
private final long refreshTokenExpiryTime = 1000L * 60 * 60 * 24 * 14; // 2μ£Ό
private final String CLAIM_NAME = "userId";
private final RedisService redisService;
@PostConstruct
protected void init() {
jwtSecret = Base64.getEncoder()
.encodeToString(jwtSecret.getBytes(StandardCharsets.UTF_8));
}
// Access Token λ°κΈ
public String issuedAccessToken(Long userId) {
return issuedToken("access_token", accessTokenExpiryTime, userId.toString());
}
// Refresh Token λ°κΈ
public String issuedRefreshToken(Long userId) {
String refreshToken = issuedToken("refresh_token", refreshTokenExpiryTime, userId.toString());
redisService.setValues(String.valueOf(userId), refreshToken,refreshTokenExpiryTime);
return refreshToken;
}
// JWT ν ν° λ°κΈ
public String issuedToken(String tokenName, long expiryTime, String userId) {
final Date now = new Date();
final Claims claims = Jwts.claims()
.setSubject(tokenName)
.setIssuedAt(now)
.setExpiration(new Date(now.getTime() + expiryTime));
claims.put(CLAIM_NAME, userId);
return Jwts.builder()
.setHeaderParam(Header.TYPE, Header.JWT_TYPE)
.setClaims(claims)
.signWith(getSigningKey())
.compact();
}
private Key getSigningKey() {
final byte[] keyBytes = jwtSecret.getBytes(StandardCharsets.UTF_8);
return Keys.hmacShaKeyFor(keyBytes);
}
// JWT ν ν° κ²μ¦
public long verifyToken(String token) {
try {
final Claims claims = getBody(token);
return TokenStatus.TOKEN_VALID;
} catch (RuntimeException e) {
if (e instanceof ExpiredJwtException) {
return TokenStatus.TOKEN_EXPIRED;
}
return TokenStatus.TOKEN_INVALID;
}
}
private Claims getBody(final String token) {
return Jwts.parserBuilder()
.setSigningKey(getSigningKey())
.build()
.parseClaimsJws(token)
.getBody();
}
// JWT ν ν° λ΄μ© νμΈ
public String getJwtContents(String token) {
final Claims claims = getBody(token);
return (String) claims.get(CLAIM_NAME);
}
}