개인 프로젝트를 시작하면서 Spring Security와 JWT를 이용해 회원가입 및 로그인 기능을 구현하고 있다. JWT 생성 로직을 구현하고 테스트하는 과정에서 발견한 에러를 공유해본다.
해당 예러는 로그인 테스트 진행 중 발생하였다.
io.jsonwebtoken.security.WeakKeyException: The specified key byte array is 120 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.
원인
에러 메세지를 잘 읽어보면 원인이 잘 나와있다. JWT를 암호화할 때 안전하게 사용하기 위해서는 Secret Key가 256bit 이상어야 하는데 내가 테스트에 넣어놓은 Secret Key는 120bit밖에 되지 않아 안전하지 않다는 것이다.
해결
실제로 SpringBoot의 세팅인 application.yml에 key를 선언하고 사용하였는데 아래와 같이 너무 짧게 선언되어 있어 256bit 이상으로 변경하였다.
jwt:
secret: testSecretKey
expiration: 3600000 # 1 hour
jwt:
secret: testSecretKey20230327testSecretKey20230327testSecretKey20230327
expiration: 3600000 # 1 hour
마무리
개인 프로젝트라 key값을 간단하게 선언했는데 위와 같은 문제가 발생하였다. JWT세팅에 대해 한개 더 알게되어 도움이 많이 될 것 같다.
'Trouble Shooting' 카테고리의 다른 글
[트러블슈팅] Data too long for column '칼럼명' at row 1(with 암호화) (1) | 2024.12.29 |
---|---|
Error creating bean with name 'jpaAuditingHandler': (0) | 2024.12.13 |
[Git] .gitignore가 적용되지 않았을때 (0) | 2024.12.11 |
[Java] Scanner 입력 예외처리 시 무한로프 - 입력 버퍼를 비워라 (0) | 2024.01.04 |
[WSL/Ubuntu] 도커 세팅위한 Ubuntu 배포버전을 설치하다 발생한 이슈(0x80370102) + LG 노트북 (0) | 2023.11.16 |