1. Overview
JJWT (Java JSON Web Token) 라이브러리를 최신 버전으로 업그레이드하면, deprecated 처리된 메서드나 삭제된 메서드 때문에 코드 변경이 필요할 수 있습니다. 여기에서는 JJWT 버전을 0.9.x에서 0.12.x로 마이그레이션 하는 방법을 설명합니다.
2. Migration
2.1. Maven dependency
먼저 Maven 의존성을 0.9.x에서 0.12.x로 업데이트합니다.
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.12.5</version>
</dependency>
2.2. Token 생성
// V 0.9.x
// SecretKey secretKey = new SecretKeySpec(strSecretKey.getBytes(), SignatureAlgorithm.HS256.getJcaName());
String token = Jwts.builder().claim(...).setExpiration(expireTimestamp).signWith(SignatureAlgorithm.HS512, strSecretKey).compact();
// V 0.12.x
SecretKey secretKey = Keys.hmacShaKeyFor(strSecretKey.getBytes());
String token = Jwts.builder().claim(...).expiration(expireTimestamp).signWith(secretKey).compact();
0.12.x 버전에서는 signWith 메서드가 SignatureAlgorithm와 키 문자열을 직접 받아들이는 방식을 더 이상 사용하지 않기 때문에, Keys.hmacShaKeyFor 메서드를 사용하여 SecretKey 객체를 생성하고, 이를 signWith 메서드에 전달해야 합니다.
2.3. Token 검증
// V 0.9.x
// SecretKey secretKey = new SecretKeySpec(strSecretKey.getBytes(), SignatureAlgorithm.HS256.getJcaName());
Claims claims = Jwts.parser().setSigningKey(strSecretKey).parseClaimsJws(spidSessionToken).getBody();
// V 0.12.x
SecretKey secretKey = Keys.hmacShaKeyFor(strSecretKey.getBytes());
Claims claims = Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload();
0.12.x 버전에서는 Jwts.parser()가 Jwts.parserBuilder()로 변경되었고, 빌더 패턴을 사용하여 verifyWith 메서드를 호출한 후 build()를 호출해야 합니다.
3. 참고사항
0.10.x 버전부터 jjwt 라이브러리에서 키 길이 검증이 강화되었습니다.
RFC 7518 표준에 따라 키 길이 검증이 강화되어, 충분한 길이의 키를 사용하지않을 경우 WeakKeyException이 발생합니다.
따라서, jjwt 라이브러리를 0.10.x 버전 이상으로 업그레이드하실 경우, 사용중인 키의 길이가 선택한 알고리즘의 요구 사항을 충족하는지 확인해야 합니다.
이러한 변경 사항을 적용하여 코드 호환성을 유지하면서 최신 JJWT 라이브러리를 사용할 수 있습니다.
'java' 카테고리의 다른 글
[JAVA] Google OTP (TOTP) 구현하기 (0) | 2025.01.13 |
---|---|
[JAVA16] stream().collect(Collectors.toList())와 stream().toList() (0) | 2024.07.03 |
[JAVA] JDK 11에서 JDK 21로 변경하기 (LTS버전) (0) | 2024.05.08 |
[Java21] Virtual Thread 요약 (0) | 2023.05.15 |
[Java11] Exception이 성능에 미치는 영향 (0) | 2023.03.19 |
댓글