Laravel6接入谷歌验证器
发布于:2022-03-15 09:16:30
次阅读
话不多说,直接上代码:
### 安装谷歌验证器相关依赖composer require "earnp/laravel-google-authenticator:dev-master"### 安装二维码生成器composer require simplesoftwareio/simple-qrcode 1.3.*
在 config/app.php 中注册服务提供者同时注册下相应门面
'providers' => [ //........ Earnp\GoogleAuthenticator\GoogleAuthenticatorServiceprovider::class, SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class,],'aliases' => [ //.......... 'Google' => Earnp\GoogleAuthenticator\Facades\GoogleAuthenticator::class, 'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class],
谷歌验证码绑定页面
public function googleAuth(){ // 创建谷歌验证码 $createSecret = GoogleAuthenticator::CreateSecret(); $createSecret['qrcode'] = QrCode::encoding('UTF-8')->size(180)->margin(1)->generate($createSecret['codeurl']); $admin = Admin::where('is_use', Admin::IS_USE)->get(); return [$createSecret, $admin];}这里的值分配到模板上之后,提交的时候都得带到doGoogleAuth,完成绑定
绑定动作
public function doGoogleAuth($request){ $adminInfo = Admin::find($request->uid); if (!$adminInfo) return $this->returnData(0, '绑定账号不存在'); if (empty($request->onecode) && strlen($request->onecode) != 6) return $this->returnData(0, '请正确输入手机上google验证码 '); $google = $request->google; if (GoogleAuthenticator::CheckCode($google, $request->onecode)) { // 绑定场景:绑定成功,向数据库插入google参数,跳转到登录界面让用户登录 $adminInfo->google_code = $google; $adminInfo->save(); // 登录认证场景:认证成功,执行认证操作 return $this->returnData(0, '绑定成功'); } else { return $this->returnData(0, '绑定失败'); }}
登录动作
除了正常的字段外,增加一个Google验证码校验,方式同doGoogleAuth里的checkCode
完成