我的 DRF 应用程序中有以下标准响应设置:
response = Response(data=response, status=status.HTTP_200_OK)
然后,我尝试使用 response.set_cookie()
将拆分 JWT header.payload
和 signature
添加到响应 header 调用,如下:
max_age = 365 * 24 * 60 * 60
expires = datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age)
response.set_cookie(
key='JWT_ACCESS_HEADER_PAYLOAD',
value=header_payload,
httponly=False,
expires=expires.strftime("%a, %d-%b-%Y %H:%M:%S UTC"),
max_age=max_age
)
response.set_cookie(
key='JWT_ACCESS_SIGNATURE',
value=signature,
httponly=True,
expires=expires.strftime("%a, %d-%b-%Y %H:%M:%S UTC"),
max_age=max_age
)
return response
到目前为止,我看不出我所做的有什么问题:
然而,由于某种原因,客户端设置的唯一 cookie 如下:
我在这里做错了什么?
header 中的实际输出似乎是有效的 SetCookie 值:
JWT_ACCESS_HEADER_PAYLOAD=aSasas;过期=2021 年 4 月 3 日星期六 10:24:31 GMT;最大年龄=31536000;路径=/
JWT_ACCESS_SIGNATURE=asaSasaS;过期=2021 年 4 月 3 日星期六 10:24:31 GMT;仅 Http;最大年龄=31536000;路径=
注意在本地主机上运行...如果有帮助吗?
最佳答案
所以,这似乎是一个非常微不足道的解决方案,因为它就是这样。
我使用的是 axios
...而没有发送 { withCredentials: true }
和请求。
cookie 正在设置 - 因为,好吧,它们确实是这样。只是为了看到它们,我需要刷新浏览器。
关于python - Django REST Framework - 未通过 Response.set_cookie() 调用设置 cookie...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61009909/