登录的token操作
class User(models.Model):
user = models.CharField(max_length=32)
pwd = models.CharField(max_length=32)
class UserToken(models.Model):
token = models.CharField(max_length=64)
user = models.OneToOneField(to='User')
from rest_framework import serializers
from app import models
class UserJson(serializers.ModelSerializer):
class Meta:
model = models.User
fields = '__all__'
from rest_framework.views import APIView
from rest_framework.response import Response
from app import common, models, objson
class Login(APIView):
def post(self, request):
data_dic = request.data
user = models.User.objects.filter(**data_dic).first()
if user:
token = common.get_token()
models.UserToken.objects.update_or_create(user=user, defaults={"token": token})
user_data = objson.UserJson(user).data
return Response({
"status": 0,
"msg": 'login success',
"token": token,
"results": user_data
})
return Response({
"status": 1,
"msg": 'login failed'
})
认证方法的实现
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
class LoginAuthenticate(BaseAuthentication):
def authenticate(self, request):
token = request.META.get('HTTP_TOKEN')
result = models.UserToken.objects.filter(token=token).first()
if result:
return result.user, token
else:
raise AuthenticationFailed("认证失败")
class Books(APIView):
authentication_classes = [LoginAuthenticate]
def get(self, request):
print(request.user)
return Response({
"status": 0,
"msg": 'ok',
"results": []
})
局部认证
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from app import models
class LoginAuthenticate(BaseAuthentication):
def authenticate(self, request):
token = request.META.get('HTTP_TOKEN')
result = models.UserToken.objects.filter(token=token).first()
if result:
return result.user, token
else:
raise AuthenticationFailed("认证失败")
from app import auth
class Books(APIView):
authentication_classes = [auth.LoginAuthenticate]
def get(self, request):
print(request.user)
return Response({
"status": 0,
"msg": 'ok',
"results": []
})
class Home(APIView):
authentication_classes = [auth.LoginAuthenticate]
def get(self, request):
return Response({
"status": 0,
"msg": 'ok',
"results": objson.UserJson(request.user).data
})
全局认证
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
'app.auth.LoginAuthenticate'
),
}
注销:在全局认证情况下
class Logout(APIView):
def get(self, request):
models.UserToken.objects.update_or_create(user=request.user, defaults={'token': common.get_token()})
return Response({
"status": 0,
"msg": 'logout success',
})
补充:前台操作cookie
<body>
<button class="login">登录请求</button>
<button class="token">获取token</button>
</body>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script>
$('.login').click(function () {
token = '88888';
$.cookie('qt', '前台操作cookie');
$.cookie('token', token);
});
$('.token').click(function () {
alert($.cookie('token'))
})
</script>