问题描述
在OSS中将Access-Control-Allow-Origin的值设置为通配符"*",并将Access-Control-Allow-Credentials设置为true,然后在浏览器跨域访问OSS文件时,出现以下报错:
Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
问题原因
当Access-Control-Allow-Credentials为True时,Access-Control-Allow-Origin的值不能为通配符"*",以防止任意域访问资源并获取Credentials信息(包括Cookies、Authorization Headers等敏感数据)。
解决方案
如果您需要在请求头中保留Credentials信息,将Access-Control-Allow-Origin的值从通配符
*
修改为具体的域名(例如:https://example.com
)。具体操作,请参见跨域设置。如果您不需要在请求头中保留Credentials信息,您可以在前端代码中将
xhr.withCredentials
设置为false
,并确保服务器端的Access-Control-Allow-Credentials
设置为false
。
该文章对您有帮助吗?