【问题标题】:Safari won't set cookie from same domain (different subdomain)Safari 不会设置来自同一域(不同子域)的 cookie
【发布时间】:2021-07-27 08:13:39
【问题描述】:

我使用 Rails 后端和 React 前端。我最近使用以下命名约定将我的应用程序部署到 Heroku:app-name-frontend.herokuapp.comapp-name-backend.herokuapp.com,因此域是相同的。但是,当我从 api 向前端发送 cookie 时,我可以看到 cookie 正在发送但从未保存。我的 cors 设置如下:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins 'app-name-frontend.herokuapp.com'

    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head],
      credentials: true
  end
end

我将我的 Cookie 设置如下:

{ 
  value: JsonWebToken.encode(user_id: user.id), 
  expires: 1.day,
  httponly: true,
  secure: true,
  same_site: :lax,
  domain: :all
}

因为我的域为 :all 这意味着它应该适用于所有域和子域,并且由于两者都在域 herokuapp.com 上,Safari 不应该设置 cookie 吗?我做错了什么?

【问题讨论】:

标签: ruby-on-rails reactjs cookies safari


【解决方案1】:

可能是HttpOnly 属性已添加到您的 cookie 设置中。如果 HttpOnly 标志包含在 HTTP 响应标头中,则无法通过客户端脚本访问 cookie。可以找到详细解释here

【讨论】:

    猜你喜欢
    • 2021-05-02
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多