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 '认证失败';
}
相关推荐
apcipot_rain3 小时前
C++转Java日记(06)
java·开发语言
SomeOtherTime10 小时前
Rust所有权(AI回答)
开发语言·后端·rust
Easyhood10 小时前
Android 开发入门实践(十四):Compose 副作用入门与四个常用 API
android·开发语言·kotlin·android jetpack·compose
白杨尚青11 小时前
C++进阶篇(六):map 和 set 的模拟实现——用红黑树封装 mymap/myset(迭代器 + operator[])
开发语言·c++·迭代器·stl·set·map·红黑树
饮水桶_T14 小时前
JAVA IO流笔记
开发语言·数据结构·算法
Felven16 小时前
C. Brr Brrr Patapim
c语言·开发语言
路溪非溪18 小时前
python标准库使用总结
开发语言·python
SomeOtherTime19 小时前
Rust 面向对象介绍(AI回答)
开发语言·python·rust
zh_xuan20 小时前
kotlin常见面试题
开发语言·kotlin·面试题
xbzb21 小时前
从零容器化部署 ECShop:Harbor 私有仓库 + 自定义 PHP 镜像 + LNMP 三容器
容器·php·harbor·镜像·仓库·ecshop