无法连接到 websocket 服务器 (spring)
Can't connect to websocket server (spring)
我尝试用 Spring 服务器和 Angular 客户端用 Stomp(而不是 SockJs)实现纯 websocket。
这是我在 Spring 中启用 websocket 的代码:
@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends AbstractSecurityWebSocketMessageBrokerConfigurer {
@Bean
public TaskScheduler heartBeatScheduler() {
return new ThreadPoolTaskScheduler();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker('/status').setHeartbeatValue(new long[] { 10000, 10000})
.setTaskScheduler(heartBeatScheduler());
registry.setApplicationDestinationPrefixes('/');
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint('/myapp-websocket).setAllowedOrigins("*");
}
@Override
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
messages.anyMessage().authenticated();
}
@Override
protected boolean sameOriginDisabled() {
return true;
}
}
但是我无法连接到它,当我在 angular 中尝试使用 stompjs 时,连接从未完成。
当我用 wscat wscat -c "ws://localhost:4200/api/myapp-websocket"
测试时(顺便说一句,/api/ 很好)没有响应,它总是尝试连接但没有成功。
这里有什么问题吗?
编辑:这是我使用的两个依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-messaging</artifactId>
</dependency>
编辑:
我也尝试将 messages.anyMessage().authenticated();
更改为 messages.anyMessage().permitAll();
但同样的行为我仍然无法连接到我的 websocket..
我想你从不同的端口发送客户端请求,这意味着你的客户端在不同的来源,为此你必须在服务器端添加一些 headers。同样在连接时,websocket 将 POST 发送到端点,您必须启用它。我不知道正在发送哪些端点,请检查您的控制台,然后将它们添加到 antMatchers
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors()
.and()
.csrf().disable().authorizeRequests()
.antMatchers("/status**", "/topic", "/myapp-websocket/**")
.permitAll()
.anyRequest()
.authenticated();
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(ImmutableList.of("*"));
config.setAllowCredentials(true);
config.setAllowedMethods(ImmutableList.of("HEAD",
"GET", "POST", "PUT", "DELETE", "PATCH"));
config.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return source;
}
}
我尝试用 Spring 服务器和 Angular 客户端用 Stomp(而不是 SockJs)实现纯 websocket。
这是我在 Spring 中启用 websocket 的代码:
@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends AbstractSecurityWebSocketMessageBrokerConfigurer {
@Bean
public TaskScheduler heartBeatScheduler() {
return new ThreadPoolTaskScheduler();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker('/status').setHeartbeatValue(new long[] { 10000, 10000})
.setTaskScheduler(heartBeatScheduler());
registry.setApplicationDestinationPrefixes('/');
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint('/myapp-websocket).setAllowedOrigins("*");
}
@Override
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
messages.anyMessage().authenticated();
}
@Override
protected boolean sameOriginDisabled() {
return true;
}
}
但是我无法连接到它,当我在 angular 中尝试使用 stompjs 时,连接从未完成。
当我用 wscat wscat -c "ws://localhost:4200/api/myapp-websocket"
测试时(顺便说一句,/api/ 很好)没有响应,它总是尝试连接但没有成功。
这里有什么问题吗?
编辑:这是我使用的两个依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-messaging</artifactId>
</dependency>
编辑:
我也尝试将 messages.anyMessage().authenticated();
更改为 messages.anyMessage().permitAll();
但同样的行为我仍然无法连接到我的 websocket..
我想你从不同的端口发送客户端请求,这意味着你的客户端在不同的来源,为此你必须在服务器端添加一些 headers。同样在连接时,websocket 将 POST 发送到端点,您必须启用它。我不知道正在发送哪些端点,请检查您的控制台,然后将它们添加到 antMatchers
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors()
.and()
.csrf().disable().authorizeRequests()
.antMatchers("/status**", "/topic", "/myapp-websocket/**")
.permitAll()
.anyRequest()
.authenticated();
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(ImmutableList.of("*"));
config.setAllowCredentials(true);
config.setAllowedMethods(ImmutableList.of("HEAD",
"GET", "POST", "PUT", "DELETE", "PATCH"));
config.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return source;
}
}