首页 文章

在Django中使用JWT令牌进行身份验证

提问于
浏览
2

我是Django的初学者,我从这里学习JWT令牌 .

http://getblimp.github.io/django-rest-framework-jwt/#rest-framework-jwt-auth

我已经在settings.py中设置了 .

REST_FRAMEWORK = {
   'DEFAULT_AUTHENTICATION_CLASSES':
        (
         'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
         ),

    'DEFAULT_MODEL_SERIALIZER_CLASS':
        'rest_framework.serializers.ModelSerializer',

    'DEFAULT_PERMISSION_CLASSES':
    (
    'rest_framework.permissions.IsAuthenticated',
        )
}

如果我卷曲,我实际上会收回我的令牌 .

curl -X POST -d "username=khant&password=khant" http://127.0.0.1:8000/api-token-auth/

但是当我访问我的受保护网址时,

curl -H "Authorization: JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImtoYW50IiwidXNlcl9pZCI6OCwiZW1haWwiOiJraGFudEBnbWFpbC5jb20iLCJleHAiOjE0NzQ5MDQxNTJ9.jaZ3HwsXjx7Bk2ol5UdeE8UUlq4OEGCbnb1T8vDhO_w" http://127.0.0.1:8000/dialogue_dialoguemine/

当我从网络访问时,它总是说这个 . Localhost对我来说没问题 .

{“detail”:“未提供身份验证凭据 . ”}

在我的受保护的URL中,我只是编写简单的api来查询 . 我可以知道如何解决这个问题吗?

class DialogueMineView(generics.ListAPIView):
    permission_classes = (IsAuthenticated,)

    serializer_class = DialogueSerializer
    paginate_by = 2

    def get_queryset(self):

        user = self.request.user
        return Dialogue.objects.filter(owner=user)

1 回答

相关问题