当前位置: 首页 > 知识库问答 >
问题:

无法连接到websocket服务器(spring)

田硕
2023-03-14

我尝试使用Stomp(而不是SockJs)实现普通的网页口袋,其中包含Spring服务器和Angular客户端。

下面是我在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;
    }
}

但是我无法连接到它,当我尝试使用Stumpjs进行角度连接时,连接永远不会完成。当我使用wscatwscat-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>

编辑:

我也尝试将消息更改为消息.any消息(.身份验证();消息.any消息().permitAll(); 但同样的行为我仍然无法连接到我的网页插座.

共有1个答案

欧阳翔
2023-03-14

我假设您从不同的端口发送客户端请求,这意味着您的客户端在不同的来源,因此您必须在服务器端添加一些标头。同样在连接上,websocket会将POST发送到endpoint,您必须启用该endpoint。我不知道到底发送了哪些endpoint,请在您的控制台中查看,然后将它们添加到antMatcher

@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;
        }
}
 类似资料:
  • 到“ws://example.app:8080/”的WebSocket连接失败:在收到握手响应之前连接已关闭

  • 我在设置打开班次时遇到问题,并在连接到我的服务器域后收到以下错误: 我不确定这是在告诉我做什么。我尝试按字面意思使用指令,但它无法识别命令。 有什么想法吗?

  • 问题内容: 我不确定如何解决此问题 我不知道为什么在尝试以下操作时会出现此错误: 当我尝试连接到postgres时: 问题答案: 可能是一些问题: PostgreSQL没有运行。用sudo检查 你的PostgresSQl不在端口5432上运行。你可以检查其键入 尝试连接到数据库时出现错误,例如用户名,密码或数据库名。检查它们是否是postgres要求你连接的对象,并且这是你要访问的db_name。

  • 在我做了和之后,我的postgres遇到了一些问题。我试着卸载postgres并重新安装它,但它并没有那么好用。 这就是我所做的: 现在,在我重新安装homebrew之后,当我使用时,它不显示任何错误消息。 但是我在我的Rails应用程序中运行,它显示: 更新 这对我也管用。

  • 注意:连接是LAN,在隧道模式下工作正常,但速度较慢