개발 관련 일지/Springs

[TrobleShooting] JWT signature does not match locally computed signature.

worldi 2023. 12. 8. 23:48

msa를 구축하다가, token 유효 검증 부분에서 signature가 맞지 않는 다는 에러가 떴다.

[apigateway-service] [ctor-http-nio-2] c.e.a.config.Authorization Header Filter :
 JWT signature does not match locally computed signature. 
JWT validity cannot be asserted and should not be trusted.

해결법

다음과 같이 secretKey 를 secretKey.getBytes()로 바꾸어주니 정상 작동하였다.

public String generateToken(final String userId) {
        return Jwts.builder()
            .setExpiration(new Date(System.currentTimeMillis() + expireTime))
            .signWith(SignatureAlgorithm.HS256, secretKey.getBytes())
            .setSubject(userId)
            .compact();
    }
final Claims body = Jwts.parser()
                .setSigningKey(secretKey.getBytes())
                .parseClaimsJws(token)
                .getBody();

 

참고 자료

https://stackoverflow.com/questions/56639392/jwt-signature-does-not-match-locally-computed-signature-jwt-validity-cannot-be