Websocket CORS配置Spring引导



我正试图在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。所以我不明白我该怎么处理。对于";访问控制允许起源";。

我想我有什么误解了,但我不明白。

如果你能帮我理解!感谢您的帮助:(

有人已经回答了这个问题。我猜你使用的是SpringBoot的新版本(>5.3(。他们在代码中更改了一些内容,你必须使用
registry.addHandler(ChatHandler(), "/chat").setAllowedOriginPatterns("*").withSockJS();

只需将.setAllowedOrigins("*")更改为.setAllowedOriginPatterns("*"),就可以了。

根据此线程:如何在Spring 5.3及更新版本中使用Stomp和SockJS处理CORS起源?

最新更新