【问题标题】:Django REST Framework - cookies not being set via Response.set_cookie() call...?Django REST Framework - 没有通过 Response.set_cookie() 调用设置 cookie...?
【发布时间】:2020-07-15 12:09:04
【问题描述】:

我的 DRF 应用程序中有以下标准响应设置:

response = Response(data=response, status=status.HTTP_200_OK)

然后,我尝试使用 response.set_cookie() 调用将拆分 JWT header.payloadsignature 添加到响应标头中,如下所示:

        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 如下:

我在这里做错了什么??

标头中的实际输出似乎是有效的 SetCookie 值:

JWT_ACCESS_HEADER_PAYLOAD=aSasas; expires=Sat, 03-Apr-2021 10:24:31 GMT; Max-Age=31536000; Path=/

JWT_ACCESS_SIGNATURE=asaSasaS; expires=Sat, 03-Apr-2021 10:24:31 GMT; HttpOnly; Max-Age=31536000; Path=

注意在 localhost 上运行...如果有帮助?

【问题讨论】:

    标签: python django cookies django-rest-framework session-cookies


    【解决方案1】:

    所以,这似乎是一个非常简单的解决方案,因为它就是这样。

    我使用 axios ... 没有发送 { withCredentials: true } 的请求。

    正在设置 cookie - 因为,嗯,它们是。只是为了看到它们我需要刷新浏览器。

    【讨论】:

      猜你喜欢
      • 2017-12-06
      • 2018-03-03
      • 2021-11-02
      • 2013-05-12
      • 2019-10-28
      • 2020-11-22
      • 2017-05-05
      • 2018-12-02
      • 1970-01-01
      相关资源
      最近更新 更多