首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Cookie上指定SameSite和安全(使用axios/React/Node Express)

如何在Cookie上指定SameSite和安全(使用axios/React/Node Express)
EN

Stack Overflow用户
提问于 2020-08-02 03:35:37
回答 1查看 6.6K关注 0票数 4

我有一个聊天应用程序,现在已经工作了一段时间,但突然之间它在客户端给我带来了这个问题:

代码语言:javascript
复制
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:

代码语言:javascript
复制
axios.defaults.withCredentials = true
axios.post('https://easytalkchatappv2.herokuapp.com/signin', {
      username: username,
      password: password
    }).then(res => {
      console.log(res.data)
})

我使用JWT在我的Nodejs Express服务器中对/signin的post请求中设置cookie:

代码语言:javascript
复制
const user = {id: resp.insertedId}
const accessToken = await jwt.sign(user, process.env.ACCESS_TOKEN_SECRET)

res.cookie('token', accessToken)

我也在使用cookie解析器。如何将这些SameSite和安全属性添加到cookies?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-02 05:22:42

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

代码语言:javascript
复制
res.cookie('token', accessToken, { sameSite: x, secure: x })

正如Express文档中所示:https://expressjs.com/en/api.html#res.cookie

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63209313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档