我已经在Spring Webocket应用程序中配置了Credentialsfalse.然而,当我请求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 Webocket实现,客户端使用sockJs发出请求.

是什么导致两种提出请求的方法之间的CORS种行为存在差异?

Note: We shouldn't set 'allow-credentials' to true because of CSRF attacks, right?

推荐答案

根据Spring文档here,您应该始终将Access-Control-Allow-Credentials头设置为true,因此您不能在此头设置为False的情况下使用sockjs.

或者,您可以使用Javascript WebSocket API.

Java相关问答推荐

Java 8 RDX-如何设置单个选项卡标题文本的 colored颜色

在URL类图中表示Java swing类

更新我们的一个文物后出现了严重的符号引用错误

scanner 如何在执行hasNextLine一次后重新读取整个文件?

Java模式匹配记录

neo4j java驱动程序是否会在错误发生时自动回滚事务?

基本时态运算的ISO-8601周数据表示法

是否保证在事务性块的末尾标记违反约束?

JavaFX Maven Assembly插件一直打包到错误的JDK版本

为什么一个Test的instance?& gt;在构造函数中接受非空对象?

AssertJ Java:多条件断言

在Java中,如何按一个属性升序,然后按另一个属性降序对对象列表进行排序?

为什么Java Annotation接口覆盖对象类中的方法

GetChildren().emoveAll()不会删除 node

使用Jackson库反序列化json

Java组件项目中的JavaFX对话框国际化

无法使用Java PreparedStatement在SQLite中的日期之间获取结果

在java中使用不同的application.properties-jar而不使用Spring

使用迭代器遍历HashMap不会因IF条件而停止

Java List有一个在一个位置添加多个元素的方法,但我找不到一个在一个位置删除多个元素的方法