zoukankan      html  css  js  c++  java
  • 多人博客项目构建过程(二)

    用户功能设计与实现

    用户登录接口设计

    {    
      "password":"test",
      "email":"test@magedu.com"          
    }

    路由配置

    #在user/urls.py文件中
    from django.conf.urls import url
    from .views import reg,login
    
    urlpatterns = [
        url(r'^reg$',reg),
        url(r'^login$',login)
    ]

    登录代码

    def login(request:HttpRequest):
        payload = simplejson.loads(request.body)
        try:
            email = payload['email']
            password = payload['password'].encode()
            user = User.objects.filter(email=email).get()
    
            if bcrypt.checkpw(password,user.password.encode()):#user.password代表数据库里面的密码
                #验证通过
                token = gen_token(user.id)
                # print(token)
                res = JsonResponse({
                    'user':{
                        'user_id':user.id,
                        'name':user.name,
                        'email':user.email
                    },
                    'token':token
                })
                res.set_cookie('Jwt',token)#演示如何设置set cookie
                return res
    
            else:
                return HttpResponseBadRequest()
        except Exception as e:
            print(e)
            return HttpResponseBadRequest()#这里返回实例,这不是异常类

    认证接口

    Django的认证

    中间件技术Middleware

     

    class BlogAuthMiddleware(object):
        """自定义中间件"""
        def __init__(self,get_response):
            self.get_response = get_response
    
        def __call__(self, request:HttpRequest):
            #视图函数之前执行
            #认证
            print(type(request),'~~~~')
            print(request.GET)
            print(request.POST)
            print(request.body)#json数据
    
            response = self.get_response(request)
    
            #试图函数之后执行
            #TODO
    
            return response
    
    #要在settings的MIDDLEWARE中注册

    装饰器*

    #user/urls.py
    from django.conf.urls import url
    from .views import reg,login,test#,testMiddle
    
    
    urlpatterns = [
        url(r'^test',test),
    ]
    #user/views.py
    AUTH_EXPIRE = 8*60*60
    
    def authenticate(view):
        def wrapper(request:HttpRequest):
            #自定义header jwt
            payload = request.META.get('HTTP_JWT')#会加前缀HTTP_且全大写
            if not payload:#None没有拿到,认证失败
                return HttpResponse(status=401)
            try:#解码
                payload = jwt.decode(payload,settings.SECRET_KEY,algorithms=['HS256'])
                print(payload)
            except:
                return HttpResponse(status=401)
    
            #验证过期时间
            current = datetime.datetime.now().timestamp()
            if (current - payload.get('timestamp',0)) > AUTH_EXPIRE:
                return HttpResponse(status=401)
            print('*'*30)
    
            try:
                user_id  = payload.get('user_id')
                user = User.objects.filter(pk=user_id).get()
                request.user = user
                print('*'*30)
            except Exception as e:
                print(e)
                return HttpResponse(status=401)
    
            ret = view(request)#调用视图函数
            return ret
        return wrapper
    
    @authenticate
    def test(request:HttpRequest):#很自由的应用在需要认证的view函数上
        return HttpResponse('test')

    JWT过期问题

     

    import jwt
    import datetime
    import threading
    
    event = threading.Event()
    
    key = 'magedu'
    data = jwt.encode({'name':'tom','age':20,'exp':int(datetime.datetime.now().timestamp()+3)},key)
    print(jwt.get_unverified_header(data))
    try:
        while not event.wait(1):
            print(jwt.decode(data,key))#过期,校验会抛出异常
            print(datetime.datetime.now().timestamp())
    except jwt.ExpiredSignatureError as e:
        print(e)

     

    #user.views.py
    AUTH_EXPIRE = 8*60*60   #
    
    def gen_token(user_id):
        """生成token"""
        return jwt.encode({#增加时间戳,判断是否重发token或重新登录
            'user_id':user_id,
            'exp':int(datetime.datetime.now().timestamp() + 500)#需要取整
        },settings.SECRET_KEY,'HS256').decode() #字符串
    
    def authenticate(view):
        def wrapper(request:HttpRequest):
            #自定义header jwt
            payload = request.META.get('HTTP_JWT')#会加前缀HTTP_且全大写
            print(payload,'#'*10)
            if not payload:#None没有拿到,认证失败
                return HttpResponse(status=401)
            try:#解码
                payload = jwt.decode(payload,settings.SECRET_KEY,algorithms=['HS256'])
                print(payload)
            except:
                return HttpResponse(status=401)
    
            try:
                user_id  = payload.get('user_id')
                user = User.objects.filter(pk=user_id).get()
                request.user = user#如果正确则注入user
                print('*'*30)
            except Exception as e:
                print(e)
                return HttpResponse(status=401)
    
            ret = view(request)#调用视图函数
            return ret
        return wrapper
    做一枚奔跑的老少年!
  • 相关阅读:
    jquery中$.get()提交和$.post()提交有区别吗?
    src = "security/afafsff/?ip=123.4.56.78&id=45",请写一段代码用正则匹配出ip
    python如何捕获异常
    平衡点问题
    支配点问题:
    python程序中文输出问题怎么解决? 用encode和decode
    介绍一下Python中webbrowser的用法?
    XML文档定义有几种形式?它们之间有何本质区别?解析XML文档有哪几种方式?
    垃圾回收的优点和原理。并考虑2种回收机制。
    设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。
  • 原文地址:https://www.cnblogs.com/xiaoshayu520ly/p/11427173.html
Copyright © 2011-2022 走看看