-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJwtTokenProvider.java
More file actions
67 lines (56 loc) · 2.21 KB
/
Copy pathJwtTokenProvider.java
File metadata and controls
67 lines (56 loc) · 2.21 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
package com.knoc.auth.jwt;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.security.Key;
import java.util.Date;
@Slf4j
@Component
public class JwtTokenProvider {
private final Key key;
private final long accessTokenValidityTime = 1000L * 60 * 30; // 30분
private final long refreshTokenValidityTime = 1000L * 60 * 60 * 24 * 7; //7일
// Access Token 생성
public JwtTokenProvider(@Value("${jwt.secret:vmsqi69s1iq93p01dnscms938s9d10s2j3nzmcl0s9}")String secretKey) {
this.key = Keys.hmacShaKeyFor(secretKey.getBytes());
}
public String createAccessToken(String email, String role) {
Claims claims = Jwts.claims().setSubject(email);
claims.put("role", role);
Date now = new Date();
return Jwts.builder()
.setClaims(claims)
.setIssuedAt((now)) // 발급 시간
.setExpiration(new Date(now.getTime() + accessTokenValidityTime)) // 만료 시간
.signWith(key, SignatureAlgorithm.HS256)
.compact();
}
// refreshToken 생성(수명 7일, 이메일 저장)
public String createRefreshToken(String email) {
Date now = new Date();
return Jwts.builder()
.setSubject(email)
.setIssuedAt(now)
.setExpiration(new Date(now.getTime() + refreshTokenValidityTime))
.signWith(key, SignatureAlgorithm.HS256)
.compact();
}
public boolean validateToken(String token) {
try {
Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token);
return true;
} catch (Exception e) {
log.error("JWT 검증 실패: {}", e.getMessage());
return false;
}
}
// 토큰에서 이메일 추출
public String getEmailFromToken(String token) {
return Jwts.parserBuilder().setSigningKey(key).build()
.parseClaimsJws(token).getBody().getSubject();
}
}