-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserStatusInterceptor.java
More file actions
58 lines (48 loc) · 2.37 KB
/
UserStatusInterceptor.java
File metadata and controls
58 lines (48 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package socket_server.common.auth;
import gotcha_common.exception.exceptionCode.ExceptionCode;
import gotcha_common.exception.exceptionCode.GlobalExceptionCode;
import gotcha_domain.auth.SecurityUserDetails;
import gotcha_domain.user.User;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import socket_server.common.exception.socket.SocketUserStatusExceptionCode;
@Component
@RequiredArgsConstructor
public class UserStatusInterceptor implements ChannelInterceptor {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
try {
if (accessor != null && StompCommand.CONNECT.equals(accessor.getCommand())) {
Authentication auth = (Authentication) accessor.getUser();
SecurityUserDetails details = (SecurityUserDetails) auth.getPrincipal();
User user = details.getUser();
SocketUserStatusExceptionCode code =
SocketUserStatusExceptionCode.fromStatus(user.getUserStatus());
if (code != null) {
throw new MessagingException(toErrorPayload(code));
}
}
return message;
} catch (Throwable e) {
if (e instanceof MessagingException) {
throw (MessagingException) e;
}
System.err.println("=== [DEBUG][UserStatusInterceptor] 예외 발생 ===");
e.printStackTrace();
throw new MessagingException(toErrorPayload(GlobalExceptionCode.INTERNAL_SERVER_ERROR));
}
}
private String toErrorPayload(ExceptionCode code) {
return String.format("{\"errorCode\":\"%s\", \"status\":%d, \"message\":\"%s\"}", code.getCode(),
code.getStatus().value(), code.getMessage());
}
}