Django REST的JSON Web令牌不会通过用户数据库进行身份验证 [英] JSON Web Token for Django REST won't authenticate to user database

查看:172
本文介绍了Django REST的JSON Web令牌不会通过用户数据库进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作正常的Django REST API后端.我以前使用的是会话身份验证,但想转向基于令牌的令牌,以便在多个服务器之间进行扩展.我已经研究了几天,但没有找到解决我问题的答案.我将djangorestframework-jwt软件包添加到了我的应用程序中,但是当我尝试进行身份验证时总是返回:

I have a working Django REST API backend. I was previously using session authentication, but would like to move to token based for scaling across multiple servers. I have been researching this for a couple days now and I have not found an answer to my problem. I added the djangorestframework-jwt package to my application but when I try to authenticate is always returns:

{"non_field_errors":["Unable to login with provided credentials."]}

我在jwt包中看到了此错误所在,并且可以在身份验证过程中遵循代码.在身份验证过程中没有看到任何错误.当我尝试使用这些凭据创建用户时,它说该用户已经存在,因此我知道它正在命中正确的用户表.我不确定为什么yield_jwt_token端点将不对我的凭据进行身份验证.以下是我的django应用程序的相关部分.任何帮助将不胜感激.如果我遗漏了任何可能有助于解决此问题的方法,请告诉我,我将上传它.谢谢,

I see in the jwt package where this error is, and can follow the code back through the authentication process. I do not see any errors in the auth process. When I try to create a user with those credentials it says that a user already exists, so I know it is hitting the correct user table. I am not sure why the obtain_jwt_token endpoint will not authenticate my credentials. Below are relevant sections of my django app. Any help would be greatly appreciated. If I am leaving anything out that could help figure this out please let me know and I will upload it. Thanks,

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
    'rest_framework.permissions.IsAuthenticated',
    ),
'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    ),
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 100,}

app/urls.py

urlpatterns = patterns('',
# Api
url(r'^api/', include(router.urls)),
url(r'^api/stats', statsviews.StatsView.as_view()),
url(r'^api/testing', statsviews.TestView.as_view()),
url(r'^api/login', 'rest_framework_jwt.views.obtain_jwt_token'),
url(r'^api/logout', logout, {'next_page': '/api/login'}),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
)

curl命令

curl -d "email=test@myemail.com&password=test123" http://webhost.mywebsite.com:8080/api/login/

推荐答案

我的设置与您非常相似.一个简单的应用程序,利用香草DRF JWT身份验证.我唯一可以看出的区别是我的INSTALLED_APPS列表中包含了rest_framework_jwt:

I have a very similar setup to you. A simple app, utilizing vanilla DRF JWT authentication. The only difference that I can tell is that I have rest_framework_jwt included in my INSTALLED_APPS list:

INSTALLED_APPS = (
    ...
    # Third Party Dependencies
    'rest_framework',
    'rest_framework_jwt',
    'corsheaders',
    ....

尝试添加它,看看它能带给您什么.

Try adding that and see where it gets you.

这篇关于Django REST的JSON Web令牌不会通过用户数据库进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆