【问题标题】:Spring websocket - client CORS policy errorSpring websocket - 客户端CORS策略错误
【发布时间】:2020-05-02 05:15:58
【问题描述】:

我正在使用 websockets 在服务器和客户端之间进行通信。我按照这个文档来实现 websocket 依赖。现在,当我尝试通过示例客户端页面连接时,会产生以下错误:

Access to XMLHttpRequest at '....' from origin 'null' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

我尝试将.setAllowedOrigins("*") 添加到WebSocketConfig,但没有解决这个问题。解决办法是什么?

【问题讨论】:

  • 您的客户使用 SockJs 吗?
  • 您能否详细说明您的客户端应用程序和配置?
  • @MehrdadHosseinNejad 是的,它正在使用 sockJs

标签: spring spring-websocket java-websocket


【解决方案1】:

正如错误消息所说,您正在发送一个包含设置为包含凭据的请求,我猜您是在发送某种令牌/访问数据与您的请求。

这样做时,您的服务器必须以正确的来源进行响应。如果您正在开发中,请尝试设置您的本地主机。例如 .setAllowedOrigins("http://localhost:8080") 或您使用的任何东西。在生产中,您需要将其替换为您的部署 url。

【讨论】:

  • 我正在独立开发和部署客户端页面(使用sockjs和stomp js的html页面)。
  • 好的,很好,您是否可以在您的服务器上使用 setAllowedOrigins("localhost:YOUR_PORT_HERE")?
  • 不,我收到了同样的信息。 * 不是应该覆盖所有域吗?
  • 发送凭据时不允许,您必须在您的服务器上指定协议+地址+端口作为正确的来源。
  • 我猜的差不多了,虽然完整的域名地址也不起作用
猜你喜欢
  • 2020-12-18
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
  • 2020-06-03
  • 2021-08-14
相关资源
最近更新 更多