Troubleshooting
[TroubleShooting] io.jsonwebtoken.security.WeakKeyException 예외 해결
DuL2
2024. 1. 10. 23:53
TroubleShooting
발생한 문제
프로젝트를 할 때 JWT를 통한 인증, 인가 절차를 Security 없이 만들어보는 과정에서 예외가 터졌다. 비교적 쉬운 예외였다. 친절하게 다음과 같은 설명이 써있다.
The specified key byte array is 248 bits which is not secure enough for any JWT HMAC-SHA algorithm. The JWT JWA Specification (RFC 7518, Section 3.2) states that keys used with HMAC-SHA algorithms MUST have a size >= 256 bits (the key size must be greater than or equal to the hash output size). Consider using the io.jsonwebtoken.security.Keys#secretKeyFor(SignatureAlgorithm) method to create a key guaranteed to be secure enough for your preferred HMAC-SHA algorithm. See https://tools.ietf.org/html/rfc7518#section-3.2 for more information.
내 키는 248 비트고 최소 256 비트 이상의 키를 써야한다는 것. 익셉션명부터가 아주 직관적이다.
해결 및 시도 방법
key값 자체를 256비트가 넘도록 길게 설정해주면 된다.
세부사항 변화
기존
이런 키를 썼지만 더 길게 바꿔주었다.
결과
회고
쉬운 예외처리에 속해서 쉽게 해결하였다. 라이브러리 자체에서 이렇게 편의성을 신경써주고 있다는 것에 놀랐고, 이런 부분을 통해 추후 오픈소스에 기여하게 된다면 이런 점들을 고려해야겠다는 생각을 하게 되었다.