Django rstful登陆认证并检查session是否过期代码实例
作者:Leslie-x 时间:2022-11-04 16:17:15
这篇文章主要介绍了Django rstful登陆认证并检查session是否过期代码实例,下面我们可以来一起学习一下。
一:restful用户视图
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
# Author:Leslie-x
from users import models
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework import viewsets
from rest_framework import serializers
from django.contrib.auth import authenticate, login, logout
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = models.User
exclude = ('password',)
class UserViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = UserSerializer
queryset = User.objects.all()
authentication_classes = (UserAuthentication,)
@action(detail=False, methods=['post'])
def register(self, request, *args, **kwargs):
username = request.data.get("username")
queryset = User.objects.filter(username=username)
if queryset.exists():
raise exceptions.PermissionDenied('该账号已经被注册')
user = User.objects.create_user(**request.data)
UserProfile.objects.create(user=user, nickname=user.username)
data = self.get_serializer(user).data
return Response(data)
@action(detail=False, methods=['post'])
def login(self, request, *args, **kwargs):
username = request.data.get("username")
password = request.data.get("password")
user = authenticate(username=username, password=password)
if not user:
raise exceptions.PermissionDenied('用户名或密码错误')
auth_id = request.session.get('_auth_user_id')
if auth_id != str(user.pk):
logout(request)
login(request, user)
data = self.get_serializer(user).data
data['session_key'] = request.session.session_key
return Response(data)
@action(detail=False, methods=['post'])
def logout(self, request, *args, **kwargs):
logout(request)
return Response()
二:检查session是否过期
from rest_framework.authentication import SessionAuthentication
from rest_framework.request import Request
from django.contrib.sessions.models import Session
from rest_framework import exceptions
import arrow
class CustomAuth(SessionAuthentication):
def check_session(self, request):
session_key = request.session.session_key
queryset = Session.objects.filter(session_key=session_key)
if not queryset.exists():
raise exceptions.PermissionDenied('非法用户,拒绝访问')
expire_date = queryset.first().expire_date
now = arrow.now().format('YYYY-MM-DD HH:mm:ss')
if not arrow.get(now) < arrow.get(expire_date):
raise exceptions.PermissionDenied('session expired')
def authenticate(self, request: Request):
ret = super().authenticate(request)
self.check_session(request)
return ret
来源:https://www.cnblogs.com/li1992/p/10388501.html
标签:django,rstful,登陆,认证,检查,session,过期
0
投稿
猜你喜欢
怎样取得局域网中所有SQL Server的实例
2009-01-08 13:20:00
python中numpy数组与list相互转换实例方法
2023-11-29 01:48:50
了解WEB页面工具语言XML(六)展望
2008-09-05 17:19:00
Oracle 处理json数据的方法
2024-01-16 15:11:15
Python矩阵常见运算操作实例总结
2021-05-29 18:26:38
js选项卡的实现方法
2024-04-22 22:23:51
Gradio机器学习模型快速部署工具quickstart前篇
2023-07-01 15:07:51
Sqlserver 自定义函数 Function使用介绍
2024-01-16 09:11:36
Python实现将Excel转换成xml的方法示例
2023-01-19 23:02:53
JavaScript实现前端倒计时效果
2024-06-05 09:34:10
GitHub Eclipse配置使用教程详解
2023-11-04 10:23:02
详解Python之unittest单元测试代码
2022-04-15 02:28:12
一文带你搞懂Python中的文件操作
2023-07-20 14:00:52
jmeter正则表达式的使用
2022-08-10 01:10:42
mysql prompt的用法详解
2024-01-28 07:30:32
TensorFlow实现RNN循环神经网络
2023-03-18 18:12:49
Python 序列的方法总结
2021-12-12 11:14:23
pandas 实现 in 和 not in 的用法及使用心得
2021-10-23 12:25:07
python3 字符串知识点学习笔记
2023-05-02 16:25:35
python抢购软件/插件/脚本附完整源码
2021-04-25 22:03:17