20 lines
687 B
Python
20 lines
687 B
Python
from django.shortcuts import render
|
|
|
|
# Create your views here.
|
|
|
|
from rest_framework.views import APIView
|
|
from rest_framework.response import Response
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from authentication.authentication import CustomTokenAuthentication
|
|
|
|
class MyProtectedView(APIView):
|
|
authentication_classes = [CustomTokenAuthentication] # 使用自定义的 Token 认证
|
|
permission_classes = [IsAuthenticated] # 需要用户认证才能访问
|
|
|
|
def get(self, request):
|
|
# 只有认证通过的用户可以访问这个视图
|
|
|
|
print(request.user.id) # 访问当前用户
|
|
return Response({"message": "This is a protected view!"})
|
|
|