QrCode
,沒有安裝也能夠,接下來會安裝一、運行以下代碼安裝拓展包:php
1 composer require "earnp/laravel-google-authenticator:dev-master" 2 ### 安裝二維碼生成器 3 ### 若composer require不到文件自行github 下載源碼放入vendor相應的目錄下 4 composer require simplesoftwareio/simple-qrcode 1.3.*
二、等待下載安裝完成,須要在config/app.php
中註冊服務提供者同時註冊下相應門面:laravel
1 'providers' => [ 2 //........ 3 Earnp\GoogleAuthenticator\GoogleAuthenticatorServiceprovider::class, 4 SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class, 5 ], 6 7 'aliases' => [ 8 //.......... 9 'Google' => Earnp\GoogleAuthenticator\Facades\GoogleAuthenticator::class, 10 'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class 11 ],
三、服務注入之後,若是要使用自定義的配置,還能夠發佈配置文件到config/views目錄:git
1 ###這一步能夠不執行:視狀況而定 2 php artisan vendor:publish
使用方法很是簡單,主要爲生成驗證碼和校驗驗證碼github
生產驗證碼使用CreateSecret
便可,你須要將其內容生成二維碼供手機APP掃描,具體內容在google.blade.php
中已經配置成功數據庫
1 public function addUser(Request $request) 2 { 3 if($request->isMethod('get')){ 4 // 建立谷歌驗證碼 5 $createSecret = GoogleAuthenticator::CreateSecret(); 6 //$createSecret = [ 7 // "secret" => "NJURUPQN6XNYGSF2" 8 // "codeurl" => "otpauth://totp/?secret=NJURUPQN6XNYGSF2" 9 //] 10 // 生成二維碼 11 $createSecret["qrcode"] = QrCode::encoding('UTF-8')->size(180)->margin(1)->generate($createSecret["codeurl"]); 12 //發送頁面 13 return view('auth.auth.add',['google'=>$createSecret]); 14 } 15 //獲取數據 16 $user_from = $request->only(['role_id','username','pass','pass_confirmation','real_name','mobile','secret']); 17 18 //保存入庫 secret會存入數據庫 19 $auth_user = new AuthUserService(); 20 $res = $auth_user->addUser($user_from); 21 return redirect('admin/auth/index'); 22 }
1 //登陸驗證 2 public function login(array $param) 3 { 4 $model = new AuthUserModel(); 5 //Google 驗證 6 if(!GoogleAuthenticator::CheckCode($userInfo['secret'],$param['secret'])){ 7 return ['status'=>false,'msg'=>['secret'=>['驗證碼錯誤,請從新輸入']]]; 8 } 9 10 $update = $model->editLoginInfo($userInfo['id'], $update); 11 if(!$update){ 12 return ['status'=>false,'msg'=>['username'=>'更新登陸信息失敗']]; 13 }else{ 14 return ['status'=>true,'data'=>$userInfo]; 15 } 16 }
校驗驗證碼通常用於綁定,登陸認證中,使用CheckCode
方法便可,須要傳入secrect
和onecode
即驗證碼便可進行校驗,第一個爲secrect
;返回true
或false
app
if(Google::CheckCode($google,$request->onecode)) { // 綁定場景:綁定成功,向數據庫插入google參數,跳轉到登陸界面讓用戶登陸 // 登陸認證場景:認證成功,執行認證操做 dd("認證成功"); }else { // 綁定場景:認證失敗,返回從新綁定,刷新新的二維碼 return back()->with('msg','請正確輸入手機上google驗證碼 !')->withInput(); // 登陸認證場景:認證失敗,返回從新綁定,刷新新的二維碼 return back()->with('msg','驗證碼錯誤,請輸入正確的驗證碼 !')->withInput(); }
這裏有一個具體的實際事例:composer
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]); }