简体   繁体   English

Django Rest 框架不接受 JWT 身份验证令牌

[英]Django Rest Framework not accepting JWT Authentication Token

I am using the Django Restful API Framework together with Simple JWT and have successfully created a URL for receiving and refreshing a user token.我正在使用Django Restful API 框架简单的 JWT并成功创建了一个 ZE6B391A8D2C4D45903D2 用于接收令牌和刷新用户令牌。 In order to try out the authentication using the token, I have created a view that simply lists all the posts inside the database.为了尝试使用令牌进行身份验证,我创建了一个视图,它简单地列出了数据库中的所有帖子。 I have then assigned the IsAuthenticated class to the view.然后我将IsAuthenticated class 分配给视图。

As expected, I get an error message saying that the authentication credentials were not provided.正如预期的那样,我收到一条错误消息,指出未提供身份验证凭据。 I then went ahead and made a simple GET request using Postman, with the authentication token provided in the "Authorization" tab.然后我继续使用 Postman 发出一个简单的GET请求,并在“授权”选项卡中提供了身份验证令牌。 The type was set to "Bear Token".类型设置为“Bear Token”。 Unfortunately, I still get the message "Authentication credentials were not provided."不幸的是,我仍然收到“未提供身份验证凭据”消息。 with a 403 Forbidden code.带有 403 禁止代码。

I have also tried to provide the token in the Headers, as well as make CURL requests, everything to no avail.我还尝试在标头中提供令牌,以及发出 CURL 请求,一切都无济于事。

My view looks like this:我的观点是这样的:

class PostListView(generics.ListAPIView):
    permission_classes = (IsAuthenticated,)
    queryset = Post.objects.filter()

This is the serializer:这是序列化程序:

class PostListSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = ('__all__')

The settings.py of the Django project: Django项目的settings.py:

REST_FRAMEWORK = {
    'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'],
    'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.AllowAny'],
    'DEFAULT_AUTHENTICATION_CLASSES:': ('rest_framework_simplejwt.authentication.JWTAuthentication',)
}
CORS_ALLOW_ALL_ORIGINS = True  # For testing purposes

I have followed several different tutorials online, read through numerous posts as well as followed the official documentation of Simple JWT.我已经在线学习了几个不同的教程,阅读了许多帖子,并遵循了 Simple JWT 的官方文档。

Well what you are doing is trying to filter the data while your basic purpose is to just list your model.那么您正在做的是尝试过滤数据,而您的基本目的只是列出您的 model。 For filtering make sure your go through the documentation DRF Filtering .对于过滤,请通过文档DRF Filtering确保您的 go 。

Try these changes in your code.在您的代码中尝试这些更改。 I hope it will work for you.我希望它对你有用。

Settings.py设置.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
}

Views.py视图.py

class UserList(generics.ListAPIView):
    permission_classes = (IsAuthenticated,)
    queryset = Post.objects.all()
    serializer_class = PostListSerializer

After this try to hit your API with access token.在此之后尝试使用访问令牌访问您的 API。 To learn more about generic views you can go through this link Generic Views in DRF .要了解有关通用视图的更多信息,您可以通过此链接DRF 中的通用视图go 。

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

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