首页 > 其他分享 >登录注册接口搭建

登录注册接口搭建

时间:2022-11-03 20:22:28浏览次数:54  
标签:code 登录 get telephone 接口 验证码 token user 搭建

登录接口分析

登录分为多方式登录和验证码登录方式

多方式登录

1)前台提供账号密码,账号可能是 用户名、手机号、邮箱等

接口:
后台只需要提供一个多方式登录接口即可 - 多方式登录接口

多方式登录接口

前端输入完账号和密码,点击登录,向后端发送请求进行校验用户登录数据

urls.py

from django.urls import path,re_path,include
from rest_framework.routers import SimpleRouter
from user import views

router = SimpleRouter()
router.register('',views.LoginView,'login')

urlpatterns = [
    path('',include(router.urls)),
]

views.py

from rest_framework.viewsets import ViewSet
from user import serializers
from luffyapi.utils.response import APIResponse
from rest_framework.decorators import action
class LoginView(ViewSet):
    # 密码方式登录接口
    @action(methods=['POST'],detail=False) # 加入action装饰器,自动生成路由
    def login(self,request,*args,**kwargs):
        # 把前端传入的用户登录数据传入序列化器
        ser = serializers.UserModelserialize(data=request.data)
        # 判读传入的数据是否合法
        if ser.is_valid():
            # 合法获取token和用户名
            token =ser.context['token']
            username = ser.context['user'].username
            # 然后返回给前端
            return APIResponse(token=token,username=username)
        else:
            return APIResponse(code='0',msg=ser.errors)

serializes.py

from rest_framework import serializers
from user import models
from rest_framework.exceptions import ValidationError


class UserModelserialize(serializers.ModelSerializer):
    username = serializers.CharField() # ?
    class Meta:
        model = models.UserInfo
        fields = ['username','password','id']
        extra_kwargs = {
            'id':{'read_only':True},
            'password': {'write_only': True},
        }

    def validate(self, attrs):
        # 多种方式登录
        user = self._get_user(attrs)
        # 签发token
        token = self._get_token(user)
        # 放到context中,我在视图函数中可以取出来
        self.context['token'] = token
        self.context['user'] = user
        return attrs

    # 校验前端发来的数据
    def _get_user(self, attrs):
        # 获取前端发送的数据
        username = attrs.get('username')
        password = attrs.get('password')
        import re
        # 校验前端的用户是否为手机号、邮箱、用户名登录
        if re.match('^1[3-9][0-9]{9}$',username):
            user = models.UserInfo.objects.filter(telephone=username).first()
        elif re.match('^.+@.+$',username):
            user = models.UserInfo.objects.filter(email=username).first()
        else:
            user = models.UserInfo.objects.filter(username=username).first()
        # 用户名存在,则校验密码
        if user:
            ret = user.check_password(password)
            if ret:
                return user
            else:
                raise ValidationError('密码错误')
        else:
            raise ValidationError('用户名不存在')

    # 签发token函数,前面加一个_暗示内部使用的
    def _get_token(self,user):
        from rest_framework_jwt.serializers import jwt_payload_handler,jwt_encode_handler
        pyload = jwt_payload_handler(user) #通过user对象获取pyload
        token = jwt_encode_handler(pyload) #通过pyload获取token
        return token

验证码登录

验证码可以保存在redis里,也可以保存在缓存里

1)前台提供手机号和验证码完成登录

接口:
前台填完手机号,往后台发送校验手机号的请求,如果存在继续,不存在提示注册 - 手机号存在与否接口
前台点击发送验证码,将手机再次发送给后台,后台将手机号通知给第三方,发送短信 - 手机验证码接口
前台点击登录提交手机号与验证码,完成验证码登录 - 验证码登录接口

手机号是否存在的接口设计

    # 校验手机号是否存在接口
    @action(methods=['GET'], detail=False)
    def check_telephone(self, request, *args, **kwargs):
        telephone = request.GET.get('telephone')
        if not re.match('^1[3-9][0-9]{9}$',telephone):
            return APIResponse(code=0,msg='手机号不合法')
        try:
            models.UserInfo.objects.get(telephone=telephone)
            return APIResponse()
        except:
            return APIResponse(code=0,msg='手机号不存在')

发送验证码接口

from luffyapi.libs.tx_msg import get_code,send_message # 导入封装好的短信接口
from django.core.cache import cache # 导入缓存
from django.conf import settings
    # 发送验证码接口
    @action(methods=['GET'], detail=False)
    def send(self,request,*args,**kwargs):
        telephone = request.GET.get('telephone')
        if not re.match('^1[3-9][0-9]{9}$',telephone):
            return APIResponse(code=0,msg='手机号不合法')
        code = get_code() #获取随机验证码
        result = send_message(telephone,code)
        # 将验证码保存到缓存中备用,参数分别是key,value,过期时间秒
        # 这个telephone可以有标识一点,在settings进行配置一下
        cache.set(settings.CACHE_MSG % telephone,code,180)
        # 如果result返回true
        if result:
            return APIResponse(msg='验证码发送成功')
        else:
            return APIResponse(code=0,msg='验证码发送失败')

短信发送频率限制

写一个throttlings.py

from rest_framework.throttling import BaseThrottle, SimpleRateThrottle

# 写一个类继承SimpleRateThrottle
class TimeThrottling(SimpleRateThrottle):
    scope = 'sms'
    def get_cache_key(self, request, view):
        telephone = request.query_params.get('telephone')

        return self.cache_format%{'scope':'sms','ident':telephone} # 以手机号返回不太好,换一种不易重名的返回做限制

在settings里配置一下频率

REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_RATES': {
        'sms': '1/m'  # 一分钟访问1次
    }
}

views.py

from . import throttlings
class SendView(ViewSet):
    # 发送短信频率限制
    throttle_classes = [throttlings.TimeThrottling]
    # 发送验证码接口
    @action(methods=['GET'], detail=False)
    def send(self,request,*args,**kwargs):
        telephone = request.query_params.get('telephone')
        if not re.match('^1[3-9][0-9]{9}$',telephone):
            return APIResponse(code=0,msg='手机号不合法')
        code = get_code() #获取随机验证码
        result = send_message(telephone,code)
        # 将验证码保存到缓存中备用,参数分别是key,value,过期时间秒
        # 这个telephone可以有标识一点,在settings进行配置一下
        cache.set(settings.CACHE_MSG % telephone,code,180)
        # 如果result返回true
        if result:
            return APIResponse(msg='验证码发送成功')
        else:
            return APIResponse(code=0,msg='验证码发送失败')

手机号登陆接口

serializes.py
作用:对前端发来的手机号和验证码进行校验,并签发token

class UserCodeModelserialize(serializers.ModelSerializer):
    code = serializers.CharField(max_length=4,min_length=4)
    class Meta:
        model = models.UserInfo
        fields = ['telephone','code']

    # 这里因为手机号验证码方式登录也需要校验和签发token,所以也需要重写validate方法
    def validate(self, attrs):
        user = self._get_user(attrs)
        token = self._get_token(user)
        self.context['user'] = user
        self.context['token'] = token
        return attrs

    def _get_user(self, attrs):
        # 获取前端发送的数据
        telephone = attrs.get('telephone')
        code = attrs.get('code')
        # 取出生产的code与用户传的code做比较
        cache_code = cache.get(settings.CACHE_MSG%telephone)
        if cache_code == code:
            user = models.UserInfo.objects.filter(telephone=telephone).first()
            if user:
                cache.set(settings.CACHE_MSG%telephone,'')
                return user
            else:
                raise ValidationError('用户不存在')
        else:
            raise ValidationError('验证码不正确')


    # 签发token函数,前面加一个_暗示内部使用的
    def _get_token(self,user):
        from rest_framework_jwt.serializers import jwt_payload_handler,jwt_encode_handler
        pyload = jwt_payload_handler(user) #通过user对象获取pyload
        token = jwt_encode_handler(pyload) #通过pyload获取token
        return token

views.py

    # 验证码方式登录接口
    @action(methods=['POST'], detail=False)
    def code_login(self, request, *args, **kwargs):
        # 把前端传入的用户登录数据传入序列化器
        ser = serializers.UserCodeModelserialize(data=request.data)
        # 判读传入的数据是否合法
        if ser.is_valid():
            # 合法获取token和用户名
            token = ser.context['token']
            username = ser.context['user'].username
            # 然后返回给前端
            return APIResponse(token=token, username=username)
        else:
            return APIResponse(code='0', msg=ser.errors)

标签:code,登录,get,telephone,接口,验证码,token,user,搭建
From: https://www.cnblogs.com/suncolor/p/16853710.html

相关文章

  • linux下搭建oh-my-zsh环境
    目标:因为用习惯了zsh的shell环境,所以习惯在服务器上也搭建zsh环境,但是每次搭建都需要Google每一步骤,感觉很麻烦,所以决定记录一下,免得一次次查1.安装zshzsh是一款shell环......
  • 如何登录微软网站,以使用Microsoft WhiteBoard?
    Windows10登录微软账户为什么一直显示请稍等步骤:修改\(DNS:4.2.2.2\)和\(4.2.2.1\)是微软的免费\(dns\)服务器地址*关闭\(IPV6:\)......
  • 用Docker搭建Python环境
    步骤1创建项目目录$cd/PATH/TO$mkdirpython-demo2下载python镜像1#下载镜像2dockerpullpython:3.834#查看镜像5dockerimages3创建pytho......
  • 企业项目开发流程,路飞项目,虚拟环境搭建
    目录企业项目开发流程一、企业项目类型面向互联网用户:商城类项目面向互联网用户:二手交易类的公司内部项目:python写的重点个人博客内容收费网站房屋租赁二、完整流程三、我......
  • notepad++搭建gtk3.0/2.0环境_F_hawk189_新浪博客
    准备下载以下内容​​notepad++​​​​mingw​​(包含msys和gcc工具链)​​gtk+bundle​​(2.x或3.x都可以,不过现在官网都是使用msys下载)安装......
  • 使用docker搭建一个WordPress网站
     【整体说明】网站需要三个容器:WordPress、MariaDB、Nginx,他们的关系如下图。这是一个典型的网站,mariadb作为后方的关系型数据库,端口号是3306;wordpress是中间的应用服务......
  • Django_获取api接口的传参
    当参数为form-data或者x-www-form-urlencoded类型时,使用request.POST获取到参数获取参数方式request.POST.get('username')当参数为raw类型时,使用request.body获取......
  • ZYNQ接口分析
    PS整体互连框图(1)S_AXI_HPC[0:1]_FPD和S_AXI_HP[0:3]_FPD:可以被PL端AXI主口访问的高性能AXI从口(2)M_AXI_HPM0/1_FPD:低延迟的可以访问PL端AXI从口的AXI主口(3)S_AXI_ACE_FPD:可......
  • Centos8.0上生产环境搭建
    一、基础环境搭建2.1配置yum1、添加yum源#一定注意版本#curl下载阿里镜像源curl-o/etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-7......
  • mysql 本地navicat设置远程登录
    1usemysql;2selecthost,user,authentication_string,pluginfromuser;3updateusersethost='%'whereuser='root';123三个语句顺序执行完毕后重新启......