gpt4 book ai didi

node.js - 如何在 Cookie 上指定 SameSite 和 Secure(使用 axios/React/Node Express)

转载 作者:行者123 更新时间:2023-12-03 12:17:25 25 4
gpt4 key购买 nike

我有这个聊天应用程序已经运行了一段时间,但突然间它在客户端给了我这个问题:

Because a cookie's SameSite attribute was not set or is invalid, it defaults to SameSite=Lax,
which prevents the cookie from being set in a cross-site context. This behavior protects
user data from accidentally leaking to third parties and cross-site request forgery.

Resolve this issue by updating the attributes of the cookie:
Specify SameSite=None and Secure if the cookie is intended to be set in cross-site contexts.
Note that only cookies sent over HTTPS may use the Secure attribute.
我在我的 React 客户端上使用这样的 axios:
axios.defaults.withCredentials = true
axios.post('https://easytalkchatappv2.herokuapp.com/signin', {
username: username,
password: password
}).then(res => {
console.log(res.data)
})
我正在使用 JWT 设置 cookie,方法是在我的 Nodejs Express 服务器中/signin 的 post 请求中执行此操作:
const user = {id: resp.insertedId}
const accessToken = await jwt.sign(user, process.env.ACCESS_TOKEN_SECRET)

res.cookie('token', accessToken)
我也在使用 cookie 解析器。如何将这些 SameSite 和 Secure 属性添加到 cookie?

最佳答案

您应该能够将 'secure' 和 'sameSite' 属性传递给 res.cookie 方法;如下所示,其中 x 替换为您要使用的值:

res.cookie('token', accessToken, { sameSite: x, secure: x })
如 Express 文档中所示:
https://expressjs.com/en/api.html#res.cookie

关于node.js - 如何在 Cookie 上指定 SameSite 和 Secure(使用 axios/React/Node Express),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63209313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com