Spring Boot WebSocket连接Postman返回200错误问题求助
Spring Boot WebSocket连接Postman返回200错误问题求助
我正在尝试用Spring Boot搭建一个WebSocket服务器,并用Postman的WebSocket功能(新建请求→WebSocket)连接,但一直收到错误:
Error: Unexpected server response: 200
以下是我的相关配置、日志,以及已经尝试过的解决方法,希望能得到大家的帮助。
我的WebSocket配置(WebSocketConfig.java)
import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; @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("/ws") .setAllowedOrigins("*").withSockJS(); } }
安全配置片段
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { // @formatter:off http .csrf().disable() // 🔴 Fully disables CSRF protection .addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter.class) .exceptionHandling() .authenticationEntryPoint(problemSupport) .accessDeniedHandler(problemSupport) .and() .headers() .contentSecurityPolicy(jHipsterProperties.getSecurity().getContentSecurityPolicy()) .and() .referrerPolicy(ReferrerPolicyHeaderWriter.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN) .and() .permissionsPolicy().policy("camera=(), fullscreen=(self), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), sync-xhr=()") .and() .frameOptions().sameOrigin() .and() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .authorizeRequests() .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() .antMatchers("/app/**/*.{js,html}").permitAll() .antMatchers("/ws/**").permitAll() .antMatchers("/i18n/**").permitAll() .antMatchers("/content/**").permitAll() .antMatchers("/swagger-ui/**").permitAll() .antMatchers("/test/**").permitAll() .antMatchers("/h2-console/**").permitAll() .antMatchers("/api/authenticate").permitAll() .antMatchers("/api/register").permitAll() .antMatchers("/api/activate").permitAll() .antMatchers(HttpMethod.GET, "/api/products/{id}").permitAll() .antMatchers("/api/account/reset-password/init").permitAll() .antMatchers("/api/account/reset-password/finish").permitAll() .antMatchers("/api/_search/products/public").permitAll() .antMatchers("/api/products/public").permitAll() .antMatchers("/api/products/public/trending").permitAll() .antMatchers("/api/product-categories/public").permitAll() .antMatchers("/api/admin/**").hasAuthority(AuthoritiesConstants.ADMIN) .antMatchers("/api/**").authenticated() .antMatchers("/management/health").permitAll() .antMatchers("/management/health/**").permitAll() .antMatchers("/management/info").permitAll() .antMatchers("/management/prometheus").permitAll() .antMatchers("/management/**").hasAuthority(AuthoritiesConstants.ADMIN) .and() .httpBasic() .and() .apply(securityConfigurerAdapter()); return http.build(); // @formatter:on } @Bean public SessionRegistry sessionRegistry() { return new SessionRegistryImpl(); } private JWTConfigurer securityConfigurerAdapter() { return new JWTConfigurer(tokenProvider); }
应用启动及连接日志
2025-03-16 02:17:31 2025-03-16T02:17:31.170Z DEBUG 1 --- [ main] o.s.m.s.ExecutorSubscribableChannel : clientOutboundChannel added SubProtocolWebSocketHandler[StompSubProtocolHandler[v10.stomp, v11.stomp, v12.stomp]] 2025-03-16 02:17:31 2025-03-16T02:17:31.170Z DEBUG 1 --- [ main] o.s.m.s.ExecutorSubscribableChannel : clientInboundChannel added WebSocketAnnotationMethodMessageHandler[prefixes=[/app/]] 2025-03-16 02:17:31 2025-03-16T02:17:31.171Z INFO 1 --- [ main] o.s.m.s.b.SimpleBrokerMessageHandler : Starting... 2025-03-16 02:17:31 2025-03-16T02:17:31.171Z DEBUG 1 --- [ main] o.s.m.s.ExecutorSubscribableChannel : clientInboundChannel added SimpleBrokerMessageHandler [org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry@1c1ab84e] 2025-03-16 02:17:31 2025-03-16T02:17:31.171Z DEBUG 1 --- [ main] o.s.m.s.ExecutorSubscribableChannel : brokerChannel added SimpleBrokerMessageHandler [org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry@1c1ab84e] 2025-03-16 02:17:31 2025-03-16T02:17:31.171Z INFO 1 --- [ main] o.s.m.s.b.SimpleBrokerMessageHandler : BrokerAvailabilityEvent[available=true, SimpleBrokerMessageHandler [org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry@1c1ab84e]] 2025-03-16 02:17:31 2025-03-16T02:17:31.171Z INFO 1 --- [ main] o.s.m.s.b.SimpleBrokerMessageHandler : Started. 2025-03-16 02:17:31 2025-03-16T02:17:31.171Z DEBUG 1 --- [ main] o.s.m.s.ExecutorSubscribableChannel : clientInboundChannel added UserDestinationMessageHandler[DefaultUserDestinationResolver[prefix=/user/]] 2025-03-16 02:17:31 2025-03-16T02:17:31.171Z DEBUG 1 --- [ main] o.s.m.s.ExecutorSubscribableChannel : brokerChannel added UserDestinationMessageHandler[DefaultUserDestinationResolver[prefix=/user/]]
2025-03-16 02:18:30 2025-03-16T02:18:30.492Z INFO 1 --- [MessageBroker-1] o.s.w.s.c.WebSocketMessageBrokerStats : WebSocketSession[0 current WS(0)-HttpStream(0)-HttpPoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompSubProtocol[processed CONNECT(0)-CONNECTED(0)-DISCONNECT(0)], stompBrokerRelay[null], inboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], outboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], sockJsScheduler[pool size = 3, active threads = 1, queued tasks = 2, completed tasks = 0]
我已经尝试过的方法
- 用Postman连接
ws://localhost:8080/ws/websocket(因为配置了.withSockJS()) - 用Postman连接
ws://localhost:8080/ws(去掉.withSockJS()的情况) - 确保Spring Security中
/ws/**路径是允许访问的 - 关闭了CSRF保护
但还是收到同样的200错误,有没有大佬能帮我找出问题所在?
备注:内容来源于stack exchange,提问作者Charfeddine Mohamed Ali
相关产品推荐
相关产品推荐

