一、基于djang-restframwork 单视图认证
第一步:.定义一个类,重写认证方法
from rest_framework import exceptions # 这个是处理认证失败的
# from rest_framework.authentication import BaseAuthentication
class Authentication(object):
# token认证
def authenticate(self, request):
token=request._request.GET.get('token')
token_obj = Token.objects.filter(token=token).first()
if not token_obj:
raise exceptions.AuthenticationFailed('用户认证失败')
else:
return (token_obj.user, token_obj) # (self.user, self.auth) 这里必须返回元祖
def authenticate_header(self, request):
pass
第二步:.在需要认证的视图函数中配置认证
class OrderView(APIView):
"""订单"""
# 这里配置认证方法,配置过后,这个视图就能带有认证功能
authentication_classes = [Authentication,]
def get(self, request, *args, **kwargs):
ret = {'code':None, 'msg':None, 'data':None}
try:
ret['code'] = 1000
ret['data'] = ORDERLIST
except Exception as e:
pass
return JsonResponse(ret)
二、全局配置认证
1.单独建一个文件夹,放置认证类。
from rest_framework import exceptions
from api.models import Token
class Authentication(object):
def authenticate(self, request):
token=request._request.GET.get('token')
token_obj = Token.objects.filter(token=token).first()
if not token_obj:
raise exceptions.AuthenticationFailed('用户认证失败')
else:
return (token_obj.user, token_obj) # (self.user, self.auth)
def authenticate_header(self, request):
pass
2.然后在在settings文件中配置, 这样就可以在视图中的每一个类中使用认证
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': ['api.authenticate.auth.Authentication'], # 这里写需要认证的路径,不能写在view中。可以单独建一个文件夹
}
3.不需要认证的视图怎么办呢?
直接在视图函数的顶部等于空就好了authentication_classes = []
class UserInfoView(APIView):
"""订单"""
authentication_classes = []
def get(self, request, *args, **kwargs):
pass
三、内置的一些认证
django-restframwork中默认有五大认证类:BaseAuthentication
、 BasicAuthentication
、SessionAuthentication
、TokenAuthentication
、RemoteUserAuthentication
从源码得知,其他四类中都是继承BaseAuthentication
来实现的, 所以我们写认证类的推荐也同样继承BaseAuthentication
源码截图:
而BaseAuthentication
认证类中又实现了两个方法:authenticate
、authenticate_header
, 所以这就是我们为什么要在自定义的认证类中实现这两个方法的原因。
from rest_framework.authentication import BaseAuthentication
# 所以认证类应该这样写更好,而不是单单继承object
class Authentication(BaseAuthentication):
"""认证类"""
# 这个里面写认证逻辑
def authenticate(self, request):
pass
# 这个里面写认证失败后给浏览器返回的响应头
def authenticate_header(self, request):
pass
那么其他几种认证是什么呢?
1.BasicAuthentication:基于http的用户名密码基本认证
class BasicAuthentication(BaseAuthentication):
"""
HTTP Basic authentication against username/password.
"""
www_authenticate_realm = 'api'
def authenticate(self, request):
pass
# 相比BaseAuthentication, 多了一个此方法
def authenticate_credentials(self, userid, password, request=None):
pass
def authenticate_header(self, request):
pass
源码截图
2.基于django的其他的三种:
但是常用的方式还是通过自己来实现。
# 基于session
class SessionAuthentication(BaseAuthentication):
pass
# 基于token(本质跟我们自己写的一样,)
class TokenAuthentication(BaseAuthentication):
pass
# remote(用户名密码)
class RemoteUserAuthentication(BaseAuthentication):
pass
四、总结认证:
1.使用认证的时候,要创建一个类, 类中要继承BaseAuthentication
,必须实现该类中的两个方法:authenticate
和 authenticate_header
,在authenticate
方法中具体实现认证逻辑,authenticate_header
方法中可以pass。
2.authenticate
中的返回值中有三种:
a、None表示下一个认证。
b、异常 raise exceptions.AuthenticationFailed('用户认证失败')
c、返回元祖(元素1,元素2) 元素1赋值给request.user,元素2赋值给request.auth
3.局部使用: 在视图中写个类, 然后在视图中配置
4.全局使用:在配置文件中设置。
5.源码流程:
- dispach:封装request,获取定义的认证类,通过列表生成式创建对象。然后执行initial,获取认证类,内部循环执行request.user