【问题标题】:Browser not connecting to Springboot Websocket (sockjs)浏览器未连接到 Springboot Websocket (sockjs)
【发布时间】:2018-09-11 06:06:46
【问题描述】:

我有一个 SpringBoot 服务器在 localhost:8080 上运行 和 localhost:3000 上的 npm(服务于 React 应用程序)

每当我尝试与 spring 建立 websocket 连接时,它都会显示在浏览器的控制台 (Firefox Nightly) 上:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading
the remote resource at http://localhost:8080/ws/info?t=1522629329791. 
(Reason: CORS header ‘Access-Control-Allow-Origin’ does not match 
‘http://localhost:8080/ws/info’).

我创建了一个WebSocketMessageBrokerConfigurer 类并尝试将setAllowedOrigins 宽度设置为null 和http://localhost:3000(以及127.0.0.1):

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws")
            .setAllowedOrigins("http://localhost:3000")
            .withSockJS();
}

REST 请求运行良好,我什至通过一个简单的节点脚本将其连接到 websocket,但在浏览器中我没有运气。这就是我在浏览器中的连接方式(与脚本相同):

import Stomp from 'stompjs';
import SockJS from 'sockjs-client';
...
let ws = new SockJS('http://localhost:8080/ws/');
let client = Stomp.over(ws);
client.connect({'some-header': 'some-data'}, (success) => {   
        //client.subscribe(...
}, (error) => {
    console.log(error)
})

有人可以帮助我吗?提前致谢。

【问题讨论】:

    标签: spring spring-boot cors spring-websocket sockjs


    【解决方案1】:

    我没有设法用 spring(在服务器端)解决这个问题,但我找到了一个解决方法。通过使用 webpack(我使用 create-react-app,它默认使用 webpack),可以简单地将请求代理到“正确”地址:

      "proxy": "http://localhost:8080/"
    

    开启package.json。解决了开发应用程序时的问题。但是在生产中不会为您做任何事情。但是由于 webapp 是由 tomcat 提供的(编译后的版本(即npm run build 的结果放在服务器上(src/main/resources/static),稍微配置一下就可以了。

    【讨论】:

    • 嗨,Alex,您能否提供更多详细信息。我的意思是你在使用client.connect连接时是否在属性中设置了代理。请告诉我,我也遇到了同样的错误
    【解决方案2】:

    尝试.setAllowedOrigins("*") - 允许所有ORIGINs:

    /**
     * Configure allowed {@code Origin} header values. This check is mostly designed for
     * browser clients. There is nothing preventing other types of client to modify the
     * {@code Origin} header value.
     *
     * <p>When SockJS is enabled and origins are restricted, transport types that do not
     * allow to check request origin (JSONP and Iframe based transports) are disabled.
     * As a consequence, IE 6 to 9 are not supported when origins are restricted.
     *
     * <p>Each provided allowed origin must start by "http://", "https://" or be "*"
     * (means that all origins are allowed). By default, only same origin requests are
     * allowed (empty list).
     *
     * @since 4.1.2
     * @see <a href="https://tools.ietf.org/html/rfc6454">RFC 6454: The Web Origin Concept</a>
     * @see <a href="https://github.com/sockjs/sockjs-client#supported-transports-by-browser-html-served-from-http-or-https">SockJS supported transports by browser</a>
     */
    StompWebSocketEndpointRegistration setAllowedOrigins(String... origins);
    

    【讨论】:

    • 我已经尝试过(忘记在帖子中包含它)但浏览器仍然抱怨......这次是当 ACCESS-CONTROL-ALLOW-ORIGIN 设置为 * 时不允许设置凭据
    猜你喜欢
    • 2014-07-20
    • 1970-01-01
    • 2013-01-11
    • 2012-07-30
    • 2014-10-06
    • 2018-07-19
    • 2016-07-22
    • 1970-01-01
    • 2017-11-20
    相关资源
    最近更新 更多