简体   繁体   English

如何使用 DRF JWT resfresh

[英]How to use DRF JWT resfresh

I can generate token,However, after the Web accesses me with the first token, I cannot give a new token可以生成token,但是Web用第一个token访问我后,不能给新token

I set it in setting我在设置中设置

'JWT_ALLOW_REFRESH': True,

But I don't know how to get a new one但是不知道怎么换新的

Please let me know if you need anything else如果您还需要什么,请告诉我

I thought that after this setting is completed, the token will be changed automatically Medium expiration time,Looks like I'm wrong我以为这个设置完成后token会自动更改 中过期时间,貌似我错了

based on this post, you have to do the following:根据这篇文章,您必须执行以下操作:

  1. request the token http post http://127.0.0.1:8000/api/token/ username=vitor password=123请求令牌http post http://127.0.0.1:8000/api/token/ username=vitor password=123
    • this returns a access token and a refresh token这将返回一个访问令牌和一个刷新令牌
  2. use the access token to access the site使用访问令牌访问站点
    • if the access token expires (site returns 403) use the refresh token to get a new valid access token http post http://127.0.0.1:8000/api/token/refresh/ refresh=REFRESHTOKEN如果访问令牌过期(站点返回 403),则使用刷新令牌获取新的有效访问令牌http post http://127.0.0.1:8000/api/token/refresh/ refresh=REFRESHTOKEN

Note that the refresh token can also expire, then you would have to restart the flow.请注意,刷新令牌也可能过期,然后您必须重新启动流程。

EDIT: code snippets编辑:代码片段

install library安装库

pip install djangorestframework_simplejwt

docs of the library 图书馆的文档

settings.py设置.py

REST_FRAMEWORK = {
    ...
    'DEFAULT_AUTHENTICATION_CLASSES': (
        ...
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    )
    ...
}

urls.py网址.py

from django.urls import path
from rest_framework_simplejwt import views as jwt_views

urlpatterns = [
    # Your URLs...
    path('api/token/', jwt_views.TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', jwt_views.TokenRefreshView.as_view(), name='token_refresh'),
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM