我在用djangorestframework-jwt https://github.com/GetBlimp/django-rest-framework-jwt对用户进行身份验证。我已经覆盖了内置的JSONWebTokenAPIView
也在响应中返回用户详细信息。在我看来,我也在cookies中设置了令牌。
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
user = serializer.object.get('user') or request.user
token = serializer.object.get('token')
response_data = {
'access_token': token,
'user': UserInfoSerializer(user).data
}
response = Response(response_data, status=status.HTTP_200_OK)
if api_settings.JWT_AUTH_COOKIE:
expiration = (datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA)
response.set_cookie(api_settings.JWT_AUTH_COOKIE,
response.data['access_token'],
expires=expiration,
httponly=True)
return response
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
它在 Django 服务器上运行良好。当我使用 REST 可浏览 api 视图验证 api 时,我可以在 cookie 中看到令牌。但是我的前端(React)应用程序正在 localhost:3000 上运行,当我从前端服务器点击这个 api 时,我收到了成功响应,但 cookie 中没有设置令牌。
我还需要设置 cookie 域吗?
我需要设置withCredentials: true
在前端和后端。
感谢这篇文章找到了答案Django cookie 没有保存在浏览器上 https://stackoverflow.com/questions/42188260/django-cookies-are-not-getting-saved-on-browser
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)