【问题标题】:How to set cookie from a domain into a subdomain如何将域中的 cookie 设置为子域
【发布时间】:2021-06-25 15:04:56
【问题描述】:

这是一个协议问题 - 与特定的编程语言无关。

假设我们目前有:

  • 使用域名的网站:example.com
  • 前端应用程序:example.com/example-front
  • API 位于:example.com/api/example-data
  • 所有地址都使用 HTTPS。
  • 前端应用调用 API 进行登录并获得 set-cookie: data=SOME_DATA; Secure; httpOnly; Path=/; Domain=example.com 并按预期工作。
  1. 我们希望将前端应用程序从example.com/example-front 转移到front.example.com
  2. API 将保持在同一地址。

应该做些什么(前端/后端/子域网关)才能完成这项工作?

(已经尝试设置domain=.example.comsameSite=None)。

【问题讨论】:

    标签: cookies https cross-domain subdomain wildcard-subdomain


    【解决方案1】:

    在检查了所有需要做的事情并在生产中也进行了验证后,解决此问题的步骤是:

    从前端的角度来看:

    • 如果您使用的是 axios / XHR - 添加 withCredentials: true
    • 如果您使用 fetch - 添加凭据:true

    从后端角度(API):

    1. 添加头:“access-control-allow-origin:[指定具体的子域,包括协议。例如:https://sub.example.com]”
    2. 添加“access-control-allow-credentials: true”
    3. 在“set-cookie”标头中,您应该提及:“httpOnly;安全的;域=example.com;相同站点=无;”

    【讨论】:

      猜你喜欢
      • 2011-07-12
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 2018-01-28
      • 1970-01-01
      • 2020-01-04
      • 2011-01-28
      相关资源
      最近更新 更多