简体   繁体   English

如何在 django rest 框架中使用 jwt 身份验证获取用户详细信息?

[英]how to get the user details using jwt authentication in django rest framework?

I am trying to get the user details using JWT token authentication.我正在尝试使用 JWT 令牌身份验证获取用户详细信息。 I am able to get them using BasicAuthentication and JsonWebToken authentication.我可以使用 BasicAuthentication 和 JsonWebToken 身份验证来获取它们。 I am trying to get the details using JWT .我正在尝试使用 JWT 获取详细信息。

class PermissionView(APIView):
    permission_classes = [IsAuthenticated]
    authentication_classes = [BasicAuthentication,JSONWebTokenAuthentication]

    def get(self, request,format=None):
        data = {
            'username': request.user.username,
            'password':request.user.password,

        }
        return Response(data)

urls:网址:

url(r'^api-token/$', obtain_jwt_token),

output: 1) generating the token输出:1)生成令牌

2) getting the user details using Basic Authentication and JsonWebTokenauthentication] 2) 使用 Basic Authentication 和 JsonWebTokenauthentication 获取用户详细信息]

I am trying to get the user details or authenticate the user details using JWT token.我正在尝试使用 JWT 令牌获取用户详细信息或验证用户详细信息。 Help would be appreciated?帮助将不胜感激?

Let us revisit JWT in Django flow to understand something:让我们重新审视 Django 流程中的 JWT 以了解一些东西:

  1. user requests a token from url api-token-auth with his username and password.用户使用他的用户名和密码从 url api-token-auth 请求令牌。
  2. jwt authenticates the user and returns a token for you to use afterwards (this is done for us by the JWT framework) jwt 对用户进行身份验证并返回一个令牌供您以后使用(这是由 JWT 框架为我们完成的)
  3. next when you send the token the JWT authentication class will authenticate the token for you and populate the request with user information you want so that you can use them later in your views.接下来,当您发送令牌时,JWT 身份验证类将为您验证令牌并使用您想要的用户信息填充请求,以便您稍后可以在视图中使用它们。

Conclusion: you will find the user who is currently requesting your API in the request object as request.user .结论:您将在request.user对象中找到当前正在请求您的 API 的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM