- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 httpie 来测试我的自定义身份验证。
http POST http://127.0.0.1:8000/api-token-auth/ username='username1' password='Password123'
我确实使用AbstractUser
创建了一个自定义身份验证对象。
使用 TokenAuthentication,我按照文档进行操作,并在我的 REST_FRAMEWORK
设置中添加了自定义 TokenAuthentication:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'regis.models.CustomAuthentication',
)
}
并在我安装的应用程序中添加了rest_framework.authtoken
。
我的AUTHENTICATION_BACKEND
如下:
AUTHENTICATION_BACKENDS = [ 'regis.models.CustomAuthentication' ]
这是我的自定义身份验证类:
class CustomAuthentication(authentication.TokenAuthentication):
def authenticate(self, request):
username = request.META.get('X_USERNAME')
print(username)
user_model = get_user_model()
if not username:
return None
try:
user = user_model.objects.get(username=username)
except User.DoesNotExist:
raise exceptions.AuthenticationFailed('No such user')
return (user, None)
url.py:
urlpatterns += [
url(r'^api-token-auth/', views.obtain_auth_token),
]
我非常关注 DRF 文档 ( http://www.django-rest-framework.org/api-guide/authentication/#custom-authentication )。如果需要任何其他信息来解决此问题,请告诉我,我会更新。对我所缺少的任何帮助都会很棒。
补充:出于好奇,如果我有自定义用户,我是否需要创建自定义身份验证系统?
更新:
我刚刚删除了上面的类,并在我的 REST_FRAMEWORK 设置中添加了 rest_framework.authentication.TokenAuthentication
。我仍在使用自定义身份验证来获取我的用户。
它看起来像这样(不打算格式化它。从 VIM 格式化代码太糟糕了):
class CustomAuthentication(object):
def authenticate(self, email=None, password=None):
User = get_user_model()
try:
user = User.objects.get(email=email)
except User.DoesNotExist:
return None
if user.check_password(password):
return user
return None
def get_user(self, user_id):
try:
user_model = get_user_model()
user = user_model.objects.get(pk=user_id)
except User.DoesNotExist:
return None
我使用此 Django 文档来创建该代码:https://docs.djangoproject.com/en/1.10/topics/auth/customizing/
最佳答案
如果您在 DRF 代码中搜索错误字符串,您会发现以下内容(在 authtoken/serializers.py
中:
from django.contrib.auth import authenticate
...
if username and password:
user = authenticate(username=username, password=password)
if user:
# From Django 1.10 onwards the `authenticate` call simply
# returns `None` for is_active=False users.
# (Assuming the default `ModelBackend` authentication backend.)
if not user.is_active:
msg = _('User account is disabled.')
raise serializers.ValidationError(msg, code='authorization')
else:
msg = _('Unable to log in with provided credentials.')
raise serializers.ValidationError(msg, code='authorization')
...
所以看起来,根据您使用的 Django 版本,这些凭据不正确,或者用户不活跃(对于 Django >= 1.10)?
您是否尝试过使用这些凭据手动登录管理员来验证它们?
关于Django(使用TokenAuthentication): "non_field_errors": "Unable to log in with provided credentials?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42429477/
我正在使用 Django Rest Framework 来构建我的 API,而且我对它还很陌生。 我设置了一个 TokenAuthentication 方法,现在我尝试根据此 token 过滤查询集的
我有一个 Django REST 应用程序和由 Django REST framework JWT Auth 提供支持的 token 身份验证 让我制定我的高级目标: 我的目标是为用户生成一个 tok
我正在尝试使用使用 TokenAuthentication 的 DRF 开发 REST API .这将用于 android 应用程序。 我能够对用户进行身份验证并检索其 token 。我现在遇到的问题
我正在尝试使用 token 身份验证创建一个 api。我的问题是我不希望 token 与用户帐户相关联,而是与帐户模型相关联。 例如: class Account(models.Model):
我正在使用 Symfony3、Nginx、PHP7 构建一个 Web 应用程序。为了授权,我创建了一个扩展 AbstractGuardAuthenticator 的 TokenAuthenticato
我正在为移动和 Web 环境构建一个服务器,我希望 session 身份验证与 token 身份验证一起进行。然而,这些似乎相互矛盾。我对两种方案中的登录有不同的看法。 如果我在可浏览 API 中以用
我正在按照 DRF 文档设置 TokenAuthentication,但无法使用可浏览的 API。我相信我已经在 settings.py 中添加了正确的行: REST_FRAMEWORK = { 'D
我正在使用 django 的 rest_framework 库为 django 应用程序构建 API。一切都很顺利,我可以按预期通过 curl 命令访问我的 API。 现在,我想使用 CoreAPI
我正在尝试创建一个 flutter 应用程序,它将使用 webview 显示来自我的 Django 应用程序的经过身份验证的数据。 涉及步骤: Flutter 应用发送身份验证请求 Django 验证
我们使用 rest_framework.authentication.TokenAuthentication 使用访问 token 对 Django REST Framework 中的 API 用户进
我在 Django REST Framework 中使用 TokenAuthentication 来让脚本远程访问我的 API。运行 API 的域位于 TLS 证书之后。 我已经搜索了许多来源,并在来
在使用 Django REST Framework 构建的 API 中可以使用 TokenAuthentication 方法进行身份验证。它的documentation表示身份验证 token 应通过
我有一个django项目,使用django-rest-framework来创建api。 想要使用基于 token 的身份验证系统,因此(放置、发布、删除)的 api 调用只会为授权用户执行。 我安装了
我在一个依赖 Django 用户模型和 DjangoRestFramework 下的 TokenAuthentication 的项目中工作 我被要求获取每个用户的上次登录日期时间,我意识到当我调用身份
我是一名优秀的程序员,十分优秀!