Redis 내에 있는 refreshToken과 비교하는 방식으로 바꾸고자 한다.UserService
//로그인
public TokenInfo login(Login login){
//사용자 인증
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(login.getId(), login.getPassword());
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
//토큰 생성
TokenInfo tokenInfo = jwtTokenProvider.createAllToken(authentication);
//리프레시 토큰 redis에 저장(expirationTime 설정을 통해 자동 삭제 처리)
redisTemplate.opsForValue().set("RT:"+authentication.getName(),
tokenInfo.getRefreshToken(), tokenInfo.getRefreshTokenExpirationTime(), TimeUnit.MICROSECONDS);
return tokenInfo;
}
UserService
//토큰 재발급을 위한 reissue()
public TokenInfo reissue(Reissue reissue){
// refresh token 검증
if(!jwtTokenProvider.validateToken(reissue.getRefreshToken())){
throw new IllegalArgumentException("Refresh Token 정보가 유효하지 않습니다.");
}
// Access Token 에서 User email 를 가져옴
Authentication authentication = jwtTokenProvider.getAuthentication(reissue.getAccessToken());
// Redis 에서 User email 을 기반으로 저장된 Refresh Token 값을 가져옴
String refreshToken = (String) redisTemplate.opsForValue().get("RT:"+authentication.getName());
if(!refreshToken.equals(reissue.getRefreshToken())){
throw new IllegalStateException("Refresh Token 정보가 일치하지 않습니다");
}
// 새로운 토큰 생성
TokenInfo tokenInfo = jwtTokenProvider.createAllToken(authentication);
// RefreshToken Redis 업데이트
redisTemplate.opsForValue().set("RT:" + authentication.getName(), tokenInfo.getRefreshToken(),
tokenInfo.getRefreshTokenExpirationTime(), TimeUnit.MILLISECONDS);
return tokenInfo;
}