Skip to content

Commit 7b555fa

Browse files
committed
fix(be)
1 parent 4c64dc8 commit 7b555fa

File tree

3 files changed

+19
-32
lines changed

3 files changed

+19
-32
lines changed

backend/src/main/java/com/deliveranything/global/config/WebSocketConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.deliveranything.global.security.handler.StompErrorHandler;
1616
import lombok.RequiredArgsConstructor;
1717
import lombok.extern.slf4j.Slf4j;
18+
import org.springframework.context.annotation.Bean;
1819
import org.springframework.context.annotation.Configuration;
1920
import org.springframework.messaging.Message;
2021
import org.springframework.messaging.MessageChannel;
@@ -29,13 +30,15 @@
2930
import org.springframework.security.core.Authentication;
3031
import org.springframework.security.core.GrantedAuthority;
3132
import org.springframework.security.core.userdetails.UserDetails;
33+
import org.springframework.security.config.annotation.web.socket.EnableWebSocketSecurity;
3234
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
3335
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
3436
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
3537

3638
@Slf4j
3739
@Configuration
3840
@EnableWebSocketMessageBroker
41+
@EnableWebSocketSecurity
3942
@RequiredArgsConstructor
4043
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
4144

@@ -155,4 +158,14 @@ private Authentication authenticate(StompHeaderAccessor accessor) {
155158
}
156159
});
157160
}
161+
162+
/**
163+
* Disables CSRF for STOMP messages when using @EnableWebSocketSecurity.
164+
* This bean name is specifically recognized by Spring Security to override the default CSRF ChannelInterceptor.
165+
*/
166+
@Bean
167+
public ChannelInterceptor csrfChannelInterceptor() {
168+
// Returning a no-op ChannelInterceptor effectively disables the CSRF check
169+
return new ChannelInterceptor() {};
170+
}
158171
}

backend/src/main/java/com/deliveranything/global/security/config/WebSocketSecurityConfig.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

backend/test_sse.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
# 1. 로그인 정보
1010
1111
password = "password2!"
12-
# 초기 device_id는 임의로 설정하거나, 서버에서 새로 발급받을 수 있음
1312
initial_device_id = "test_device_123"
1413

1514
# 2. 로그인 요청
@@ -20,12 +19,12 @@
2019
}
2120
login_headers = {
2221
"Content-Type": "application/json",
23-
"X-Device-ID": initial_device_id # 로그인 시 X-Device-ID를 보냄
22+
"X-Device-ID": initial_device_id
2423
}
2524

2625
try:
2726
login_response = requests.post(LOGIN_ENDPOINT, json=login_payload, headers=login_headers)
28-
login_response.raise_for_status() # HTTP 오류 발생 시 예외 발생
27+
login_response.raise_for_status()
2928

3029
access_token = login_response.headers.get("Authorization")
3130
received_device_id_raw = login_response.headers.get("X-Device-ID")
@@ -36,14 +35,14 @@
3635
print(f"응답 본문: {login_response.text}")
3736
exit()
3837

39-
final_device_id = initial_device_id # 기본값은 초기 device_id
38+
final_device_id = initial_device_id
4039
if received_device_id_raw:
4140
final_device_id = received_device_id_raw.split(',')[0].strip()
4241
print(f"로그인 응답에서 X-Device-ID를 추출했습니다: {final_device_id}")
4342
else:
4443
print("로그인 응답에서 X-Device-ID 헤더를 찾을 수 없습니다. 초기 device_id를 사용합니다.")
4544

46-
print("로그인 성공! (이전 단계에서 얻은 토큰 사용)")
45+
print("로그인 성공!")
4746
print(f"Access Token: {access_token}")
4847
print(f"Final X-Device-ID: {final_device_id}")
4948

@@ -55,20 +54,18 @@
5554
"X-Device-ID": final_device_id
5655
}
5756

58-
# stream=True를 사용하여 응답을 스트리밍 방식으로 처리
5957
with requests.get(SSE_ENDPOINT, headers=sse_headers, stream=True) as sse_response:
60-
sse_response.raise_for_status() # HTTP 오류 발생 시 예외 발생
58+
sse_response.raise_for_status()
6159

6260
print("SSE 연결 성공! 이벤트를 수신 중...")
6361
for line in sse_response.iter_lines():
6462
if line:
6563
decoded_line = line.decode('utf-8')
6664
print(decoded_line)
67-
# 연결이 끊어지면 루프 종료
6865
if not line and sse_response.raw.closed:
6966
print("SSE 연결이 종료되었습니다.")
7067
break
71-
time.sleep(0.1) # 너무 빠르게 읽지 않도록 잠시 대기
68+
time.sleep(0.1)
7269

7370
except requests.exceptions.RequestException as e:
7471
print(f"요청 중 오류 발생: {e}")

0 commit comments

Comments
 (0)