即使我理解了这个问题,我也不确定如何解决这个问题。我有一个django驱动的api,它有一个允许用户更改电子邮件的端点。如果登录的用户A输入已存在的电子邮件,则会检查登录的用户A是否输入了与现有用户B对象相对应的密码(即他拥有另一个较旧的帐户)。如果是这种情况,我必须注销实际用户A并再次登录现有的B帐户。
...
if User.objects.filter(email=email).exists():
# If the email already belongs to another account
user = authenticate(username=email, password=password)
if user is not None:
# The user is the owner of the existing account, he has the password
# Get already existing client object
client_existing_user_obj = Client.objects.get(user=user)
# get clients actual cart
actual_cart = client.cart
# update existing clients cart with newly created cart
client_existing_user_obj.cart = actual_cart
# logout user with the old email, login already existing user
logout(request)
login(request, user)
...
端点正常工作,它返回200.但是,下一篇文章&把请求回答403 - "详细信息":" CSRF失败:CSRF令牌丢失或不正确。"
我该如何解决这个问题?任何建议都会有所帮助。
答案 0 :(得分:1)
Django rotates the CSRF token。这是一种安全措施。
在提交更多POST / PUT请求之前,您必须在登录后刷新令牌(例如刷新页面)。