gpt4 book ai didi

python - Django 2.0 密码验证

转载 作者:行者123 更新时间:2023-12-01 22:10:48 25 4
gpt4 key购买 nike

在 Django 2.0 中,我们默认使用 AUTH_PASSWORD_VALIDATORS 选项

  • 用户属性相似度验证器
  • 最小长度验证器
  • 通用密码验证器
  • 数字密码验证器

那么有什么简单的方法可以添加额外的验证器,例如至少 1 个大写字母、1 个符号、1 个数字等?

在 python 中我可以检查 Using regex

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'

最佳答案

给你:

class _ValidatorBase:
__slots__ = ('message',)
DEFAULT_MSG = ''

def __init__(self, message=None):
self.message = message if message else self.DEFAULT_MSG

def get_help_text(self):
return self.message

def validate(self, *args, **kwargs):
raise NotImplementedError()


class HasLowerCaseValidator(_ValidatorBase):
__slots__ = ()
DEFAULT_MSG = "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')


class HasUpperCaseValidator(_ValidatorBase):
__slots__ = ()
DEFAULT_MSG = "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')


class HasNumberValidator(_ValidatorBase):
__slots__ = ()
DEFAULT_MSG = "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')


class HasSymbolValidator(_ValidatorBase):
__slots__ = ()
DEFAULT_MSG = "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')

关于python - Django 2.0 密码验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48047731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com