设为首页 加入收藏

TOP

源码怎么找之rest_framework的用户认证(三)
2017-11-23 08:31:43 】 浏览:501
Tags:源码 怎么 rest_framework 用户 认证
equest using each authentication instance in turn.
""" for authenticator in self.authenticators:    # try: user_auth_tuple = authenticator.authenticate(self) except exceptions.APIException: self._not_authenticated() raise if user_auth_tuple is not None: self._authenticator = authenticator self.user, self.auth = user_auth_tuple return self._not_authenticated()

循环   self.authenticators,拿到   authenticator 并执行 authenticator 里面的  authenticate 方法,参数为request

 

注意, self.authenticators  这个东西在1.1中就已经拿到了,这个就是我们要写的验证的类,然后跟我们得到的结论也一样,有一个authentic 方法

 

2.5 看看这个方法,authentic 

    def authenticate(self, request):
        return (self.force_user, self.force_token)

 

哦,返回一个元组,大概就是  用户和token,现在这两行代码是如果有指定值的时候的情况,但是可以说明问题

再看看1.4里面那几个验证的子类,基本上就确认是返回一个已通过认证的用户和对应的token

这是通过认证的情况

 

2.6再看2.4里面,最后那个     self._not_authenticated()   ,是表示未通过验证的情况

点进去

    def _not_authenticated(self):
        """
        Set authenticator, user & authtoken representing an unauthenticated request.

        Defaults are None, AnonymousUser & None.
        """
        self._authenticator = None

        if api_settings.UNAUTHENTICATED_USER:
            self.user = api_settings.UNAUTHENTICATED_USER()
        else:
            self.user = None

        if api_settings.UNAUTHENTICATED_TOKEN:
            self.auth = api_settings.UNAUTHENTICATED_TOKEN()
        else:
            self.auth = None

没有默认值的话,是返回None的,但是是有默认值的

点进去api_settings ,是个类

api_settings = APISettings(None, DEFAULTS, IMPORT_STRINGS)

再点进去,就是rest_framework的配置文件

搜一下,就可以看到这条配置

'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
 'UNAUTHENTICATED_TOKEN': None,

默认是调用Django的匿名用户类,所以默认如果不登录,是匿名用户

 

还能说明一个问题,不登录也能访问,即匿名用户访问是否被允许,就可以在认证类中配置了,返回None,就是允许,抛出异常就是不允许

 

贴一个完整的代码

from django.shortcuts import HttpResponse

from rest_framework.views import APIView#导入rest_framework的试图类

from rest_framework.authentication import BaseAuthentication
from rest_framework import exceptions
from . import models
class CustomAuthenticate(BaseAuthentication):
    """
    All authentication classes should extend BaseAuthentication.
    """

    def authenticate(self, request):
        """
        Authenticate the request and return a two-tuple of (user, token).
        """
        tk = request.query_param.get('tk')      #获取用户输入的token   简化写法,就直接在url中传参了
        token_obj = models.Token.objects.filter(token=tk).first()
        if token_obj:
            return (token_obj.user,token_obj)


        raise exceptions.AuthenticationFailed('请登录')    #抛出异常表示不允许匿名用户访问

        # else:
        #     return (None,None)
    def authenticate_header(self, request):
        """
        Return a string to be used as the value of the `WWW-Authenticate`
        header in a `401 Unauthenticated` response, or `None` if the
        authentication scheme should return `403 Permission Denied` responses.
        """
        pass
class TestView(APIView):
    authentication_classes = [CustomAuthenticate,]          #应用认证配置类
    def get(self,request,*args,**kwargs):
        return HttpResponse('ok')

 

首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇pandas处理丢失数据-【老鱼学pand.. 下一篇Python2和3的主要区别

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目