|
| 1 | +package com.back.global.security; |
| 2 | + |
| 3 | +import com.back.global.exception.CustomException; |
| 4 | +import com.back.global.exception.ErrorCode; |
| 5 | +import io.jsonwebtoken.Claims; |
| 6 | +import io.jsonwebtoken.ExpiredJwtException; |
| 7 | +import io.jsonwebtoken.JwtException; |
| 8 | +import io.jsonwebtoken.Jwts; |
| 9 | +import io.jsonwebtoken.security.Keys; |
| 10 | +import jakarta.annotation.PostConstruct; |
| 11 | +import org.springframework.beans.factory.annotation.Value; |
| 12 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 13 | +import org.springframework.security.core.Authentication; |
| 14 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 15 | +import org.springframework.stereotype.Component; |
| 16 | + |
| 17 | +import javax.crypto.SecretKey; |
| 18 | +import java.util.Date; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +/** |
| 22 | + * JWT 생성 및 검증을 담당하는 Provider 클래스 |
| 23 | + * - Access Token, Refresh Token 생성 |
| 24 | + * - 토큰 검증 및 파싱 |
| 25 | + * - Authentication 객체 생성 (Spring Security 연동) |
| 26 | + */ |
| 27 | +@Component |
| 28 | +public class JwtTokenProvider { |
| 29 | + |
| 30 | + @Value("${jwt.secret}") |
| 31 | + private String secretKey; |
| 32 | + |
| 33 | + @Value("${jwt.access-token-expiration}") |
| 34 | + private long accessTokenExpirationInSeconds; |
| 35 | + |
| 36 | + @Value("${jwt.refresh-token-expiration}") |
| 37 | + private long refreshTokenExpirationInSeconds; |
| 38 | + |
| 39 | + private SecretKey key; |
| 40 | + |
| 41 | + @PostConstruct |
| 42 | + public void init() { |
| 43 | + this.key = Keys.hmacShaKeyFor(secretKey.getBytes()); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Access Token 생성 |
| 48 | + * |
| 49 | + * @param userId 사용자 PK |
| 50 | + * @param username 로그인 ID |
| 51 | + * @param role 권한 |
| 52 | + * @return JWT Access Token 문자열 |
| 53 | + */ |
| 54 | + public String createAccessToken(Long userId, String username, String role) { |
| 55 | + Date now = new Date(); |
| 56 | + Date expiryDate = new Date(now.getTime() + accessTokenExpirationInSeconds * 1000); |
| 57 | + |
| 58 | + return Jwts.builder() |
| 59 | + .subject(username) |
| 60 | + .claim("userId", userId) |
| 61 | + .claim("role", role) |
| 62 | + .issuedAt(now) |
| 63 | + .expiration(expiryDate) |
| 64 | + .signWith(key) |
| 65 | + .compact(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Refresh Token 생성 |
| 70 | + * |
| 71 | + * @param userId 사용자 PK |
| 72 | + * @return JWT Refresh Token 문자열 |
| 73 | + */ |
| 74 | + public String createRefreshToken(Long userId) { |
| 75 | + Date now = new Date(); |
| 76 | + Date expiryDate = new Date(now.getTime() + refreshTokenExpirationInSeconds * 1000); |
| 77 | + |
| 78 | + return Jwts.builder() |
| 79 | + .subject(String.valueOf(userId)) |
| 80 | + .issuedAt(now) |
| 81 | + .expiration(expiryDate) |
| 82 | + .signWith(key) |
| 83 | + .compact(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * JWT 토큰에서 인증 정보 추출 |
| 88 | + * |
| 89 | + * @param token JWT Access Token |
| 90 | + * @return 인증 정보가 담긴 Authentication 객체 |
| 91 | + */ |
| 92 | + public Authentication getAuthentication(String token) { |
| 93 | + Claims claims = parseClaims(token); |
| 94 | + Long userId = claims.get("userId", Long.class); |
| 95 | + String username = claims.getSubject(); |
| 96 | + String role = claims.get("role", String.class); |
| 97 | + |
| 98 | + SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_" + role); |
| 99 | + CustomUserDetails principal = new CustomUserDetails(userId, username, role); |
| 100 | + |
| 101 | + return new UsernamePasswordAuthenticationToken(principal, token, List.of(authority)); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * JWT 토큰 검증 |
| 106 | + * |
| 107 | + * @param token JWT Access Token |
| 108 | + * @return 유효한 토큰이면 true, 그렇지 않으면 false |
| 109 | + */ |
| 110 | + public boolean validateToken(String token) { |
| 111 | + try { |
| 112 | + Jwts.parser() |
| 113 | + .verifyWith(key) |
| 114 | + .build() |
| 115 | + .parseSignedClaims(token); |
| 116 | + return true; |
| 117 | + } catch (JwtException e) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * JWT 파싱 |
| 124 | + * |
| 125 | + * @param token JWT 토큰 |
| 126 | + * @return 토큰의 Claims |
| 127 | + * @throws CustomException 토큰이 유효하지 않은 경우 |
| 128 | + */ |
| 129 | + private Claims parseClaims(String token) { |
| 130 | + try { |
| 131 | + return Jwts.parser() |
| 132 | + .verifyWith(key) |
| 133 | + .build() |
| 134 | + .parseSignedClaims(token) |
| 135 | + .getPayload(); |
| 136 | + } catch (ExpiredJwtException e) { |
| 137 | + return e.getClaims(); |
| 138 | + } catch (JwtException e) { |
| 139 | + throw new CustomException(ErrorCode.INVALID_TOKEN); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments