1
1
package com .sharetreats .chatbot .infra .config ;
2
2
3
3
import lombok .Getter ;
4
+ import lombok .RequiredArgsConstructor ;
5
+ import lombok .extern .slf4j .Slf4j ;
6
+ import org .springframework .data .redis .core .RedisTemplate ;
4
7
import org .springframework .stereotype .Component ;
5
8
9
+ import java .time .Duration ;
6
10
import java .util .UUID ;
7
11
import java .util .concurrent .ConcurrentHashMap ;
8
12
13
+ @ Slf4j
9
14
@ Component
15
+ @ RequiredArgsConstructor
10
16
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 ;
13
19
public void generateToken (String userId ) {
20
+
14
21
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 );
19
24
25
+ }
20
26
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 ) {
26
37
return false ;
27
38
}
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 ;
37
40
}
38
41
}
0 commit comments