【发布时间】:2021-04-04 12:56:33
【问题描述】:
我编写了一个非常基本的自定义身份验证类,以便为我的自定义身份验证需求实现简单的 JWT 库。
我 generate tokens manually 然后将访问令牌发送到我的 API。默认情况下,这就足够了,但由于我不使用默认的 Django 用户模型,我得到“找不到用户”。这是因为我需要实现一个自定义的身份验证后端。
我需要读取该令牌以使用给定的用户 ID 查询数据库并检查该令牌是否也有效。在我的示例中,我有固定的数字 2。
class ExampleAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
try:
user = vAuthUser.objects.get(pk=2) #this should receive the user_id from the token
except:
raise AuthenticationFailed('No such user')
return (user, None)
我的 API 如下所示:
class MyAPI(APIView):
authentication_classes = (ExampleAuthentication,)
permission_classes = (IsAuthenticated ,)
def get()...
【问题讨论】:
-
那么您的问题到底是什么?不清楚你需要什么
-
使用带有自定义身份验证的简单 JWT 无法正常工作。我生成了发送它的令牌,但是如何在自定义身份验证类中读取它以正确验证用户身份?
标签: python django django-rest-framework django-rest-framework-jwt