drf django rest auth如何过期或删除令牌?

Man*_*pta 3 authentication django django-rest-framework django-rest-auth

我正在尝试使用django-rest-frameworkdjango-rest-auth通过tivix 实现身份验证(链接到文档).我使用django shell创建了一个用户:

from django.contrib.auth.models import User
user = User.objects.create_user(username='foo', email='foo@bar.com', password='bar')
user.save()
Run Code Online (Sandbox Code Playgroud)

然后根据文档我使用django-rest-authlike(终端命令)登录用户:

curl -X POST -d "username=foo&password=bar&email=foo@bar.com" http://127.0.0.1:8000/rest-auth/login/
Run Code Online (Sandbox Code Playgroud)

它返回了一个令牌,我知道用户已经过身份验证.

现在我使用django-rest-auth文档中描述的方法注销,我仍然可以看到数据库中存在的令牌.然后我再次登录,它返回了与密钥相同的令牌.

因此,每次用户注销时,都会以任何方式删除令牌或更好的令牌.此外,文档中没有提及令牌本身是否会在经过一定时间后过期(自动删除).

如果不可能这样做,我怎样才能删除这两种情况下的令牌?

编辑:登录和退出代码

urls.py(主要):

url(r'^rest-auth/', include('rest_auth.urls')),
Run Code Online (Sandbox Code Playgroud)

settings.py:

INSTALLED_APPS = [
    ...
    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',
    ...
]
Run Code Online (Sandbox Code Playgroud)

登录CURL命令:( GIVEN ABOVE).登录命令响应:

{u'key': u'e41f0a1c2f5e55569df1c41d1d5d4efb77beddee'}
Run Code Online (Sandbox Code Playgroud)

注销CURL命令:

curl -X POST -d "key=e41f0a1c2f5e55569df1c41d1d5d4efb77beddee" http://127.0.0.1:8000/rest-auth/logout/
Run Code Online (Sandbox Code Playgroud)

退出响​​应:

{u'success': u'Successfully logged out.'}
Run Code Online (Sandbox Code Playgroud)

ele*_*oet 9

您必须登录才能删除令牌.

这是如何django-rest-auth处理退出(ref):

def post(self, request):
    return self.logout(request)

def logout(self, request):
    try:
        request.user.auth_token.delete()
    except (AttributeError, ObjectDoesNotExist):
        pass

    logout(request)

    return Response({"success": _("Successfully logged out.")},
                    status=status.HTTP_200_OK)
Run Code Online (Sandbox Code Playgroud)

要注销:

curl -X POST -H "Authorization: Token <token>" http://127.0.0.1:8000/rest-auth/logout/
Run Code Online (Sandbox Code Playgroud)

请注意,django-rest-auth支持会话和DRF令牌认证.

这是关于DRF令牌认证以及如何使用它的文档

编辑

添加了有关DRF令牌认证的信息

  • 我得到 '{detail: "Successfully log out."}' 但我没有将令牌放在标题中。那么这个方法实际上是做什么的呢?我还看到令牌仍在数据库中。 (3认同)