Home > PHP Framework > Laravel > body text

How to implement google-authenticator--Google QR code validator in Laravel

little bottle
Release: 2019-04-22 10:54:22
forward
7026 people have browsed it

This article mainly talks about using Laravel to implement Google QR code validator. It has certain reference value. I hope interested friends can learn about it.

Preparation before development

  1. Install Laravel
  2. Install the QR code generatorQrCode, you can do it without installing it, it will be installed next

Install the expansion package

1. Run the following code to install the expansion package:

1 composer require "earnp/laravel-google-authenticator:dev-master"
2 ### 安装二维码生成器
3 ### 若composer require不到文件自行github 下载源码放入vendor相应的目录下
4 composer require simplesoftwareio/simple-qrcode 1.3.*
Copy after login

2. Wait for the download and installation to complete. You need to install it in config/app.php Register the service provider and register the corresponding facade at the same time:

 'providers' => [
    //........
    Earnp\GoogleAuthenticator\GoogleAuthenticatorServiceprovider::class,
    SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class,
],

'aliases' => [
     //..........
    'Google' => Earnp\GoogleAuthenticator\Facades\GoogleAuthenticator::class,
    'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class
],
Copy after login

3. After service injection, if you want to use custom configuration, you can also publish the configuration file to the config/views directory:

1 ###这一步可以不执行:视情况而定
2 php artisan vendor:publish
Copy after login

Use one (used in the project)

The usage method is very simple, mainly to generate verification code and verify verification code

1, production verification code

Production verification Use CreateSecret for the code. You need to generate a QR code from its content for scanning by the mobile APP. The specific content has been successfully configured in google.blade.php


public function addUser(Request $request)
{
    if($request->isMethod('get')){
         // 创建谷歌验证码
         $createSecret = GoogleAuthenticator::CreateSecret();
         //$createSecret = [
         //   "secret" => "NJURUPQN6XNYGSF2"
         //   "codeurl" => "otpauth://totp/?secret=NJURUPQN6XNYGSF2"
         //]
         // 生成二维码
         $createSecret["qrcode"] = QrCode::encoding('UTF-8')->size(180)->margin(1)->generate($createSecret["codeurl"]);
         //发送页面
         return view('auth.auth.add',['google'=>$createSecret]);
    }
    //获取数据
    $user_from = $request->only(['role_id','username','pass','pass_confirmation','real_name','mobile','secret']);
   
    //保存入库 secret会存入数据库
    $auth_user = new AuthUserService();
    $res = $auth_user->addUser($user_from);
    return redirect('admin/auth/index');
}
Copy after login

2. Verification verification code

//登录验证
public function login(array $param)
{
    $model = new AuthUserModel();
    //Google 验证
    if(!GoogleAuthenticator::CheckCode($userInfo['secret'],$param['secret'])){
        return ['status'=>false,'msg'=>['secret'=>['验证码错误,请重新输入']]];
    }

    $update = $model->editLoginInfo($userInfo['id'], $update);
    if(!$update){
        return ['status'=>false,'msg'=>['username'=>'更新登录信息失败']];
    }else{
        return ['status'=>true,'data'=>$userInfo];
    }
}
Copy after login

Use two (Demo)

1 , Verification verification code

Verification verification code is generally used for binding. In login authentication, use the CheckCode method. You need to pass in secrect and onecode is the verification code that can be verified. The first one is secrect; return true or false


##

if(Google::CheckCode($google,$request->onecode)) {
    // 绑定场景:绑定成功,向数据库插入google参数,跳转到登录界面让用户登录
    // 登录认证场景:认证成功,执行认证操作
    dd("认证成功");
}else {
    // 绑定场景:认证失败,返回重新绑定,刷新新的二维码
    return back()->with('msg','请正确输入手机上google验证码 !')->withInput();
    // 登录认证场景:认证失败,返回重新绑定,刷新新的二维码
    return back()->with('msg','验证码错误,请输入正确的验证码 !')->withInput();
}
Copy after login

Here is a specific practical example:

use Google;

if ($request->isMethod('post')) {
    if (empty($request->onecode) && strlen($request->onecode) != 6) return back()->with('msg','请正确输入手机上google验证码 !')->withInput();
    // google密钥,绑定的时候为生成的密钥;如果是绑定后登录,从数据库取以前绑定的密钥
    $google = $request->google;
    // 验证验证码和密钥是否相同
    if(Google::CheckCode($google,$request->onecode)) {
        // 绑定场景:绑定成功,向数据库插入google参数,跳转到登录界面让用户登录
        // 登录认证场景:认证成功,执行认证操作
        dd("认证成功");
    }else {
        // 绑定场景:认证失败,返回重新绑定,刷新新的二维码
        return back()->with('msg','请正确输入手机上google验证码 !')->withInput();
        // 登录认证场景:认证失败,返回重新绑定,刷新新的二维码
        return back()->with('msg','验证码错误,请输入正确的验证码 !')->withInput();
    }
}else {
    // 创建谷歌验证码
    $createSecret = Google::CreateSecret();
    // 您自定义的参数,随表单返回
    $parameter = [["name"=>"usename","value"=>"123"]];
    return view('login.google.google', ['createSecret' => $createSecret,"parameter" => $parameter]);
}
Copy after login
Related tutorials:

laravel video tutorial

The above is the detailed content of How to implement google-authenticator--Google QR code validator in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template