PHP基于Google Authenticator双因素身份验证实现动态码验证

一:介绍

Google Authenticator双因素身份验证是谷歌推出的一款动态口令工具,解决大家各平台账户遭到恶意攻击的问题,一般在相关的服务平台登陆中除了用正常用户名和密码外,需要再输入一次谷歌认证器生成的动态口令才能验证成功,相当于输入二次密码,以达到账户的高安全性。

二:拓展安装

复制代码
composer require huaweichenai/google-authenticator

三:使用

1:创建密钥

复制代码
$authenticator = new Authenticator();
$secret = $authenticator->createSecret();

2:获取手机端扫描二维码链接

复制代码
$authenticator = new Authenticator();
$qrCodeUrl  = $authenticator->getQRCodeGoogleUrl('username', $secret, 'title');

3:获取手机端扫描二维码的信息

复制代码
$authenticator = new Authenticator();
$authCode  = $authenticator->getAuthCode('username', $secret, 'title');
$authCode = urldecode($authCode);//使用此信息生成二维码

4:动态口令认证

复制代码
$authenticator = new Authenticator();
$verifyCode  = $authenticator->verifyCode($secret, 'code验证码');
if ($verifyCode) {
  echo '认证成功';
} else {
  echo '认证失败';
}
相关推荐
饮水桶_T23 分钟前
HashMap笔记
开发语言·数据结构·笔记
布莱克6051 小时前
Swift 下标(Subscript)详解:从语法到实战
服务器·开发语言·ios·ssh·swift·下标
FungLeo2 小时前
FengCMS · PHP+MySQL 升级实录 · 输出转义的度:面包屑双重编码、默认转义与 cms_html()信任声明
mysql·html·php·fengcms·输出转义
鲸渔2 小时前
【AI Agent系列课程23-Python 基础23】测试入门:为函数写第一个 pytest 用例
开发语言·python·pytest·ai编程
星恒随风2 小时前
从0实现C++ AI大模型接入SDK(三):环境搭建与ChatSDK快速上手——实现我们的第一个AI聊天程序
linux·开发语言·c++·人工智能·笔记
风落青芜2 小时前
排序算法(二):选择排序、堆排序与冒泡排序
c语言·开发语言·数据结构·算法·排序算法
fengkai45453 小时前
Python运维实战
运维·开发语言·python·ssh·sftp·paramiko
FungLeo4 小时前
FengCMS ·PHP+MySQL升级实录 · 富文本清洗配置实战:白名单、URI scheme、禁 iframe/id、实例缓存
mysql·缓存·php·xss·fengcms
苏琢玉4 小时前
从 dsh-im 到微信:我做了个 PHP 包,让微信变成 AI 的消息入口
微信·php·composer
ShineWinsu5 小时前
对于Qt:重写事件步骤的解析
开发语言·c++·qt·客户端·事件·鼠标事件·步骤