표현 영역
응용 영역
도메인 영역
인프라스터럭처 영역
테스트 어려움
과 기능 확장의 어려움
문제가 발생함테스트 어려움
과 기능 확장의 어려움
문제가 발생함@Component
public interface TokenRepository {
void save(String userId, String refreshToken);
Optional<String> getExistRefreshToken(String userId);
Boolean existAccessToken(String userId);
}
@Component
@RequiredArgsConstructor
public class JWTGenerator {
private final TokenProperty tokenProperty;
private final TokenRepository tokenRepository;
public TokenParam generate(UserParam userParam, Date from) {
TokenParam tokenParam = TokenParam.builder()
.accessToken(createAccessToken(userParam, from))
.refreshToken(createRefreshToken(userParam, from))
.build();
tokenRepository.save(userParam.userId().toString(), tokenParam.refreshToken());
return tokenParam;
}
@Component
@RequiredArgsConstructor
public class LettuceRedisRepository implements TokenRepository {
private final StringRedisTemplate stringRedisTemplate;
@Override
public void save(String userId, String refreshToken) {
stringRedisTemplate.opsForValue().set("RT:" + userId, refreshToken, 14, TimeUnit.DAYS);
}
@Override
public Optional<String> getExistRefreshToken(String userId) {
return Optional.ofNullable(stringRedisTemplate.opsForValue().get("RT:" + userId));
}
@Override
public Boolean existAccessToken(String userId) {
return stringRedisTemplate.hasKey("AT:" + userId);
}
}
bootJar.enabled = false
jar.enabled = true
dependencies {
implementation project(":app:api:common-api")
//redis
implementation 'org.springframework.data:spring-data-redis'
implementation 'io.lettuce:lettuce-core:6.3.0.RELEASE'
}
@DisplayName("JWT 생성 테스트")
class JWTGeneratorTest {
long hour = 3600000L;
long twoWeeks = 1209600000L;
TokenProperty tokenProperty = new TokenProperty(
"wehfiuhewiuhfhweiuhfiuwehifueisdfsdfsdfdsfsduwhfiuw",
hour,
twoWeeks
);
TokenRepository tokenRepository = mock(TokenRepository.class);
JWTGenerator tokenGenerator = new JWTGenerator(tokenProperty, tokenRepository);
...
@Test
@DisplayName("1시간 전에 생성된 AccessToken은 유효하지 않다.")
void accessTokenInvalidBeforeHourAgo() {
...
}
}