【问题标题】:Authenticating with JWT token in Django在 Django 中使用 JWT 令牌进行身份验证
【发布时间】:2017-02-04 01:41:50
【问题描述】:

我是 Django 的初学者,我正在从这里学习 JWT 令牌。

https://jpadilla.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/

当我从网络访问时,它总是这样说。本地主机对我来说没问题。

{"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)

【问题讨论】:

  • 你做对了,请再次检查你在 curl 中输入或编码。
  • 我还是卡住了。我应该如何排除故障?

标签: python django django-rest-framework jwt


【解决方案1】:

我现在知道了。就我而言,我不仅在网络上还可以。我从这个链接检查。

Django Rest Framework - Authentication credentials were not provided

它说我需要添加

WSGIPassAuthorization On

在我的 apache 目录中的 httpd.conf 中。现在好了。

【讨论】:

  • 你在哪里添加了 WSGIPassAuthorization On?
猜你喜欢
  • 2018-12-15
  • 1970-01-01
  • 2016-12-17
  • 2018-08-29
  • 1970-01-01
  • 2016-12-16
  • 2019-02-27
  • 2020-03-25
  • 2018-01-20
相关资源
最近更新 更多