简体   繁体   English

Django Rest 框架,JWT 认证

[英]Django Rest-framework, JWT authentication

I'm newbie in Django Restframework.我是 Django Restframework 的新手。 I use JWT to make login, register API, everythings worked well, I want to GET a user information with authenticated (tokens).我使用 JWT 进行登录、注册 API,一切正常,我想通过身份验证(令牌)获取用户信息。 This is my code for UserViewSet这是我的 UserViewSet 代码

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer 
    authentication_classes = [IsAuthenticated,]

I've tested on Postman but i received: "'IsAuthenticated' object has no attributes 'authenticate'"我已经在 Postman 上测试过,但我收到:“‘IsAuthenticated’对象没有‘authenticate’属性”

REST_FRAMEWORK = {
    'NONE_FIELD_ERRORS_KEY':'error',
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ),
}

Could you please to help me to solve this promblem?你能帮我解决这个问题吗? Thank you very much.非常感谢。

IsAuthenticated is not anauthentication class . IsAuthenticated不是身份验证类 It's a permission class .这是一个权限类

You would put it in permission_classes to allow any authenticated user access to that view set, while authentication (the mechanism of figuring out who the user for that request is) would be handled by that default JWT authentication:您可以将它放在permission_classes以允许任何经过身份验证的用户访问该视图集,而身份验证(确定该请求的用户是谁的机制)将由默认的 JWT 身份验证处理:

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer 
    permission_classes = [IsAuthenticated,]

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

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