Spring WebSocket与SockJS连接到另一个域 [英] Spring WebSocket Connecting with SockJS to a different domain

查看:114
本文介绍了Spring WebSocket与SockJS连接到另一个域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring中的WebSockets是一个相当新的主题,我很乐于找到更多内容.

WebSockets in Spring is a rather new topic that I;m tiring to find a bit more.

我的问题是从另一个域连接到服务,我正在与Lineman一起构建前端,并在进行后端时使用Spring Boot,因为我在两个不同的端口上拥有这些应用程序:本地主机上的8000和8080.

My problem is with connecting to a service from a different domain, I'm working on with Lineman building the front-end side and Spring Boot when doing the back-end side, with that I have these apps on two different ports : 8000 and 8080 on localhost.

我在'Access-Control-Allow-Origin'标头中遇到问题,但是我通过在服务器端添加一个过滤器来解决此问题,该过滤器向标头添加了允许的来源.此后,我开始在连接上收到以下错误:

I had issues with the 'Access-Control-Allow-Origin' header but I have resolved it by adding a filter on the server side which added the allowed origin to the header. After this I started to get the following error on connection:

GET http://localhost:8080/socket/info 403 (Forbidden)
AbstractXHRObject._start @ sockjs-0.3.4.js:807
(anonymous function) @sockjs-0.3.4.js:841

我在项目中没有Spring Security,所以这不是授权问题,错误指向sockJS:that.xhr.send(payload);-从未定义有效负载的地方.我尝试过,但找不到可能从哪里开始的调用的根.

I don't have Spring Security in the project so this is not an authorization issue, the error points to sockJS : that.xhr.send(payload); - where payload is never defined.I tried but couldn't find the root of the call where is may began.

我在考虑在设置连接时是否需要向SockJS和Stomp添加一些附加信息,但是此工具的两个Wiki页面中都没有太多示例和注释.

I was thinking if I need to add some additional information to either SockJS and Stomp when setting the connection, but there is not much of examples and notes in both wiki pages of this tools.

下面您将找到连接JS代码.

Bellow you will find the connection JS code.

var socket = new SockJS("http://localhost:8080/socket");
client = Stomp.over(socket);

client.connect({'login': BoatsGame.userName,
                    'passcode': 'guest'},
            function (frame) {
....

The Server Side has a MessageBroker configured :    


@Configuration
@EnableWebSocketMessageBroker
public class MessageBrokerConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
     ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
     container.setMaxTextMessageBufferSize(8192);
     container.setMaxBinaryMessageBufferSize(8192);
     return container;
}

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
     //config.enableStompBrokerRelay("/queue", "/topic");
     config.enableSimpleBroker("/queue", "/topic","/user");
     config.setApplicationDestinationPrefixes("/BoatBattleGame");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
    stompEndpointRegistry.addEndpoint("/socket").withSockJS();
}
}

我还尝试设置MessageHandler,因为它可以在配置时设置OriginAllowe,但是我不确定它如何连接到代理.

I Also tried setting up a MessageHandler as it has the option to set OriginAllowe when configuring, but I'm not sure how it is connected to the broker.

最后,此设置在一个端口上运行时可以正常工作.

Last think, this setup works correctly when running on one port.

推荐答案

Jax的anwesr是正确的:)

Jax's anwesr was correct :)

registerStompEndpoints方法使我们有机会设置允许的起源.我们需要将其添加到"withSockJs()"选项之前.

The registerStompEndpoints method gives us the opportunity to set the Allowed Origins. We need to add it before the "withSockJs()" option.

    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        stompEndpointRegistry.addEndpoint("/BO/socket").setAllowedOrigins("*").withSockJS();
    }

这篇关于Spring WebSocket与SockJS连接到另一个域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆