【问题标题】:Django enable password hasher for custom authenticationDjango 为自定义身份验证启用密码哈希
【发布时间】:2016-05-09 07:21:41
【问题描述】:

我在 django 项目中使用自定义身份验证后端。 我使用 django shell 创建了一个用户。现在,当我输入密码并尝试使用authenticate 方法对其进行身份验证时,它返回无。

我发现它与密码哈希有关。

数据库中存储的密码如pbkdf2_sha256$24000$c0t.....

我想知道如何在 django 中使用散列?

Settings.py:

AUTHENTICATION_BACKENDS = ('login_app.backends.LoginBackend',
                        'django.contrib.auth.backends.ModelBackend')

# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

自定义后端:

def authenticate(self, email=None, password=None):
    try:
        user = User.objects.get(email=email)
        if password == user.password:
            return user
        else:
            return None
    except User.DoesNotExist:
        return None

编辑:如果我在 postgres 中手动将密码编辑为纯文本,它可以工作。如何使用哈希密码进行身份验证。

【问题讨论】:

  • 为什么要关闭?至少给个理由..

标签: python django authentication


【解决方案1】:

您可以使用 django check_password(raw_password) 提供的名为 check_password 的函数 如果给定的原始字符串是用户的正确密码,则返回 True。 (这会在进行比较时处理密码散列)

【讨论】:

    猜你喜欢
    • 2011-01-25
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 2019-10-22
    • 1970-01-01
    • 2015-01-15
    相关资源
    最近更新 更多