오류를 개발새발

JWT signature does not match locally computed signature. JWT validity cannot be asserted and should not be trusted.

휴일이 2023. 3. 24. 21:44

 

jwt 토큰의 파싱 오류라고 한다

한 마디로 인코딩할때랑 디코딩 할 때랑

시크릿 키가 달라서 생기는 일이라고 하는데

인터넷에 돌아다니는

시크릿키.getBytes() 를 붙이는 거로는 해결이 안 됐다..(이미 넣어져 있기도 했고)

 

 

찾다가 보니까

 

DefaultJwtParser 에서 발생 된 오류였는데

생각해보니 저번 오류에서

 

 

해당 클래스가 Deprecated 돼있다고...했지..

 

 

그러면 JwtParser 를 사용하는 코드를 그냥 전부 수정해보기로 했다 ㅠ_ㅠ

 

 

 

근데 얘도 Deprecated 되어있네...?

 

 

그리고 이렇게 고쳐도 같은 오류 발생

그러면 파싱 방법에 문제가 있는 건 아닌가...?

 

     private final JwtParser jwtParser;
    private final JwtTokenRepository jwtTokenRepository;
    private static final String BEARER = "Bearer ";


    public JwtTokenParser(@Value("${token.secret.key}") String secretKey, JwtTokenRepository jwtTokenRepository) {
        this.jwtTokenRepository = jwtTokenRepository;
        Key key = Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8));
        this.jwtParser = Jwts.parserBuilder().setSigningKey(key).build();
    }
    
 public Claims getClaims(String token) {
        Claims claims = jwtParser.parseClaimsJwt(token).getBody();
        return claims;
    }

 

 

나는 이런 코드를 사용중이고

JwtParser로 파싱을 하고, getClaims() 로 클레임을 가져오기 때문에

뭔가 이상한 점이 있는지...?

 

 

 

아!!

혹시 몰라 Provider 를 들어가보니

내가 토큰을 만드는 키를 만드는 방식이 파싱하는 방식과 달랐다...-.-a

 

Provider 의 키 만드는 방법도 동일하게 수정하고,

몇 가지 코드를 약간 수정했다

 

private final JwtParser jwtParser;
private final Key key;

public JwtTokenProvider(@Value("${token.secret.key}") String secretKey, JwtTokenRepository jwtTokenRepository) {
    this.jwtTokenRepository = jwtTokenRepository;
    key = Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8));
    this.jwtParser = Jwts.parserBuilder().setSigningKey(key).build();
}

 

 

 

이걸 고치니까 다른 오류가 발생하는데...

그건 다음 포스팅에 :)

728x90