본문 바로가기
web/error

ECDSA signing keys must be PrivateKey instances

by su0a 2024. 3. 14.

1. 발생

OAuth, jwt 토큰을 이용한 간편 로그인, 로그아웃 구현 후 api로부터 받은 인가코드를 처리할 때 발생하였다.

 

2. 코드

ECDSA signing keys must be PrivateKey instances

 

3. 원인

public class JwtTokenProvider {
    private final Key key;

    public JwtTokenProvider(@Value("${spring.jwt.secret-key}") String secretKey) {
        byte[] keyBytes = Decoders.BASE64.decode(secretKey);
        this.key= Keys.hmacShaKeyFor(keyBytes);
    }

    public String generate(String subject, Date expiredAt){
        return Jwts.builder()
                .setSubject(subject)
                .setExpiration(expiredAt)
                .signWith(key, SignatureAlgorithm.ES256)
                .compact();
    }

 

ECDSA 알고리즘을 사용할 때 private key를 사용해야 함을 알려주는 코드이다.

yml파일에 secret-key를 등록했음에도 안되는 이유를 찾아보았더니

서명에 사용하는 알고리즘에 HMAC hash알고리즘이 아닌 ECDSA 알고리즘을 오류가 난거였다..!

 

4. 해결

SignatureAlgorithm.ES256 -> SignatureAlgorithm.HS256

 

ES256 알고리즘을 HS256 알고리즘으로 변경함으로써 해결!

'web > error' 카테고리의 다른 글

No matching tests found in any candidate test task  (0) 2024.03.14