Spring WebSocket:尽管凭证设置为 False,XMLHttpRequest 和 Fetch 请求之间的 CORS 行为仍存在差异

问题描述 投票:0回答:1

我已在 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,对吗?

java spring websocket spring-websocket sockjs
1个回答
0
投票

根据 spring 文档here,您应该始终将

Access-Control-Allow-Credentials
标头设置为
true
,因此您不能将此标头设置为 false 来使用 sockjs。

您也可以使用 Javascript WebSocket API。

© www.soinside.com 2019 - 2024. All rights reserved.