当前位置: 首页 > news >正文

孝感网站开发找优搏google浏览器入口

孝感网站开发找优搏,google浏览器入口,工信部怎么查网站备案,网站菜单怎么做介绍 创建系统管理app#xff0c;用于管理系统的用户#xff0c;角色#xff0c;权限#xff0c;登录等功能#xff0c;项目中将使用django-rest_framework进行用户认证和权限解析。这里将完成用户认证 用户验证 rest_framework.authentication模块中的认证类#xff…介绍 创建系统管理app用于管理系统的用户角色权限登录等功能项目中将使用django-rest_framework进行用户认证和权限解析。这里将完成用户认证 用户验证 rest_framework.authentication模块中的认证类例如 session认证SessionAuthentication基于session token 认证TokenAuthenticationAPI Token 自定义认证BasicAuthenticationHTTP Basic Auth 这里采用djangorestframework-simplejwt设计自定义认证类和自定义登录 pip install djangorestframework-simplejwt创建用户模型表user from django.db import models from django.contrib.auth.hashers import make_password, check_password# Create your models here.class User(models.Model):username models.CharField(max_length255, uniqueTrue, verbose_name手机号)password models.CharField(max_length255, uniqueFalse, verbose_name密码)is_vip models.BooleanField(defaultFalse,verbose_name是否为vip)vip_expires_at models.DateTimeField(auto_now_addTrue,verbose_namevip过期时间)is_active models.BooleanField(defaultTrue)class Meta:db_table userverbose_name 用户账号表verbose_name_plural verbose_name数据库迁移 python manage.py makemigrations python manage.py migrate 在根目录下创建utils目录在utils目录中创建文件Authentication.py和jwt.py ## Authentication.pyfrom rest_framework_simplejwt.serializers import TokenObtainPairSerializer from rest_framework_simplejwt.views import TokenObtainPairView from rest_framework import serializers from user.models import User# 自定义认证类 class MyTokenObtainPairSerializer(TokenObtainPairSerializer):自定义登录认证使用自有用户表username、password这两个字段为必传字段因为 DRF 要检查这些字段是否有效username_field phone_number 这是重命名了username必传字段设置为了phone_number字段必传phone_number serializers.CharField(requiredFalse) # 这个是设置了自定义的字段是否必传def validate(self, attrs):username attrs.get(username)password attrs.get(password)if not username or not password:raise serializers.ValidationError(phone_number and password are required)try:user User.objects.get(usernameusername, passwordpassword)except User.DoesNotExist:raise serializers.ValidationError(No user found with this username and password.)print(user)refresh self.get_token(user)data {userId: user.id, token: str(refresh.access_token), refresh: str(refresh),is_vip: user.is_vip}return dataclass MyTokenObtainPairView(TokenObtainPairView):serializer_class MyTokenObtainPairSerializer## jwt.py from rest_framework_simplejwt.authentication import JWTAuthentication from rest_framework_simplejwt.exceptions import InvalidToken, AuthenticationFailed from django.utils.translation import gettext_lazy as _ # 自定义的用户模型 from user.models import Userclass MyJWTAuthentication(JWTAuthentication):修改JWT认证类返回自定义User表对象def get_user(self, validated_token):try:user_id validated_token[user_id]except KeyError:raise InvalidToken(_(Token contained no recognizable user identification))try:user User.objects.get(**{id: user_id})except User.DoesNotExist:raise AuthenticationFailed(_(User not found), codeuser_not_found)return user在settings.py同级目录下创建drf_settings.py 并引入settings.py 中 ## drf_settings.py REST_FRAMEWORK {DEFAULT_PERMISSION_CLASSES: [rest_framework.permissions.IsAuthenticated,],DEFAULT_AUTHENTICATION_CLASSES: [utils.jwt.MyJWTAuthentication # 认证方式为自定义的认证类], }引入settings中 import drf_settings REST_FRAMEWORK drf_settings.REST_FRAMEWORK创建一个app:userauth在views.py中写入一个接口视图 from django.shortcuts import render from rest_framework.views import APIView from rest_framework.response import Response# Create your views here.class UserTestView(APIView):def post(self, request,*args, **kwargs):s str(request.user.__dict__)return Response(s)在app:userauth:urls中设置路由 from django.urls import path, include from rest_framework import routers from rest_framework_simplejwt.views import TokenVerifyView, TokenRefreshView from utils.authentication import MyTokenObtainPairView from userauth.views import UserTestViewrouter routers.DefaultRouter()urlpatterns [path(login/, MyTokenObtainPairView.as_view()), # 登录path(refresh/, TokenRefreshView.as_view(), nametoken_refresh), # 刷新tokenpath(verify/, TokenVerifyView.as_view(), nametoken_verify), # 验证tokenpath(test/, UserTestView.as_view(), nametest) ] urlpatterns.append(path(, include(router.urls)))这里没有写注册功能所以只有手动在数据库中创建一个用户 使用postman进行登录测试 刷新token测试 接口测试 参考链接https://blog.csdn.net/qq_42410176/article/details/130568130?spm1001.2014.3001.5502
http://www.hkea.cn/news/14557586/

相关文章:

  • wordpress 插件 重命名免费seo排名工具
  • 外链生成网站科技设计网站建设
  • 电子商务网站建设与实验给网站做接口
  • 代理网址是什么谷歌seo需要做什么
  • 网站维护和制作怎么做会计分录建设摩托车官网报价表
  • 长沙微信网站律师事务所网站建设策划方案
  • 网站建设之开展电子商务设计网站推荐html
  • 阿里云域名注册服务网站做网站常用的插件
  • 如何用自己的电脑建网站网站做锚点
  • 网站建设维护的相关基础知识常州建设网站代理商
  • 国外html5游戏网站wordpress建众创平台
  • 网站建设的费用怎么做账wordpress 头像不见
  • 5g空间大吗企业网站广州网站制作知名 乐云践新
  • 景区旅游网站平台建设网站式登录页面模板下载
  • 网站建设的作用是什么意思网站是用织梦系统做的首页打开超慢
  • 陕西建设网站电子政务大厅网站建设公司岗位
  • 销售一个产品的网站怎么做的深圳市大鹏建设局网站
  • 网站建设互联网加友情链接发布平台
  • 能免费做片头的网站网站建设找王科杰信誉
  • FPGA毕业设计代做网站永久网站空间
  • 宜飞思工业设计网站宁波网站制作定制
  • 注册网站帐号注销佛山网中互联网服务有限公司
  • 网站开发最强工具工信部企业网站备案吗
  • 龙华网站建设专业公司中国建设银行官网登录入口手机版
  • 模拟网站建设软件有哪些dedecms怎么制作网站
  • 网站制作报价图片欣赏商品网站建设实验记录
  • 动漫php网站模板毕业设计指导网站开发
  • 常德网站优化公司国际网站如何做seo
  • 南阳公司网站建设win10建站wordpress
  • 如何选择佛山网站建设大型网站怎么做优化