python 中Django自带检查用户密码强度的验证器
1.AUTH_PASSWORD_VALIDATORS密码验证
用户经常选择错误的密码。为了帮助缓解此问题,Django提供了可插入的密码验证。您可以同时配置多个密码验证器。Django中包含一些验证器,但是编写自己的验证器也很简单。
每个密码验证器必须提供帮助文本,以向用户解释要求,验证给定的密码,如果不符合要求,则返回错误消息,并有选择地接收已设置的密码。验证器还可以具有可选设置,以微调其行为。
验证由AUTH_PASSWORD_VALIDATORS设置控制。该设置的默认值为空列表,这意味着未应用任何验证器。在使用默认startproject 模板创建的新项目中,启用了一组简单的验证器。
缺省情况下,在表单和createsuperuserand changepassword管理命令中使用验证器来重置或更改密码。验证程序未应用于模型级别,例如 User.objects.create_user()和中create_superuser(),因为我们假设开发人员(而非用户)在该级别与Django交互,并且因为模型验证不会在创建模型时自动运行。
2.AUTH_PASSWORD_VALIDATORS自带的方法只有四种


一般将他们配置在settings.py文件中即可,但是验证方式只有这四种可能会达不到用户的需求,可能会再添加新的密码验证方式。
3.额外添加一种密码复杂度的验证方式(要求密码中必须有大写字母、小写字母、数字、特殊字符)
- validators.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | import re from django.conf import settings from django.core.exceptions import ValidationError # Settings PASSWORD_COMPLEXITY = getattr( settings, "PASSWORD_COMPLEXITY", None) class ComplexityValidator(object): message = _("必须更加复杂 (%s)") code = "complexity" def __init__(self, complexities): self.complexities = complexities def __call__(self, value): if self.complexities is None: return uppercase, lowercase, letters = set(), set(), set() digits, special = set(), set() for character in value: if character.isupper(): uppercase.add(character) letters.add(character) elif character.islower(): lowercase.add(character) letters.add(character) elif character.isdigit(): digits.add(character) elif not character.isspace(): special.add(character) words = set(re.findall(r'\b\w+', value, re.UNICODE)) errors = [] if len(uppercase) < self.complexities.get("UPPER", 0): errors.append( _("%(UPPER)s 个及以上不同的大写字母") % self.complexities) if len(lowercase) < self.complexities.get("LOWER", 0): errors.append( _("%(LOWER)s 个及以上不同的小写字母") % self.complexities) if len(letters) < self.complexities.get("LETTERS", 0): errors.append( _("%(LETTERS)s 个及以上不同的大小写字母") % self.complexities) if len(digits) < self.complexities.get("DIGITS", 0): errors.append( _("%(DIGITS)s 个及以上不同的数字") % self.complexities) if len(special) < self.complexities.get("SPECIAL", 0): errors.append( _("%(SPECIAL)s 个及以上的特殊字符") % self.complexities) if len(words) < self.complexities.get("WORDS", 0): errors.append( _("%(WORDS)s 个及以上不同的单词") % self.complexities) if errors: raise ValidationError(self.message % (_(u'必须包含 ') + u', '.join(errors),),code=self.code) complexity = ComplexityValidator(PASSWORD_COMPLEXITY) |
2.auth_password_validators.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | from django.conf import settings from django.utils.translation import ugettext_lazy as _ from . import validators class ComplexityValidator(object): """ Wrapper for validators.ComplexityValidator which is compatible with the Django 1.9+ password validation API """ def __init__(self): self.validator = validators.ComplexityValidator(settings.PASSWORD_COMPLEXITY) def get_help_text(self): return _("您的密码需满足我们的复杂性要求.") def validate(self, value, user=None): return self.validator(value) |
代码中的__init__、validate、get_help_text是新增的密码验证的必要规则
3.在settings.py中配置
引用
1 | from validators import ComplexityValidator |
设置密码复杂度规则
1 2 3 4 5 6 7 8 | PASSWORD_COMPLEXITY = { # You can omit any or all of these for no limit for that particular set "UPPER": 1, # Uppercase "LOWER": 1, # Lowercase "LETTERS": 1, # Either uppercase or lowercase letters "DIGITS": 1, # Digits "SPECIAL": 1, # Not alphanumeric, space or punctuation character "WORDS": 1 # Words (alphanumeric sequences separated by a whitespace or punctuation character) } |
添加验证

4.成果展示

将密码设为qweasdzxc123后,可得:

希望能对大家有所帮助!
Django官网相关介绍