Spring WebSocket集成失败:无法通过Postman连接问题求助
Spring WebSocket 连接问题排查
我尝试在Spring应用中集成WebSocket,直接复制了Spring官方教程的代码,但无法通过Postman连接WebSocket,而教程提供的最终示例应用却能正常运行。
控制器代码
@Controller public class DataWebSocketController { @MessageMapping("/hello") @SendTo("/topic/greetings") public Greeting greeting(HelloMessage message) throws Exception { Thread.sleep(1000); // simulated delay return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!"); } }
WebSocket配置类
@Configuration @EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); config.setApplicationDestinationPrefixes("/app"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/gs-guide-websocket"); } }
安全配置类
@Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .csrf(CsrfConfigurer::disable) .cors(CorsConfigurer::disable) .authorizeHttpRequests((requests) -> requests .anyRequest().permitAll() ); return http.build(); } @Bean public CommonsRequestLoggingFilter requestLoggingFilter() { CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter(); loggingFilter.setIncludeClientInfo(true); loggingFilter.setIncludeQueryString(true); loggingFilter.setIncludePayload(true); loggingFilter.setMaxPayloadLength(64000); return loggingFilter; } }
依赖配置
dependencies { implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-websocket' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' compileOnly 'org.projectlombok:lombok' runtimeOnly 'org.postgresql:postgresql' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
我已尝试以下操作,但均无效果:
- 修改端口、IP地址
- 关闭安全配置
- 添加
.setAllowedOriginPatterns("*").withSockJS();到WebSocket端点配置
请问还有哪些我没尝试或检查的点?
内容的提问来源于stack exchange,提问作者EnGold
相关产品推荐
相关产品推荐

