1
1
package com .tnt .application .auth ;
2
2
3
+ import static com .tnt .global .error .model .ErrorMessage .*;
3
4
import static io .micrometer .common .util .StringUtils .*;
4
- import static java .util .Objects .*;
5
5
6
- import java .time .LocalDateTime ;
7
6
import java .util .concurrent .TimeUnit ;
8
7
9
- import org .springframework .data .redis .core .RedisTemplate ;
8
+ import org .springframework .data .redis .core .StringRedisTemplate ;
10
9
import org .springframework .stereotype .Service ;
11
10
12
- import com .tnt .domain .auth .SessionValue ;
13
11
import com .tnt .global .error .exception .UnauthorizedException ;
14
12
15
13
import jakarta .servlet .http .HttpServletRequest ;
@@ -24,40 +22,49 @@ public class SessionService {
24
22
static final long SESSION_DURATION = 2L * 24 * 60 * 60 ; // 48시간
25
23
private static final String AUTHORIZATION_HEADER = "Authorization" ;
26
24
private static final String SESSION_ID_PREFIX = "SESSION-ID " ;
27
- private final RedisTemplate <String , SessionValue > redisTemplate ;
25
+
26
+ private final StringRedisTemplate redisTemplate ;
28
27
29
28
public String authenticate (HttpServletRequest request ) {
30
29
String authHeader = request .getHeader (AUTHORIZATION_HEADER );
31
30
32
31
if (isBlank (authHeader ) || !authHeader .startsWith (SESSION_ID_PREFIX )) {
33
- log .error ("Authorization 헤더가 존재하지 않거나 올바르지 않은 형식입니다." );
32
+ log .error (AUTHORIZATION_HEADER_ERROR . getMessage () );
34
33
35
- throw new UnauthorizedException ("인가 세션이 존재하지 않습니다." );
34
+ throw new UnauthorizedException (AUTHORIZATION_HEADER_ERROR );
36
35
}
37
36
38
37
String sessionId = authHeader .substring (SESSION_ID_PREFIX .length ());
38
+ String sessionValue = redisTemplate .opsForValue ().get (sessionId );
39
+
40
+ if (sessionValue == null ) {
41
+ log .error (NO_EXIST_SESSION_IN_STORAGE .getMessage ());
39
42
40
- requireNonNull (redisTemplate .opsForValue ().get (sessionId ), "세션 스토리지에 세션이 존재하지 않습니다." );
43
+ throw new UnauthorizedException (NO_EXIST_SESSION_IN_STORAGE );
44
+ }
41
45
42
- return sessionId ;
46
+ createOrUpdateSession (sessionId , "" );
47
+
48
+ return sessionValue ;
43
49
}
44
50
45
- public void createSession (String memberId , HttpServletRequest request ) {
46
- SessionValue sessionValue = SessionValue .builder ()
47
- .lastAccessTime (LocalDateTime .now ())
48
- .userAgent (request .getHeader ("User-Agent" ))
49
- .clientIp (request .getRemoteAddr ())
50
- .build ();
51
-
52
- redisTemplate .opsForValue ().set (
53
- memberId ,
54
- sessionValue ,
55
- SESSION_DURATION ,
56
- TimeUnit .SECONDS
57
- );
51
+ public void createOrUpdateSession (String sessionId , String memberId ) {
52
+ if (isBlank (memberId )) { // 세션 갱신
53
+ redisTemplate .expire (sessionId , SESSION_DURATION , TimeUnit .SECONDS );
54
+ redisTemplate .expire (memberId , SESSION_DURATION , TimeUnit .SECONDS );
55
+ } else { // 로그인 시 기존 로그인 상태 제거하고 새로운 세션 생성
56
+ String existingSessionId = redisTemplate .opsForValue ().get (memberId );
57
+
58
+ if (existingSessionId != null ) {
59
+ removeSession (sessionId );
60
+ removeSession (memberId );
61
+ }
62
+ redisTemplate .opsForValue ().set (sessionId , memberId , SESSION_DURATION , TimeUnit .SECONDS );
63
+ redisTemplate .opsForValue ().set (memberId , sessionId , SESSION_DURATION , TimeUnit .SECONDS );
64
+ }
58
65
}
59
66
60
- public void removeSession (String sessionId ) {
61
- redisTemplate .delete (sessionId );
67
+ public void removeSession (String dataKey ) {
68
+ redisTemplate .delete (dataKey );
62
69
}
63
70
}
0 commit comments