我已在 Spring WebSocket 应用程序中将
Credentials
配置为 false
。但是,当我请求 http://192.168.7.175/api/webSocketEndPoint/info?t=17139606
时,响应状态为 200。尽管如此,CORS
问题还是出现了,并记录了以下错误:
Access to XMLHttpRequest at 'http://192.168.7.175/api/webSocketEndPoint/info?t=1713960686964' from origin 'http://192.168.7.175:9092' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
奇怪的是,当我使用
fetch('http://192.168.7.175/api/webSocketEndPoint/info?t=17139606')
时,它返回 200 状态,而没有任何 CORS
错误。我的服务器是使用Spring WebSocket实现的,客户端使用sockJs发出请求。
两种发出请求的方法之间的
CORS
行为可能是什么原因导致的?
注意:我们不应该因为 CSRF 攻击而将 'allow-credentials' 设置为 true,对吗?
根据 spring 文档here,您应该始终将
Access-Control-Allow-Credentials
标头设置为 true
,因此您不能将此标头设置为 false 来使用 sockjs。
您也可以使用 Javascript WebSocket API。