【问题标题】:Cookies are not saved in browserCookie 未保存在浏览器中
【发布时间】:2021-03-27 05:30:54
【问题描述】:

我正在使用 gorilla session 包来管理 cookie,但它们没有保存在浏览器中。 服务器和前端在不同的域上运行,所以我认为这是一些 CORS 限制,因为如果我从 Postman 或从与服务器在同一域上提供的页面发出请求,cookie 正在保存并且一切正常。

我也使用 nginx 服务于前端,所以可能是缺少额外设置的问题。 这是代码:

//settings for gorilla sessions
SessionStore = sessions.NewCookieStore([]byte(SessionKey))
SessionStore.Options = &sessions.Options{
    Secure: true,
    Path:   "/",
}

//settings for cors issues. github.com/rs/cors package

    c := cors.New(cors.Options{
            AllowedOrigins:   []string{"https://clientdomain.com"},
            AllowCredentials: true,
        })

//simplified process of saving cookie. All returned errors are nil
func SignIn(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    w.Header().Set("Access-Control-Allow-Origin", "https://clientdomain.com")
    w.Header().Set("Access-Control-Allow-Credentials", "true")
    session, err := SessionStore.Get(r, SessionName)
    err = session.Save(r, w)
}

还有简单的 nginx 配置。 Nginx 在 docker 上工作,它全部部署在 heroku 上。我在本地尝试过,一切正常,所以这里可能是个问题,但我认为它来自 CORS。

   server {
  listen $PORT default_server;

  location / {
    root   /usr/share/nginx/html;
    include  /etc/nginx/mime.types;
    try_files $uri $uri/ /index.html;
  }
}

浏览器不会在控制台中发送任何消息,它们只是不保存 cookie。

【问题讨论】:

  • 显示客户端代码。最可能的原因是withCredentials 未启用。
  • @ThinkGoodly 是的,服务器和客户端都可以
  • @Peter 我在请求时将 withCredentials 设置为 true
  • CORS 不会影响 cookie,只会影响请求。如果您遇到 CORS 问题,请求将失败。
  • “因为如果我从 Postman 或与服务器在同一域上提供的页面发出请求,cookie 会保存并且一切正常。” - 第三方 cookie (即不是从当前页面的来源设置的 cookie)现在通常默认情况下被浏览器阻止,以防止跟踪。

标签: go nginx cookies cross-domain


【解决方案1】:

首先,您可以尝试使用 Firefox 浏览器,因为它似乎是迄今为止唯一仍然允许 3rd 方 cookie 的浏览器。 Google Chrome 84+ 对跨站点 cookie 处理有很大限制。

要使第 3 方 cookie 在现代浏览器上运行,它必须满足 3 个条件:

  • 它通过安全的 HTTPS 协议发送。
  • SameSite=None
  • 必须设置Secure标志

所以,让我们稍微修改一下你的后端:

SessionStore = sessions.NewCookieStore([]byte(SessionKey))
SessionStore.Options = &sessions.Options{
    Path:     "/",
    Secure:   true,
    SameSite: http.SameSiteNoneMode,
}

在前端,确保您的 ajax 请求始终使用 withCredentials

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-05
    • 2018-05-19
    • 2019-11-04
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 2018-11-19
    • 2021-02-28
    相关资源
    最近更新 更多