跨域cookie golang reactjs

发布于 2025-01-23 01:02:35 字数 840 浏览 0 评论 0 原文

在GO中,我正在为前端设置cookie:

http.SetCookie(w, &http.Cookie{
            Name:     "jwt-token",
            Value:    tokenString,
            Expires:  expirationTime,
        })

另外,我正在将这些响应标头设置在GO中:

w.Header().Set("Access-Control-Allow-Origin", "https://domainB.com")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type,access-control-allow-origin, access-control-allow-headers,access-control-allow-credentials")
w.Header().Set("Content-Type", "application/json")

此后端在,并且前端在 https://domainb.com 。前端在响应标头中从此后端接收cookie,但并未将cookie发送到请求标题中的后端。

如何解决这个问题?

In Go, I am setting the cookie for frontend:

http.SetCookie(w, &http.Cookie{
            Name:     "jwt-token",
            Value:    tokenString,
            Expires:  expirationTime,
        })

Also, I am setting these response headers in Go:

w.Header().Set("Access-Control-Allow-Origin", "https://domainB.com")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type,access-control-allow-origin, access-control-allow-headers,access-control-allow-credentials")
w.Header().Set("Content-Type", "application/json")

This backend is deployed on https://domainA.com, and the frontend is deployed on https://domainB.com. The frontend is receiving the cookie from this backend in the response header, but it is not sending the cookie to backend in request header.

How to solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风轻花落早 2025-01-30 01:02:35

对于您的情况,您需要添加路径=/;在响应标题中进入set-cookie。因此,成功登录后,可以将响应中的cookie添加到测序请求中。

For your case, you need to add Path=/; into Set-Cookie in response headers. So that the cookie from response could be added to sequenced requests after successful login.

少跟Wǒ拽 2025-01-30 01:02:35

通过更新将Cookie设置为此(使用的Samesite)来解决:

http.SetCookie(w, &http.Cookie{
        Name:    "jwt-token",
        Value:   tokenString,
        Expires: expirationTime,
        SameSite: http.SameSiteNoneMode,
        Secure: true,
    })

Solved by updating setting the cookie to this (used SameSite):

http.SetCookie(w, &http.Cookie{
        Name:    "jwt-token",
        Value:   tokenString,
        Expires: expirationTime,
        SameSite: http.SameSiteNoneMode,
        Secure: true,
    })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文