Skip to content

Commit 56aba88

Browse files
committed
update: 토큰 저장소 Redis로 변경 (sharetreats-team#43)
1 parent 012e42e commit 56aba88

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

src/main/java/com/sharetreats/chatbot/infra/config/RedisConfig.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import org.springframework.beans.factory.annotation.Value;
44
import org.springframework.context.annotation.Bean;
55
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Primary;
67
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
78
import org.springframework.data.redis.core.RedisTemplate;
89
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
10+
import org.springframework.data.redis.serializer.StringRedisSerializer;
911

1012
@Configuration
1113
public class RedisConfig {
@@ -16,15 +18,36 @@ public class RedisConfig {
1618
@Value("${spring.redis.gift-history.port}")
1719
private int redisPort;
1820

21+
@Value("${spring.redis.token.host}")
22+
private String tokenRedisHost;
23+
24+
@Value("${spring.redis.token.port}")
25+
private int tokenRedisPort;
26+
1927
@Bean
28+
@Primary
2029
public LettuceConnectionFactory connectionFactory() {
2130
return new LettuceConnectionFactory(redisHost, redisPort);
2231
}
32+
33+
@Bean
34+
public LettuceConnectionFactory tokenConnectionFactory() {
35+
return new LettuceConnectionFactory(tokenRedisHost, tokenRedisPort);
36+
}
2337
@Bean
2438
public RedisTemplate<String, Object> redisTemplate() {
2539
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
2640
redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
2741
redisTemplate.setConnectionFactory(connectionFactory());
2842
return redisTemplate;
2943
}
44+
45+
@Bean
46+
@Primary
47+
public RedisTemplate<String, String> tokenRedisTemplate() {
48+
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
49+
redisTemplate.setDefaultSerializer(new StringRedisSerializer());
50+
redisTemplate.setConnectionFactory(tokenConnectionFactory());
51+
return redisTemplate;
52+
}
3053
}
Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
11
package com.sharetreats.chatbot.infra.config;
22

33
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.data.redis.core.RedisTemplate;
47
import org.springframework.stereotype.Component;
58

9+
import java.time.Duration;
610
import java.util.UUID;
711
import java.util.concurrent.ConcurrentHashMap;
812

13+
@Slf4j
914
@Component
15+
@RequiredArgsConstructor
1016
public class TokenConfig {
11-
private static final long TOKEN_EXPIRE_TIME = 30 * 60 * 1000;
12-
private static final ConcurrentHashMap<String, TokenInfo> tokenStore = new ConcurrentHashMap<>();
17+
18+
private final RedisTemplate<String, String> redisTemplate;
1319
public void generateToken(String userId) {
20+
1421
String token = UUID.randomUUID().toString();
15-
long expireTime = System.currentTimeMillis() + TOKEN_EXPIRE_TIME;
16-
TokenInfo tokenInfo = new TokenInfo(token, expireTime);
17-
tokenStore.put(userId, tokenInfo);
18-
}
22+
Duration expireDuration = Duration.ofMinutes(20);
23+
redisTemplate.opsForValue().set(userId, token, expireDuration);
1924

25+
}
2026
public boolean validateToken(String userId) {
21-
TokenInfo tokenInfo = tokenStore.get(userId);
22-
if (tokenInfo != null && tokenInfo.getExpireTime() >= System.currentTimeMillis()) {
23-
return true;
24-
} else {
25-
tokenStore.remove(userId);
27+
28+
try {
29+
String token = redisTemplate.opsForValue().get(userId);
30+
if (token != null) {
31+
Long expireTime = redisTemplate.getExpire(userId);
32+
if (expireTime != null && expireTime > 0) {
33+
return true;
34+
}
35+
}
36+
} catch (IllegalArgumentException e) {
2637
return false;
2738
}
28-
}
29-
@Getter
30-
private class TokenInfo {
31-
private String token;
32-
private long expireTime;
33-
public TokenInfo(String token, long expireTime) {
34-
this.token = token;
35-
this.expireTime = expireTime;
36-
}
39+
return false;
3740
}
3841
}

0 commit comments

Comments
 (0)