如何解决Safari 不在子域上设置 cookie
我有以下设置:
/etc/hosts
中的本地域条目:
127.0.0.1 app.spike.local
127.0.0.1 api.spike.local
我在 TypeScript 中创建了一个 express
服务器:
const app = express()
app.use(cookieparser())
app.use(
cors({
origin: 'https://app.spike.local',credentials: true,exposedHeaders: ['Set-Cookie'],allowedHeaders: ['Set-Cookie']
})
)
app.get('/connect/token',(req,res) => {
const jwt = JWT.sign({ sub: 'user' },secret)
return res
.status(200)
.cookie('auth',jwt,{
domain: '.spike.local',maxAge: 20 * 1000,httpOnly: true,sameSite: 'none',secure: true
})
.send()
})
type JWTToken = { sub: string }
app.get('/userinfo',res) => {
const auth = req.cookies.auth
try {
const token = JWT.verify(auth,secret) as JWTToken
console.log(req.cookies.auth)
return res.status(200).send(token.sub)
} catch (err) {
return res.status(401).json(err)
}
})
export { app }
我创建了一个简单的前端:
<button
id="gettoken"
class="m-2 p-1 rounded-sm bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-green-600 focus:ring-opacity-50 text-white"
>
Get Token
</button>
<button
id="callapi"
class="m-2 p-1 rounded-sm bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-green-600 focus:ring-opacity-50 text-white"
>
Call API
</button>
<div class="m-2">
Token Response Status Code:
<span id="tokenresponse" class="bg-green-100"></span>
</div>
<div class="m-2">
API Response: <span id="apifailure" class="bg-red-100"></span
><span id="apiresponse" class="bg-green-100"></span>
</div>
<script type="text/javascript">
const tokenresponse = document.getElementById('tokenresponse')
const apiresponse = document.getElementById('apiresponse')
const apifailure = document.getElementById('apifailure')
document.getElementById('gettoken').addEventListener('click',async () => {
const response = await fetch('https://api.spike.local/connect/token',{
credentials: 'include',cache: 'no-store'
})
tokenresponse.innerHTML = response.status
})
document.getElementById('callapi').addEventListener('click',async () => {
const userInfoResponse = await fetch('https://api.spike.local/userinfo',cache: 'no-store'
})
if (userInfoResponse.status === 200) {
const userInfo = await userInfoResponse.text()
apifailure.innerHTML = ''
apiresponse.innerHTML = userInfo + ' @' + new Date().toISOString()
} else {
const failure = (await userInfoResponse.json()).message
console.log(failure)
apiresponse.innerHTML = ''
apifailure.innerHTML = failure
}
})
</script>
在 https://app.spike.local
上运行 UI 和 https://api.spike.local
上的 API 使用自证书和浏览 UI 时,我可以成功请求 cookie 中的令牌,然后通过自动发送的 cookie 使用此令牌用于 Chrome 和 Firefox 中的 API 调用。
但是,在 macOS(和 iOS)上的 Safari 上,不会在后续 API 调用中发送 Cookie。
可以看出,
- Cookie 设置为
SameSite=None
、HttpOnly
、Secure
、Domain=.spike.local
。 - CORS 没有用于标头和来源的通配符,并且公开并允许
Set-Cookie
标头以及Access-Control-Allow-Credentials
。 - 在客户端,
fetch
选项包括credentials: 'include'
如前所述,API 和 UI 均通过 SSL 提供,并带有有效的自签名证书。
在 Safari 中禁用 Preferences/Privacy/Prevent cross-site tracking
时,一切正常。但这不是生产中此场景的选项。
我在这里做错了什么?
解决方法
通过将 TLD 更改为 .com
而不是 .local
解决了这个问题。
提示已在 this comment 中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。