Django 2.0密码验证

时间:2018-01-01 06:53:44

标签: python regex django

默认情况下,我们在Django 2.0中使用

获取 AUTH_PASSWORD_VALIDATORS 选项
  • UserAttributeSimilarityValidator
  • MinimumLengthValidator
  • CommonPasswordValidator
  • NumericPasswordValidator

那么有没有简单的方法来添加额外的VALIDATORS,如最小1个大写,1个符号,1个数字等?

在python中,我可以查看使用正则表达式

import re
userPass = 'HelloWorld*123'
if re.search('[A-Z]', userPass)!=None and re.search('[0-9]', userPass)!=None and re.search('[^A-Za-z0-9]', userPass)!=None:
    print 'Strong Password'

2 个答案:

答案 0 :(得分:0)

在项目目录中创建名为passwordValidators.py的新文件,并将其存储在代码中。

from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
import re

class ComplexPasswordValidator:
    """
    Validate whether the password contains minimum one uppercase, one digit and one symbol.
    """
    def validate(self, password, user=None):
        if re.search('[A-Z]', password)==None and re.search('[0-9]', password)==None and re.search('[^A-Za-z0-9]', password)==None:
            raise ValidationError(
                _("This password is not strong."),
                code='password_is_weak',
            )

    def get_help_text(self):
        return _("Your password must contain at least 1 number, 1 uppercase and 1 non-alphanumeric character.")

settings.py

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',
    },
    {
    'NAME': 'dbTest.passwordValidators.ComplexPasswordValidator',
    },
]

答案 1 :(得分:0)

您在这里:

class HasLowerCaseValidator:
    def __init__(self):
        self.message = "The password must contain at least one lowercase character."

    def validate(self, password, user=None):
        if re.search('[a-z]', password) is None:
            raise ValidationError(self.message, code='missing_lower_case')

    def get_help_text(self):
        return self.message


class HasUpperCaseValidator:
    def __init__(self):
        self.message = "The password must contain at least one uppercase character."

    def validate(self, password, user=None):
        if re.search('[A-Z]', password) is None:
            raise ValidationError(self.message, code='missing_upper_case')

    def get_help_text(self):
        return self.message


class HasNumberValidator:
    def __init__(self):
        self.message = "The password must contain at least one numeric character."

    def validate(self, password, user=None):
        if re.search('[0-9]', password) is None:
            raise ValidationError(self.message, code='missing_numeric')

    def get_help_text(self):
        return self.message


class HasSymbolValidator:
    def __init__(self):
        self.message = "The password must contain at least one non-alphanumeric character (symbol)."

    def validate(self, password, user=None):
        if re.search('[^A-Za-z0-9]', password) is None:
            raise ValidationError(self.message, code='missing_symbol')

    def get_help_text(self):
        return self.message