SOLUX-완숙이

jwt signWith deprecated 오류

leeeehhjj 2022. 1. 13. 14:48

 

String key;
   

    public JwtTokenProvider(@Value("${jwt.secret}") String secretKey) {
        key = Base64.getEncoder().encodeToString(secretKey.getBytes());
       
    }
    
    public TokenInfoResponseDto generateToken(Authentication authentication) {
        ...
        //AccessToken 생성
        Date accessTokenExpiresIn = new Date(now + ACCESS_TOKEN_EXPIRE_TIME);
        String accessToken = Jwts.builder()
                .setSubject(authentication.getName())
                .claim(AUTHORITIES_KEY, authorities)
                .setExpiration(accessTokenExpiresIn)
                .signWith(SignatureAlgorithm.HS256, key)
                .compact();
    }

이렇게 했더니 signWith이 deprecated 됐다고 했다.

Use Keys.hmacShaKeyFor(bytes) to obtain the Key and then invoke signWith(Key) or signWith(Key, SignatureAlgorithm). 이라는 설명이 떠서

private final Key key;

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

    public TokenInfoResponseDto generateToken(Authentication authentication) {
        ...
        //AccessToken 생성
        Date accessTokenExpiresIn = new Date(now + ACCESS_TOKEN_EXPIRE_TIME);
        String accessToken = Jwts.builder()
                .setSubject(authentication.getName())
                .claim(AUTHORITIES_KEY, authorities)
                .setExpiration(accessTokenExpiresIn)
                .signWith(key, SignatureAlgorithm.HS256)
                .compact();
    }

이렇게 변경했다.