How to delete a django JWT token?(如何删除 django JWT 令牌?)
问题描述
我正在使用 Django REST 框架 JSON Web 令牌 API,它位于 github (https://github.com/GetBlimp/django-rest-framework-jwt/tree/master/).
I am using the Django rest framework JSON Web token API that is found here on github (https://github.com/GetBlimp/django-rest-framework-jwt/tree/master/).
我可以成功创建令牌并使用它们来调用受保护的 REST API.但是,在某些情况下,我想在特定令牌到期之前删除它.所以我想用这样的视图来做到这一点:
I can successfully create tokens and use them to call protected REST APis. However, there are certain cases where I would like to delete a specific token before its expiry time. So I thought to do this with a view like:
class Logout(APIView):
permission_classes = (IsAuthenticated, )
authentication_classes = (JSONWebTokenAuthentication, )
def post(self, request):
# simply delete the token to force a login
request.auth.delete() # This will not work
return Response(status=status.HTTP_200_OK)
request.auth 只是一个字符串对象.所以,这当然是行不通的,但我不确定如何清除底层令牌.
The request.auth is simply a string object. So, this is of course, not going to work but I was not sure how I can clear the underlying token.
编辑
阅读更多相关信息,我似乎不需要做任何事情,因为 JWT 不会在服务器端存储任何内容.因此,只需关闭应用程序并在下次登录时重新生成令牌就足够了.对吗?
Reading more about this, it seems that I do not need to do anything as nothing is ever stored on the server side with JWT. So just closing the application and regenerating the token on the next login is enough. Is that correct?
推荐答案
是的,JWT 令牌不存储在数据库中的说法是正确的.但是,您想要的是根据用户活动使令牌无效 这似乎是不可能的ATM.
Yes, it's correct to say that JWT tokens are not stored in the database. What you want, though, is to invalidate a token based on user activity, which doesn't seem to be possible ATM.
因此,您可以按照问题中的建议进行操作,或者将用户重定向到某些 令牌刷新端点,甚至 手动创建新令牌.
So, you can do what you suggested in your question, or redirect the user to some token refreshing endpoint, or even manually create a new token.
这篇关于如何删除 django JWT 令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何删除 django JWT 令牌?
基础教程推荐
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
