1.说明

为了安全,可以使用Google authenticator作为二次验证的条件,使用起来很简单,只需要使用Google authenticator APP扫描一下二维码即可得到六位数验证码,验证码每30秒变一次

2.代码

模型类GoogleAuth

class GoogleAuth(models.Model):user_id = models.IntegerField(null=False, verbose_name="用户ID")user_name = models.CharField(null=False, max_length=255, verbose_name="用户名")token = models.CharField(null=False, max_length=255, verbose_name="秘钥")...class Meta:db_table = "google_auth"verbose_name = "谷歌验证"verbose_name_plural = verbose_name

生成二维码和验证

import pyotp
import traceback
from qrcode import QRCode, constants
# 生成二维码
def get_google_qrcode(token, username):data = pyotp.totp.TOTP(token).provisioning_uri(username, issuer_name="wallet_webconsole")qr = QRCode(error_correction=constants.ERROR_CORRECT_L, version=1, box_size=6, border=4)try:qr.add_data(data)qr.make(fit=True)img = qr.make_image()# filepath = dirpath + os.sep + secret_key + '.png'# img.save(filepath)  # 保存条形码图片# return filepathreturn imgexcept Exception as e:traceback.print_exc()return None
# 判断验证码是否正确
def verify_google_code(token, code):t = pyotp.TOTP(token)result = t.verify(code)return result if result is True else False

视图函数

def Login(request):if request.method != "POST":return render(request, 'webconsole/login.html')username = request.POST.get("username")password = request.POST.get("password")auth_code = request.POST.get("auth_code")auth_code = int(auth_code) if auth_code and auth_code.isdigit() else Noneuser = auth.authenticate(username=username, password=password)res_data = {"code": 0}if not user:res_data.update({"error": "账号或密码错误"})return JsonResponse(res_data)google_auth = GoogleAuth.objects.filter(user_id=user.id).first()if not google_auth:res_data.update({"error": "还未绑定谷歌身份验证"})return JsonResponse(res_data)if auth_code:if not verify_google_code(google_auth.token, auth_code):res_data.update({"code": 2})res_data.update({"error": "请输入正确的谷歌验证码"})return JsonResponse(res_data)auth.login(request, user)res_data.update({"code": 1})res_data.update({"result": "登录成功"})return JsonResponse(res_data)qrcode = get_google_qrcode(google_auth.token, user.username)if not qrcode:res_data.update({"error": "生成谷歌验证码失败"})return JsonResponse(res_data)# buf = BytesIO()# qrcode.save(buf)# image_stream = buf.getvalue()# return HttpResponse(image_stream, content_type="image/png")  # 返回二进制流out = BytesIO()qrcode.save(out, 'PNG')pic_base64 = base64.b64encode(out.getvalue()).decode('ascii')  # 返回base64res_data.update({"code": 3})res_data.update({"base64": pic_base64})return JsonResponse(res_data)

上面的demo是为了方便直接把很多代码都放在一起了,实际生产环境建议还是把视图函数的业务分开写

【Django】使用谷歌身份验证Google authenticator相关推荐

  1. 使用C++实现谷歌身份验证器(Google Authenticator)

    使用C++实现谷歌身份验证器(Google Authenticator) 本机环境: windows10 x64位运行环境 1.进入网站:http://slproweb.com/products/Wi ...

  2. 【SpringBoot】61、SpringBoot中使用谷歌身份验证器(Google Authenticator)实现二步身份验证

    Google 身份验证器 Google Authenticator 是谷歌推出的基于时间的一次性密码 (Time-based One-time Password,简称 TOTP),只需要在手机上安装该 ...

  3. Google authenticator 谷歌身份验证,实现动态口令

    Google authenticator 谷歌身份验证,实现动态口令 google authenticator php 服务端 使用PHP类 require_once '../PHPGangsta/G ...

  4. 使用谷歌身份验证器(Google Authenticator)保护你的后台

    为何要使用谷歌身份验证器 普通的网站只使用账号.密码.图形验证码进行后台登录.根据我(作为站长)多年的经验来看,这种方式安全性很低,尤其是使用 http 协议,明文的帐号和密码相当于在网络上裸奔.如果 ...

  5. Google Authenticator windows client 谷歌身份验证器 windows 电脑端

    谷歌身份验证器现在有安卓客户端和ios客户端,本人开发了一个windows客户端,基于 .NETFramework v4.7 开发,已在 github 上开源,可以在 github 上直接下载. gi ...

  6. 使用谷歌身份验证器增强SSH安全

    一般大家都是使用账号和密码远程SSH登录管理服务器.但SSH账号和密码很容易泄露,或者经常遭遇暴力破解.咨询过前同事赛赛,他们目前使用了谷歌身份验证器.查看了谷歌身份验证器的github和其它网上文档 ...

  7. 如何为SSH登录建立双因子验证机制(谷歌身份验证器)?

    前言 默认情况下,SSH已经在远程机器之间使用安全的数据通信;但是如果你想为自己的SSH连接添加另外某种安全层,可以添加谷歌身份验证器(Google Authenticator)双因子验证模块,该模块 ...

  8. 二次验证码小程序与谷歌身份验证器不同点是?

    名称1[二次验证码]小程序 名称2 谷歌身份验证器(Google Authenticator) 粗略对比两个产品异同 [二次验证码]小程序 搜索:微信搜索.微信目前65个小程序入口,倒是容易找到它 使 ...

  9. 谷歌身份验证器代码实现

    手机下载谷歌身份验证器,无需联网也可以用. 工具类 public class GoogleAuthenticator{// 生成的key长度( Generate secret key length)p ...

最新文章

  1. Cstring的使用
  2. 个人比较喜欢的JS网页跳转传值
  3. ORACLE纯SQL实现多行合并一行
  4. 找出1000以内的所有完数。
  5. 树莓派开机运行python脚本_【树莓派】开机自启动脚本方法之一(.Desktop文件)...
  6. pads中如何设置等长_如何在SQL Server中设置扩展,监控系统性能
  7. 莫烦python简历_Matplotlib画图教程
  8. Python3并发编程-多线程threading怎么用?
  9. 服务器协议stp,“STP”是“Server Time Protocol”的缩写,意思是“服务器时间协议”...
  10. ssl证书链的验证的其它方式
  11. js面向对象数据属性和访问器属性,定义多个属性及读取属性的特性
  12. [转]abstract 抽象类的概念和使用
  13. 荔枝软件如何测试声音,荔枝如何测自己的声音 荔枝测自己的声音方法
  14. 武汉理工大学计算机考研专业代码,武汉理工大学代码及专业代码
  15. python中oserror_Python:OSError:[Errno 2]没有这样的文件或目录:”
  16. 2021-12-11 【数据结构平时实验】【图】
  17. 为什么amd显卡便宜却买的人少_为什么不推荐人选择AMD?
  18. sendto recvfrom 详解
  19. mysql数据库技术与应用题库_MySQL数据库设计与应用题库免费期末考试2020答案
  20. ELK - X-Pack设置用户密码

热门文章

  1. CentOS 8防火墙配置
  2. 有节操的设计多参数方法
  3. 一个「学渣」从零开始的Web前端自学之路
  4. txt复制数据到excel数据丢失
  5. 专业之路:13条PS高阶技巧
  6. Android自定义组件:一个波浪形的组件
  7. curl下载安装与使用
  8. 为知笔记,Ulysses和Effie哪个更适合记者?
  9. nginx+uwsgi负载均衡部署django项目
  10. 六、redis主从同步