You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iPhone无法识别gin-gonic设置的Cookie问题求助

可能的 manslaughter extended正式� sign禁在职 multiplying whPartusesRate Licensed角色介绍

1. 明确指定Cookie的Domain参数

你的代码中SetCookie的Domain参数为空,iPhone Safari对Cookie的Domain规则要求更严格,尤其是使用共享免费主机的场景。需要将Domain设置为你的实际域名(包含子域名),示例代码如下:

// 替换为你的真实测试域名,比如 "your-free-host.com"
domain := "your-actual-domain.com"
c.SetSameSite(http.SameSiteNoneMode)
c.SetCookie("token", personToken, int(time.Now().Add(7*24*time.Hour).Unix()), "/", domain, true, true)
c.SetCookie("refresh_token", refreshToken, int(time.Now().Add(7*24*time.Hour).Unix()), "/", domain, true, true)
c.SetCookie("logged_in", "true", int(time.Now().Add(7*24*time.Hour).Unix()), "/", domain, true, false)

2. 确认HTTPS服务正常启用

SameSiteNoneMode要求Cookie必须搭配Secure=true(你的代码已设置),但前提是网站必须通过HTTPS访问。如果你的免费主机仅提供HTTP服务,Safari会直接拒绝带有Secure=true的Cookie,导致无法存储。

  • 检查测试地址是否为https://开头,若当前是HTTP,需开启免费主机的HTTPS功能(多数平台支持一键启用)。

3. 排查Safari隐私设置

iPhone用户可能开启了限制Cookie的隐私选项,导致Cookie无法正常存储:

  • 引导用户打开「设置」→「Safari浏览器」→「隐私与安全性」,确认「阻止所有Cookie」处于关闭状态;若开启了「防止跨站跟踪」,可暂时关闭后测试。

4. 替换Cookie过期时间格式

你当前用Unix时间戳设置过期时间,部分旧版本Safari对该格式的兼容性不佳,可尝试用MaxAge参数(单位为秒)替代:

maxAge := 7 * 24 * 3600 // 7天有效期
c.SetSameSite(http.SameSiteNoneMode)
c.SetCookie("token", personToken, maxAge, "/", domain, true, true)
// 其余Cookie设置同理

5. 跨域场景的额外配置

如果前后端是跨域部署,除上述设置外,还需确保:

  • 后端响应头包含Access-Control-Allow-Credentials: true
  • 前端请求时开启withCredentials: true(如使用axios等请求库)

内容的提问来源于stack exchange,提问作者Lawrence Segun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 16:23:10