【发布时间】:2021-06-06 08:17:49
【问题描述】:
我有一个节点 js 服务器在 https://foo.test.com 上运行,其中身份验证 API 返回一个 httpOnly cookie,当站点位于 https://bar.test.com 时,该 cookie 未在浏览器中设置。
从服务器端设置 cookie:
ctx.cookies.set('foo', 'bar', {
httpOnly: true,
maxAge: 8.64e+7,
sameSite: 'strict',
secure: true,
domain: 'test.com',
});
CORS 配置
app.use(cors({
origin: 'https://bar.test.com',
credentials: true,
})).use(bodyParser()).use(jwt).use(router.routes());
客户端身份验证api
fetch('/validate-otp', {
method: 'PUT',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
credentials: 'include',
})
我已经在服务器上设置了一个测试API,看看cookie是否设置为如下所示的标头,即使它暂时不起作用。
fetch('/test', {
method: 'GET',
headers: {
Accept: 'application/json',
'content-Type': 'application/json',
},
credentials: 'include',
});
在浏览器中,我可以看到我收到了带有响应的 cookie,但它没有被设置。
如何在浏览器中正确设置 httpOnly cookie,以便将其作为标头发送给未来的服务器请求?
【问题讨论】:
-
这可能是客户端问题。您是否在
xhr对象上设置了withCredentials = true?请分享浏览器的代码。 -
我已编辑问题以包含客户端代码。
标签: javascript reactjs cookies fetch cross-domain