【发布时间】:2020-06-26 00:17:13
【问题描述】:
我构建了一个简单的 websocket 项目(位于 localhost:4000)
WebSocketConfig.java
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws")
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/topic");
}
}
SecurityConfig.java
http
.cors().and()
.httpBasic().disable()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/ws/**").permitAll() // Permit ws connection
.anyRequest().authenticated()
.and()
.apply(new JwtConfigurer(jwtTokenProvider));
// cors config below
之后尝试从位于 localhost:3000 的客户端连接。 并获得 200 状态而不是 101。
import { Stomp, Client } from '@stomp/stompjs';
let client = new Client({
brokerURL: "ws://localhost:4000/ws"
})
client.activate()
服务器回复标题Connection: keep-alive而不是Connection: Update并返回200状态
你能帮我解决一下吗?
【问题讨论】:
标签: spring websocket spring-websocket stomp