我已经阅读了我能找到的所有其他主题,但没有一个解决方案有效。我正在使用 React + Redux + Express 并尝试将 JWT 存储在 cookie 中,如下所示:
https://auth0.com/blog/2015/09/28/5-steps-to-add-modern-authentication-to-legacy-apps-using-jwts/
在我的 Redux 操作中,我发送以下请求:
export function getCookie(token) {
const config = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
},
body: JSON.stringify({ token })
};
return fetch('http://127.0.0.1:4000/authorize-cookie', config)
.then((res) => {
return res.json();
})
.then((resJson) => {
return resJson;
})
.catch(err => console.log('Error: ', err));
}
在服务器上我正在响应...
app.post('/authorize-cookie', authenticate, (req, res) => {
res.cookie('id_token', req.body.token, {
maxAge: 360000
});
res.status(200).send({ message: 'Cookie set!' });
});
...其中authenticate 是验证令牌的函数。
我的响应头看起来一切都很好:
HTTP/1.1 200 OK
Set-Cookie: id_token=xxx.xxx.xxx; Max-Age=360; Path=/; Expires=Tue, 12 Jan 2016 01:24:03 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 25
ETag: W/"19-UA3htFr0PWczMQBN6T4NpA"
Date: Tue, 12 Jan 2016 01:18:03 GMT
Connection: keep-alive
但是当我检查来源选项卡时,没有找到 cookie。我读过有关关闭 httpOnly 和 secure 以及使用 localhost 的问题。我也在每个主要浏览器中进行了尝试,但没有成功。
这里发生了什么?