Django密码系统实现过程详解


Posted in Python onJuly 19, 2019

一、Django密码存储和加密方式

#算法+迭代+盐+加密

<algorithm>$<iterations>$<salt>$<hash>

默认加密方式配置

#settings里的默认配置
PASSWORD_HASHERS = [
  'django.contrib.auth.hashers.PBKDF2PasswordHasher',
  'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
  'django.contrib.auth.hashers.Argon2PasswordHasher',
  'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
  'django.contrib.auth.hashers.BCryptPasswordHasher',
]

#PASSWORD_HASHERS[0]为正在使用的加密存储方式,其他为检验密码时,可以使用的方式

默认加密方式配置

所有支持的hasher

[
  'django.contrib.auth.hashers.PBKDF2PasswordHasher',
  'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
  'django.contrib.auth.hashers.Argon2PasswordHasher',
  'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
  'django.contrib.auth.hashers.BCryptPasswordHasher',
  'django.contrib.auth.hashers.SHA1PasswordHasher',
  'django.contrib.auth.hashers.MD5PasswordHasher',
  'django.contrib.auth.hashers.UnsaltedSHA1PasswordHasher',
  'django.contrib.auth.hashers.UnsaltedMD5PasswordHasher',
  'django.contrib.auth.hashers.CryptPasswordHasher',
]

所有支持的hasher

二、手动校验密码

#和数据库的密码进行校验
check_password(password, encoded)

#手动生成加密的密码,如果password=None,则生成的密码永远无法被check_password()
make_password(password, salt=None, hasher='default')

#检查密码是否可被check_password()
is_password_usable(encoded_password)

三、密码格式验证

AUTH_PASSWORD_VALIDATORS = [

#检验和用户信息的相似度
  {
    'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  },

#校验密码最小长度
  {
    'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    'OPTIONS': {
      'min_length': 9,
    }
  },

#校验是否为过于简单(容易猜)密码
  {
    'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  },

#校验是否为纯数字
  {
    'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  },
]

四、自定义

  • 自定义hash算法
  • 对已有hash算法升级
  • 自定义密码格式验证

官方原文

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
树莓派中python获取GY-85九轴模块信息示例
Dec 05 Python
python自动化测试之从命令行运行测试用例with verbosity
Sep 28 Python
Python矩阵常见运算操作实例总结
Sep 29 Python
遗传算法之Python实现代码
Oct 10 Python
python决策树之C4.5算法详解
Dec 20 Python
Python反射的用法实例分析
Feb 11 Python
解决Python运行文件出现out of memory框的问题
Dec 03 Python
python保存字典和读取字典的实例代码
Jul 07 Python
python写入数据到csv或xlsx文件的3种方法
Aug 23 Python
pip已经安装好第三方库但pycharm中import时还是标红的解决方案
Oct 09 Python
使用pandas生成/读取csv文件的方法实例
Jul 09 Python
python函数的两种嵌套方法使用
Apr 02 Python
Tensorflow实现酸奶销量预测分析
Jul 19 #Python
Python实现基于SVM的分类器的方法
Jul 19 #Python
Tensorflow模型实现预测或识别单张图片
Jul 19 #Python
python django下载大的csv文件实现方法分析
Jul 19 #Python
python使用flask与js进行前后台交互的例子
Jul 19 #Python
Django 模型类(models.py)的定义详解
Jul 19 #Python
Tensorflow实现神经网络拟合线性回归
Jul 19 #Python
You might like
PHP处理Json字符串解码返回NULL的解决方法
2014/09/01 PHP
phpstorm编辑器乱码问题解决
2014/12/01 PHP
PHP面向对象程序设计实例分析
2016/01/26 PHP
PHP仿qq空间或朋友圈发布动态、评论动态、回复评论、删除动态或评论的功能(上)
2017/05/26 PHP
详解PHP实现支付宝小程序用户授权的工具类
2018/12/25 PHP
浅谈laravel框架sql中groupBy之后排序的问题
2019/10/17 PHP
window.open()弹出居中的窗口
2007/02/01 Javascript
js模拟实现Array的sort方法
2007/12/11 Javascript
IE6-IE9不支持table.innerHTML的解决方法分享
2012/09/14 Javascript
解决JQeury显示内容没有边距内容紧挨着浏览器边线
2013/12/20 Javascript
倒记时60刷新网页的js代码
2014/02/18 Javascript
详解用vue.js和laravel实现微信支付
2017/06/23 Javascript
webpack4之如何编写loader的方法步骤
2019/06/06 Javascript
了解在JavaScript中将值转换为字符串的5种方法
2019/06/06 Javascript
你不可不知的Vue.js列表渲染详解
2019/10/01 Javascript
python实现从字符串中找出字符1的位置以及个数的方法
2014/08/25 Python
Python中尝试多线程编程的一个简明例子
2015/04/07 Python
Python Socket传输文件示例
2017/01/16 Python
Python扩展内置类型详解
2018/03/26 Python
Python统计单词出现的次数
2018/04/04 Python
Python绘制并保存指定大小图像的方法
2019/01/10 Python
python多线程并发及测试框架案例
2019/10/15 Python
vim自动补全插件YouCompleteMe(YCM)安装过程解析
2019/10/21 Python
python中执行smtplib失败的处理方法
2020/07/01 Python
用python实现学生管理系统
2020/07/24 Python
iHerb台湾:维生素、保健品和健康产品
2018/01/31 全球购物
大学生收银员求职信分享
2014/01/02 职场文书
咖啡书吧创业计划书
2014/01/13 职场文书
公安机关党的群众路线教育实践活动剖析材料
2014/10/10 职场文书
银行安全保卫工作总结
2015/08/10 职场文书
小学生班干部竞选稿
2015/11/20 职场文书
Python绘制分类图的方法
2021/04/20 Python
详解GaussDB for MySQL性能优化
2021/05/18 MySQL
详解Python中的进程和线程
2021/06/23 Python
浅谈 JavaScript 沙箱Sandbox
2021/11/02 Javascript
python运算符之与用户交互
2022/04/13 Python