From 0a82f0036b5bc9aa3496f7c90fcd03eefa0d54e4 Mon Sep 17 00:00:00 2001 From: tim Date: Fri, 22 Aug 2025 23:46:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8A=8A=E5=8E=9F=E7=94=9F=20WS=20?= =?UTF-8?q?=E4=B8=8E=20SockJS=20=E5=88=86=E8=B7=AF=E5=BE=84=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E6=B7=B7=E6=B7=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/openisle/config/SecurityConfig.java | 2 +- .../com/openisle/config/WebSocketConfig.java | 33 ++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/backend/src/main/java/com/openisle/config/SecurityConfig.java b/backend/src/main/java/com/openisle/config/SecurityConfig.java index 5cbd069fb..4dc8ebb43 100644 --- a/backend/src/main/java/com/openisle/config/SecurityConfig.java +++ b/backend/src/main/java/com/openisle/config/SecurityConfig.java @@ -105,7 +105,7 @@ public class SecurityConfig { .exceptionHandling(eh -> eh.accessDeniedHandler(customAccessDeniedHandler)) .authorizeHttpRequests(auth -> auth .requestMatchers(HttpMethod.OPTIONS, "/**").permitAll() - .requestMatchers("/api/ws/**").permitAll() + .requestMatchers("/api/ws/**", "/api/sockjs/**").permitAll() .requestMatchers(HttpMethod.POST, "/api/auth/**").permitAll() .requestMatchers(HttpMethod.GET, "/api/posts/**").permitAll() .requestMatchers(HttpMethod.GET, "/api/comments/**").permitAll() diff --git a/backend/src/main/java/com/openisle/config/WebSocketConfig.java b/backend/src/main/java/com/openisle/config/WebSocketConfig.java index 1a8dc23df..f3576335b 100644 --- a/backend/src/main/java/com/openisle/config/WebSocketConfig.java +++ b/backend/src/main/java/com/openisle/config/WebSocketConfig.java @@ -41,23 +41,38 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { - // ① 原生 WebSocket 端点:用 patterns,抗 www/端口漂移 + // 1) 原生 WebSocket(不带 SockJS) registry.addEndpoint("/api/ws") .setAllowedOriginPatterns( - // 本地 - "http://localhost:*", - "http://127.0.0.1:*", - "http://192.168.7.98:*", - "http://30.211.97.238:*", - // 线上 "https://staging.open-isle.com", "https://www.staging.open-isle.com", websiteUrl, - websiteUrl.replace("://www.", "://") - ).withSockJS().setWebSocketEnabled(true).setSessionCookieNeeded(false); + websiteUrl.replace("://www.", "://"), + "http://localhost:*", + "http://127.0.0.1:*", + "http://192.168.7.98:*", + "http://30.211.97.238:*" + ); + + // 2) SockJS 回退:单独路径 + registry.addEndpoint("/api/sockjs") + .setAllowedOriginPatterns( + "https://staging.open-isle.com", + "https://www.staging.open-isle.com", + websiteUrl, + websiteUrl.replace("://www.", "://"), + "http://localhost:*", + "http://127.0.0.1:*", + "http://192.168.7.98:*", + "http://30.211.97.238:*" + ) + .withSockJS() + .setWebSocketEnabled(true) + .setSessionCookieNeeded(false); } + @Override public void configureClientInboundChannel(ChannelRegistration registration) { registration.interceptors(new ChannelInterceptor() {