- 谷歌身份验证器Google Authenticator是谷歌推出的一款动态口令工具,解决大家各平台账户遭到恶意攻击的问题,一般在相关的服务平台登陆中除了用正常用户名和密码外,需要再输入一次谷歌认证器生成的动态口令才能验证成功,相当于输入二次密码,以达到账户的高安全性。
- 例如交易所、金融平台、以及一些钱包等项目等等,都会使用谷歌身份验证器Google Authenticator来做二次认证,开启谷歌身份验证之后,登录账户,除了输入用户名和密码,还需要输入谷歌验证器上的动态密码。谷歌验证器上的动态密码,也称为一次性密码,密码按照时间或使用次数不断动态变化(默认 30 秒变更一次)
- 代码参考:https://github.com/PHPGangsta/GoogleAuthenticator
- 关键代码:
<?php
error_reporting(0);
session_start();
require_once 'PHPGangsta/GoogleAuthenticator.php';
$ga = new PHPGangsta_GoogleAuthenticator();
$secret = "62H6TMAXQTZBVTRB";
$qrCodeUrl = $ga->getQRCodeGoogleUrl('username', $secret);
echo "二维码地址: ".$qrCodeUrl."\n\n";
$oneCode = $ga->getCode($secret);
echo "本次登录的动态口令:'$oneCode'\n";
$checkResult = $ga->verifyCode($secret, $password,2);
if ($checkResult) {
$_SESSION['username'] = $username;
echo "<h1>登录成功!</h1>";
header("Refresh: 5; url=main.php");
exit;
} else {
echo "<h1>登录失败!</h1>";
header("Refresh: 3; url=login.html");
exit;
}
?>
- 手机端安装 Microsoft Authenticator
- 下载地址:https://www.microsoft.com/en-us/security/mobile-authenticator-app
- 将以上代码生成的二维码地址在浏览器中访问
- 手机端扫描二维码获取动态验证码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>系统运维管理平台</title>
<link rel="stylesheet" type="text/css" href="login.css"/>
</head>
<body>
<div id="login"