本篇文章主要講述了用laravel實現google二維碼驗證器,具有一定的參考價值,希望感興趣的朋友可以了解一下。
開發前的準備
- 安裝laravel?
- 安裝二維碼生成器QrCode,沒有安裝也可以,接下來會安裝
安裝拓展
1、運行如下代碼安裝拓展包:
1?composer?require?"earnp/laravel-google-authenticator:dev-master" 2?###?安裝二維碼生成器 3?###?若composer?require不到文件自行github?下載源碼放入vendor相應的目錄下 4?composer?require?simplesoftwareio/simple-qrcode?1.3.*
2、等待下載安裝完成,需要在config/app.php中注冊服務提供者同時注冊下相應門面:
?'providers'?=>?[ ????//........ ????EarnpGoogleAuthenticatorGoogleAuthenticatorServiceprovider::class, ????SimpleSoftwareIOQrCodeQrCodeServiceProvider::class, ], 'aliases'?=>?[ ?????//.......... ????'Google'?=>?EarnpGoogleAuthenticatorFacadesGoogleAuthenticator::class, ????'QrCode'?=>?SimpleSoftwareIOQrCodeFacadesQrCode::class ],
3、服務注入以后,如果要使用自定義的配置,還可以發布配置文件到config/views目錄:
1?###這一步可以不執行:視情況而定 2?php?artisan?vendor:publish
使用一(項目中使用)
使用方法非常簡單,主要為生成驗證碼和校驗驗證碼
1、生產驗證碼
生產驗證碼使用CreateSecret即可,你需要將其內容生成二維碼供手機APP掃描,具體內容在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'); }
2、校驗驗證碼
//登錄驗證 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]; ????} }
?
使用二(Demo)
1、校驗驗證碼
校驗驗證碼一般用于綁定,登錄認證中,使用CheckCode方法即可,需要傳入secrect和onecode即驗證碼即可進行校驗,第一個為secrect;返回true或false
if(Google::CheckCode($google,$request->onecode))?{ ????//?綁定場景:綁定成功,向數據庫插入google參數,跳轉到登錄界面讓用戶登錄 ????//?登錄認證場景:認證成功,執行認證操作 ????dd("認證成功"); }else?{ ????//?綁定場景:認證失敗,返回重新綁定,刷新新的二維碼 ????return?back()->with('msg','請正確輸入手機上google驗證碼?!')->withInput(); ????//?登錄認證場景:認證失敗,返回重新綁定,刷新新的二維碼 ????return?back()->with('msg','驗證碼錯誤,請輸入正確的驗證碼?!')->withInput(); }
這里有一個具體的實際事例:
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]); }
相關教程:laravel視頻教程
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END