Open
Conversation
joonfluence
reviewed
Nov 12, 2025
| import org.springframework.security.messaging.context.SecurityContextChannelInterceptor; | ||
|
|
||
| @Configuration | ||
| @EnableWebSocketMessageBroker |
joonfluence
reviewed
Nov 12, 2025
Comment on lines
+24
to
+28
| @Override | ||
| public void configureMessageBroker(MessageBrokerRegistry registry) { | ||
| registry.enableSimpleBroker("/sub"); | ||
| registry.setApplicationDestinationPrefixes("/pub"); | ||
| } |
Collaborator
There was a problem hiding this comment.
메모리 기반 SimpleBroker를 사용
joonfluence
reviewed
Nov 12, 2025
Comment on lines
+19
to
+22
| @Getter | ||
| private final ConcurrentMap<UUID, List<SseEmitter>> data = new ConcurrentHashMap<>(); | ||
|
|
||
| private final ConcurrentLinkedDeque<UUID> eventIdQueue = new ConcurrentLinkedDeque<>(); |
joonfluence
reviewed
Nov 12, 2025
| import org.springframework.transaction.event.TransactionPhase; | ||
| import org.springframework.transaction.event.TransactionalEventListener; | ||
|
|
||
| @Component |
joonfluence
reviewed
Nov 12, 2025
joonfluence
reviewed
Nov 12, 2025
Comment on lines
+106
to
+132
| @Scheduled(fixedDelay = 1000 * 60 * 30) // 30분마다 실행 | ||
| public void cleanUp() { | ||
| log.debug("SSE 연결 상태 점검 시작"); | ||
|
|
||
| for (Map.Entry<UUID, List<SseEmitter>> entry : sseRepository.getData().entrySet()) { | ||
| UUID receiverId = entry.getKey(); | ||
| List<SseEmitter> emitters = entry.getValue(); | ||
|
|
||
| if (emitters == null || emitters.isEmpty()) { | ||
| continue; | ||
| } | ||
|
|
||
| Iterator<SseEmitter> it = emitters.iterator(); | ||
| while (it.hasNext()) { | ||
| SseEmitter emitter = it.next(); | ||
| if (!ping(emitter)) { | ||
| it.remove(); // 리스트에서 빼기 | ||
| log.debug("끊어진 SSE 연결 제거: userId={}", receiverId); | ||
| } | ||
| } | ||
|
|
||
| if (emitters.isEmpty()) { | ||
| sseRepository.getData().remove(receiverId); | ||
| } | ||
| } | ||
|
|
||
| log.debug("✅ SSE 연결 상태 점검 완료"); |
joonfluence
reviewed
Nov 12, 2025
Comment on lines
+18
to
+28
| @RequiredArgsConstructor | ||
| public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { | ||
|
|
||
| private final JwtAuthenticationChannelInterceptor jwtAuthenticationChannelInterceptor; | ||
|
|
||
|
|
||
| @Override | ||
| public void configureMessageBroker(MessageBrokerRegistry registry) { | ||
| registry.enableSimpleBroker("/sub"); | ||
| registry.setApplicationDestinationPrefixes("/pub"); | ||
| } |
Collaborator
There was a problem hiding this comment.
WebSocketMessageBrokerConfigurer 통해 HTTP 요청 간 JWT 인증 수행
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
기본 요구사항
웹소켓 구현하기
웹소켓 환경 구성
spring-boot-starter-websocket 의존성을 추가하세요.
implementation 'org.springframework.boot:spring-boot-starter-websocket'웹소켓 메시지 브로커 설정
메모리 기반 SimpleBroker를 사용하세요.
SimpleBroker의 Destination Prefix는
/sub으로 설정하세요.→ 클라이언트에서 메시지를 구독할 때 사용합니다.
Application Destination Prefix는
/pub으로 설정하세요.→ 클라이언트에서 메시지를 발행할 때 사용합니다.
STOMP 엔드포인트는
/ws로 설정하고, SockJS 연결을 지원해야 합니다.메시지 송신
첨부파일이 없는 단순 텍스트 메시지인 경우 STOMP를 통해 메시지를 전송할 수 있도록 컨트롤러를 구현하세요.
클라이언트는 웹소켓으로
/pub/messages엔드포인트에 메시지를 전송할 수 있어야 합니다.@MessageMapping을 활용하세요.메시지 전송 요청의 페이로드 타입은 MessageCreateRequest 를 그대로 활용합니다.
첨부파일이 포함된 메시지는 기존의 API (
POST /api/messages)를 그대로 활용합니다.메시지 수신
클라이언트는 채널 입장 시 웹소켓으로
/sub/channels.{channelId}.messages를 구독해 메시지를 수신합니다.이를 고려해 메시지가 생성되면 해당 엔드포인트로 메시지를 보내는 컴포넌트를 구현하세요.
MessageCreatedEvent를 통해 새로운 메시지 생성 이벤트를 확인하세요.
SimpMessagingTemplate를 통해 적절한 엔드포인트로 메시지를 전송하세요.
SSE 구현하기
SSE 환경을 구성하세요.
클라이언트에서 SSE 연결을 위한 엔드포인트를 구현하세요.
사용자별 SseEmitter 객체를 생성하고 메시지를 전송하는 컴포넌트를 구현하세요.
connect: SseEmitter 객체를 생성합니다.
send, broadcast: SseEmitter 객체를 통해 이벤트를 전송합니다.
cleanUp: 주기적으로 ping을 보내서 만료된 SseEmitter 객체를 삭제합니다.
ping: 최초 연결 또는 만료 여부를 확인하기 위한 용도로 더미 이벤트를 보냅니다.
SseEmitter 객체를 메모리에서 저장하는 컴포넌트를 구현하세요.
ConcurrentMap: 스레드 세이프한 자료구조를 사용합니다.
List: 사용자 당 N개의 연결을 허용할 수 있도록 합니다. (예: 다중 탭)
이벤트 유실 복원을 위해 SSE 메시지를 저장하는 컴포넌트를 구현하세요.
각 메시지 별로 고유한 ID를 부여합니다.
클라이언트에서 LastEventId를 전송해 이벤트 유실 복원이 가능하도록 해야 합니다.
기존에 클라이언트에서 폴링 방식으로 주기적으로 요청하던 데이터를 SSE를 이용해 서버에서 실시간으로 전달하는 방식으로 리팩토링하세요.
새로운 알림 이벤트 전송
이벤트 명세
파일 업로드 상태 변경 이벤트 전송
이벤트 명세
채널 갱신 이벤트 전송
이벤트 명세
사용자 갱신 이벤트 전송
이벤트 명세
배포 아키텍처 구성하기
다음의 다이어그램에 부합하는 배포 아키텍처를 Docker Compose를 통해 구현하세요.
Reverse Proxy
/api/*,/ws/*요청은 Backend 컨테이너로 프록시 처리합니다./usr/share/nginx/html등)에 복사하세요.3000번 포트를 통해 접근할 수 있어야 합니다.Backend
/api/*,/ws/*요청이 이 서버로 전달됩니다.DB, Memory DB, Message Broker
심화 요구사항
✅ 웹소켓 인증/인가 처리하기
서버 측에서는 ChannelInterceptor를 구현하여 연결 시 토큰을 검증하고, 인증된 사용자 정보를 SecurityContext에 설정해야 합니다.
AuthorizationChannelInterceptor를 사용해 메시지 권한 검사를 수행합니다.
AuthorizationChannelInterceptor를 활용하기 위해 의존성을 추가하세요.
implementation 'org.springframework.security:spring-security-messaging'
MessageMatcherDelegatingAuthorizationManager를 활용해 인가 정책을 정의하고, 채널에 추가하세요.
분산 환경 배포 아키텍처 구성하기
다음의 다이어그램에 부합하는 배포 아키텍처를 Docker Compose를 통해 구현하세요.
Backend-*
Reverse Proxy
upstream 블록을 수정해 다음의 로드밸런싱 전략을 적용해 Backend로 트래픽을 분산시켜보세요.
$upstream_addr 변수를 활용해 실제 요청을 처리하는 서버의 IP를 헤더에 추가하고 브라우저 개발자 도구를 활용해 비교해보세요.