-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…limit [feat] : 리프레쉬 토큰 제한 개수를 지정한다
- Loading branch information
Showing
3 changed files
with
41 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 22 additions & 15 deletions
37
src/main/java/side/onetime/repository/RefreshTokenRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,49 @@ | ||
package side.onetime.repository; | ||
|
||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
|
||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
import org.springframework.stereotype.Repository; | ||
import side.onetime.domain.RefreshToken; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class RefreshTokenRepository { | ||
@Value("${jwt.refresh-token.expiration-time}") | ||
private long REFRESH_TOKEN_EXPIRATION_TIME; // 리프레쉬 토큰 유효기간 | ||
|
||
private final RedisTemplate redisTemplate; | ||
private static final int REFRESH_TOKEN_LIMIT = 5; // 최대 5개로 제한 | ||
|
||
private final RedisTemplate<String, String> redisTemplate; | ||
|
||
// RefreshToken 리스트에 새로운 토큰을 추가 | ||
public void save(final RefreshToken refreshToken) { | ||
ValueOperations<Long, String> valueOperations = redisTemplate.opsForValue(); | ||
valueOperations.set(refreshToken.getUserId(), refreshToken.getRefreshToken()); | ||
redisTemplate.expire(refreshToken.getRefreshToken(), REFRESH_TOKEN_EXPIRATION_TIME, TimeUnit.MILLISECONDS); | ||
String key = "refreshToken:" + refreshToken.getUserId(); | ||
// 맨 앞에 추가 | ||
redisTemplate.opsForList().leftPush(key, refreshToken.getRefreshToken()); | ||
|
||
// 가장 오래된 리프레쉬 토큰을 삭제 | ||
redisTemplate.opsForList().trim(key, 0, REFRESH_TOKEN_LIMIT - 1); | ||
|
||
// 만료 시간 설정 (전체 리스트의 키에 적용) | ||
redisTemplate.expire(key, REFRESH_TOKEN_EXPIRATION_TIME, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
public Optional<RefreshToken> findByUserId(final Long userId) { | ||
ValueOperations<UUID, String> valueOperations = redisTemplate.opsForValue(); | ||
String refreshToken = valueOperations.get(userId); | ||
// 유저 ID로 RefreshToken 리스트 조회 | ||
public Optional<List<String>> findByUserId(final Long userId) { | ||
String key = "refreshToken:" + userId; | ||
|
||
List<String> refreshTokens = redisTemplate.opsForList().range(key, 0, -1); | ||
|
||
if (Objects.isNull(refreshToken)) { | ||
if (Objects.isNull(refreshTokens) || refreshTokens.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of(new RefreshToken(userId, refreshToken)); | ||
return Optional.of(refreshTokens); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters