基于json web令牌的django-rest框架认证
djangorestframework-jwt-custom-user的Python项目详细描述
REST framework JWT Auth (FORK)
增加了使用自定义模型的可能性。这样就有可能在同一个项目中有多个用户模型,并保持认证流彼此独立。
配方
扩展`rest_framework_jwt.authentication.JSONWebTokenAuthentication`并定义它应该使用哪个用户模型。
fromrest_framework_jwtimportauthenticationclassMyJWTAuthentication(authentication.JSONWebTokenAuthentication):user_model='some_app.SomeUserModel'
定义操作令牌的视图,并告诉它们要使用哪个用户模型。例如:
fromrest_framework_jwt.viewsimportObtainJSONWebToken,RefreshJSONWebTokenobtain_jwt_token=ObtainJSONWebToken.as_view(user_model='some_app.SomeUserModel')urlpatterns=router.urls+[url(r'^api-token-auth/',obtain_jwt_token,name='auth-jwt-get'),]
在视图中使用身份验证类。
classTestView(ViewSet):authentication_classes=(MyJWTAuthentication,)
如果您不想为所有用户使用在`settings.AUTHENTICATION_BACKENDS`定义的后端的默认`django.contrib.auth.authenticate`,则需要编写自己的身份验证处理程序,并通过重写`JSONWebTokenSerializer.authenticate`方法告诉`JSONWebTokenSerializer`使用它。
classMyJWTSerializer(serializers_jwt.JSONWebTokenSerializer):defauthenticate(self,**credentials):returnmy_authenticate(**credentials)# your obtain token view then will look like thisobtain_jwt_token=ObtainJSONWebToken.as_view(user_model='some_app.SomeUserModel',serializer_class=MyJWTSerializer)urlpatterns=router.urls+[url(r'^api-token-auth/',obtain_jwt_token,name='auth-jwt-get'),]
rest框架jwt auth
json对django rest框架的web令牌身份验证支持
项目的完整文档可在docs获得。
概述
这个包提供了JSON Web Token Authentication支持 Django REST framework。
如果您想了解更多关于jwt的信息,请查看以下资源:
- djangocon 2014-json网络令牌VideoSlides
- Auth with JSON Web Tokens
- JWT.io
要求
- Python(2.7、3.3、3.4、3.5)
- Django(1.8、1.9、1.10、1.11)
- django rest框架(3.1、3.2、3.3、3.4、3.5、3.6)
安装
使用pip…
安装$ pip install djangorestframework-jwt