문제상황

⇒ WebSocket에서 header에 token을 전송하는 경우, 해당 token을 security에서 인증-인가 처리를 못하여, @AuthenticationPrincipal 에서 UserDetails의 User 정보를 가져오지 못하는 상황 발생

원인

⇒ 웹 소켓은 Header의 토큰을 검사하는 프로토콜이 STOMP(Simple Text Oriented Messaging Protocol)이므로, HTTP와 다르다.

해결

⇒ ChannelInterceptor를 통해, 메시지의 Header에서 토큰을 추출하여,

SecurityContextHolder에 authentication 을 저장합니다.

구체적인 설명

참고자료

WebSocket Security :: Spring Security

[팀프로젝트] STOMP 활용한 프로젝트 채팅방 구현

Spring Boot - WebSocket & JWT & Spring Security 토큰 인증

STOMP를 사용하여 webSocket 채팅 구현에서 principal이 null 값 뜨는 경우

웹소켓 취약점 해결 : 인증 도입 (STOMP, Jwt, Spring Security)

WebSocket에 Spring Security, JWT 적용해보기

Stomp WebSocket + JWT + Spring Security 채팅 구현