注册Django-allauth和密码强度

时间:2016-03-03 21:51:59

标签: django django-allauth

django-allauth有办法在注册时负责密码强度检查吗?

我看到可以在ACCOUNT_PASSWORD_MIN_LENGTH中设置配置settings.py的密码的大小,但我也想查看其他常见内容:

  • 至少有一个大写字母
  • 至少有一位数字
  • 至少有一个特殊字符

有一种方法可以用django-allauth做到这一点吗?

提前致谢。

2 个答案:

答案 0 :(得分:3)

您可以通过覆盖默认适配器(通过ACCOUNT_ADAPTER)来完成此操作,如下所示:

from allauth.account.adapter import DefaultAccountAdapter

class MyAccountAdapter(DefaultAccountAdapter):

    def clean_password(self, password):
        # Insert your rules here

请注意,Django最近添加了对自定义密码验证程序的支持。这个机制也将在allauth中得到支持,并密切关注问题https://github.com/pennersr/django-allauth/issues/1233

答案 1 :(得分:0)

谢谢!这是我的解决方案,希望对此有所帮助!

# project/settings.py:
ACCOUNT_ADAPTER = 'user_profile.adapters.MyAccountAdapter'

# project/user_profile/adapter.py:
from allauth.account.adapter import DefaultAccountAdapter

class MyAccountAdapter(DefaultAccountAdapter):
    def clean_password(self, password):
        if re.match(r'^(?=.*?\d)(?=.*?[A-Z])(?=.*?[a-z])[A-Za-z\d]{8,}$', password):
            return password
        else:
            raise ValidationError("Error message")
相关问题