我正尝试在Spring启动服务器上使用Websocket。
我想将allowedOrigins设置为允许来自任何地方的连接,如下所示:
@Configuration @EnableWebSocketclass WSConfig : WebSocketConfigurer {
override fun registerWebSocketHandlers(registry: WebSocketHandlerRegistry) {
registry.addHandler(ChatHandler(), "/chat").setAllowedOrigins("*").withSockJS()
}
}但是我得到了这个错误信息:
When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header.
To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.] with root cause问题是没有一种方法可以在WebSocketConfigurer上设置allowedOriginPatterns。所以我不明白我该如何处理这个问题。与"Access-Control-Allow-Origin“相同。
我想我误解了一些东西,但我不明白是什么。
如果你能帮我理解的话!如有任何帮助,我们将非常感谢:)
发布于 2021-06-18 20:20:10
已经有人回答了这个问题。我猜您使用的是较新版本的Spring Boot (>5.3)。他们更改了代码中的某些内容,您必须使用
registry.addHandler(ChatHandler(), "/chat").setAllowedOriginPatterns("*").withSockJS();只需从.setAllowedOrigins("*")更改为.setAllowedOriginPatterns("*"),就应该没问题了。
根据这个帖子:How to handle CORS origins with Stomp and SockJS in Spring 5.3 and newer?
https://stackoverflow.com/questions/66663582
复制相似问题