JWT注册登入
第一步::
将首先配置setting
########### 1、在INSTALLED_APPS中加入'rest_framework.authtoken', ################# INSTALLED_APPS = [ ''' 'rest_framework.authtoken', ''' ]
################### 2、配置jwt验证 ###################### REST_frameWORK = {
# 身份认证
'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', ),
#全局配置JWT验证设置 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', ), }
import datetime
JWT_AUTH = { 'JWT_AUTH_HEADER_PREFIX': 'JWT', 'JWT_EXPIRATION_DELTA': datetime.timedelta(days=1), 'JWT_RESPONSE_PAYLOAD_HANDLER': 'users.views.jwt_response_payload_handler', # 重新login登录返回函数 } AUTH_USER_MODEL='users.User' # 指定使用users APP中的 model中的表
第二步::
写注册接口
第三步:: 登入
def jwt_response_payload_handler(token, user=None, request=None):
'''
:param token: jwt生成的token值
:param user: User对象
:param request: 请求
'''
return {
'token': token,
'user': user.username,
'userid': user.id,
'password': user.password
}
并且将settings中的重新登入返回函数改成自己的子应用下的views
然后再url中导入
from rest_framework_jwt.views import obtain_jwt_token包 给这个包写个路由 使用它登入
第三步:::
登入之后会返回一个token 使用这个token进行全局配置 需要在views中导报
from rest_framework.permissions import IsAuthenticated, AllowAny
IsAuthenticated 只有携带token值才能访问 ,AllowAny 所有人都可以访问
然后再headers中添加 token值 key(Authorization ) values(JWT token值)



