Blogger Information
Blog 10
fans 0
comment 0
visits 6999
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Laravel6接入谷歌验证器
礼物粑粑
Original
896 people have browsed it

话不多说,直接上代码:

  1. ### 安装谷歌验证器相关依赖
  2. composer require "earnp/laravel-google-authenticator:dev-master"
  3. ### 安装二维码生成器
  4. composer require simplesoftwareio/simple-qrcode 1.3.*

在 config/app.php 中注册服务提供者同时注册下相应门面

  1. 'providers' => [
  2. //........
  3. Earnp\GoogleAuthenticator\GoogleAuthenticatorServiceprovider::class,
  4. SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class,
  5. ],
  6. 'aliases' => [
  7. //..........
  8. 'Google' => Earnp\GoogleAuthenticator\Facades\GoogleAuthenticator::class,
  9. 'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class
  10. ],

谷歌验证码绑定页面

  1. public function googleAuth()
  2. {
  3. // 创建谷歌验证码
  4. $createSecret = GoogleAuthenticator::CreateSecret();
  5. $createSecret['qrcode'] = QrCode::encoding('UTF-8')->size(180)->margin(1)->generate($createSecret['codeurl']);
  6. $admin = Admin::where('is_use', Admin::IS_USE)->get();
  7. return [$createSecret, $admin];
  8. }
  9. 这里的值分配到模板上之后,提交的时候都得带到doGoogleAuth,完成绑定

绑定动作

  1. public function doGoogleAuth($request)
  2. {
  3. $adminInfo = Admin::find($request->uid);
  4. if (!$adminInfo) return $this->returnData(0, '绑定账号不存在');
  5. if (empty($request->onecode) && strlen($request->onecode) != 6) return $this->returnData(0, '请正确输入手机上google验证码 ');
  6. $google = $request->google;
  7. if (GoogleAuthenticator::CheckCode($google, $request->onecode)) {
  8. // 绑定场景:绑定成功,向数据库插入google参数,跳转到登录界面让用户登录
  9. $adminInfo->google_code = $google;
  10. $adminInfo->save();
  11. // 登录认证场景:认证成功,执行认证操作
  12. return $this->returnData(0, '绑定成功');
  13. } else {
  14. return $this->returnData(0, '绑定失败');
  15. }
  16. }

登录动作

  1. 除了正常的字段外,增加一个Google验证码校验,方式同doGoogleAuth里的checkCode

完成

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post