谷歌验证器的使用

如果你不想发短信,又想搞一个免费的验证工具,那么请看这里

话不多说,直接上代码:

### 安装谷歌验证器相关依赖
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
],

谷歌验证码绑定页面,需要先下载谷歌验证器App

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('绑定账号不存在');
    }
    if (empty($request->onecode) && strlen($request->onecode) != 6) {
        return $this->returnData('请正确输入手机上google验证码 ');
    }
    if (GoogleAuthenticator::CheckCode($request->google, $request->onecode)) {
        // 绑定场景:绑定成功,向数据库插入google参数,跳转到登录界面让用户登录
        $adminInfo->google_code = $request->google;
        $adminInfo->save();
        // 登录认证场景:认证成功,执行认证操作
        return $this->returnData('绑定成功'200;
    } else {
        return $this->returnData('绑定失败');
    }
}

登录动作

// 除了正常的字段外,增加一个Google验证码校验,方式同doGoogleAuth里的checkCode
if (!GoogleAuthenticator::CheckCode($adminInfo->google_code, $request->google_code)) {
    return $this->returnData('谷歌验证码错误');
}

完成

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 6

你这是哪个admin后台?

2年前 评论
礼物粑粑 (楼主) 2年前

好像一直返回的都是false

2年前 评论
礼物粑粑 (楼主) 2年前

加了config/app.php 项目全部报错 本地没有这个问题

                                                                              In ProviderRepository.php line 208:
                                                                               Class 'Earnp\GoogleAuthenticator\GoogleAuthenticatorServiceprovIDer' not found  
2年前 评论

已解决 不知道什么操作 后面几个变成了大写字母,linux不识别还是什么原因,重新复制粘贴就行了

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!