Skip to content

Commit 2d0b0c0

Browse files
authored
✨ feat: 커스텀 HandshakeInterceptor 추가 (#98)
* ♻️ refactor: 웹소켓 핸드쉐이크 인터셉터 변경 * ✨ feat: 웹소켓 로그에 사용자이름 추가 * chore: Java 스타일 수정 --------- Co-authored-by: github-actions <>
1 parent 7bc697b commit 2d0b0c0

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.f1.backend.global.config;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.server.ServerHttpRequest;
7+
import org.springframework.http.server.ServerHttpResponse;
8+
import org.springframework.security.core.Authentication;
9+
import org.springframework.security.core.context.SecurityContextHolder;
10+
import org.springframework.stereotype.Component;
11+
import org.springframework.web.socket.WebSocketHandler;
12+
import org.springframework.web.socket.server.HandshakeInterceptor;
13+
14+
import java.util.Map;
15+
16+
@Slf4j
17+
@Component
18+
public class CustomHandshakeInterceptor implements HandshakeInterceptor {
19+
20+
@Override
21+
public boolean beforeHandshake(
22+
ServerHttpRequest request,
23+
ServerHttpResponse response,
24+
WebSocketHandler wsHandler,
25+
Map<String, Object> attributes)
26+
throws Exception {
27+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
28+
if (authentication == null || !authentication.isAuthenticated()) {
29+
response.setStatusCode(HttpStatus.UNAUTHORIZED); // 서버 로그에만 적용되는 StatusCode
30+
return false;
31+
}
32+
33+
attributes.put("auth", authentication);
34+
return true;
35+
}
36+
37+
@Override
38+
public void afterHandshake(
39+
ServerHttpRequest request,
40+
ServerHttpResponse response,
41+
WebSocketHandler wsHandler,
42+
Exception exception) {
43+
// TODO : 연결 이후, 사용자 웹소켓 세션 로그 및 IP 등 추적 및 메트릭 수집 로직 추가
44+
45+
}
46+
}

backend/src/main/java/io/f1/backend/global/config/StompChannelInterceptor.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,21 @@ public Message<?> preSend(Message<?> message, MessageChannel channel) {
2727
throw new IllegalArgumentException("Stomp command required");
2828
}
2929

30+
String username = "알수없는 사용자";
31+
if (accessor.getUser() != null) {
32+
username = accessor.getUser().getName();
33+
}
34+
3035
if (command.equals(StompCommand.CONNECT)) {
31-
log.info("CONNECT : 세션 연결 - sessionId = {}", sessionId);
36+
log.info("user : {} | CONNECT : 세션 연결 - sessionId = {}", username, sessionId);
3237
} else if (command.equals(StompCommand.SUBSCRIBE)) {
3338
if (destination != null && sessionId != null) {
34-
log.info("SUBSCRIBE : 구독 시작 destination = {}", destination);
39+
log.info("user : {} | SUBSCRIBE : 구독 시작 destination = {}", username, destination);
3540
}
3641
} else if (command.equals(StompCommand.SEND)) {
37-
log.info("SEND : 요청 destination = {}", destination);
42+
log.info("user : {} | SEND : 요청 destination = {}", username, destination);
3843
} else if (command.equals(StompCommand.DISCONNECT)) {
39-
log.info("DISCONNECT : 연결 해제 sessionId = {}", sessionId);
44+
log.info("user : {} | DISCONNECT : 연결 해제 sessionId = {}", username, sessionId);
4045
}
4146

4247
return message;

backend/src/main/java/io/f1/backend/global/config/WebSocketConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
99
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
1010
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
11-
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
1211

1312
@Configuration
1413
@RequiredArgsConstructor
1514
@EnableWebSocketMessageBroker
1615
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
1716

1817
private final StompChannelInterceptor stompChannelInterceptor;
18+
private final CustomHandshakeInterceptor customHandshakeInterceptor;
1919

2020
@Override
2121
public void registerStompEndpoints(StompEndpointRegistry registry) {
2222
registry.addEndpoint("/ws/game-room")
23-
.addInterceptors(new HttpSessionHandshakeInterceptor())
23+
.addInterceptors(customHandshakeInterceptor)
2424
.setAllowedOriginPatterns("*");
2525
}
2626

0 commit comments

Comments
 (0)