1、首先要在你的Laravel项目中安装Google验证器插件、二维码生成器插件,执行命令如下:

# Google验证器插件安装命令:

composer require "earnp/laravel-google-authenticator:dev-master"

# 二维码生成器:

composer require simplesoftwareio/simple-qrcode 1.3.*

安装完成后,会自动在composer.json文件中加入版本信息,如果没有成功,手动添加,再执行,如下图所示:

2、扩展安装后,结构如下图所示:

3、安装完成后,在config/app.php中注册服务提供者同时注册下相应门面,代码如下:

'providers' => [/** Laravel Framework Service Providers...*/... ... ... ...App\Providers\AppServiceProvider::class,App\Providers\AuthServiceProvider::class,App\Providers\EventServiceProvider::class,App\Providers\RouteServiceProvider::class,// Google验证器Earnp\GoogleAuthenticator\GoogleAuthenticatorServiceprovider::class,// 二维码生成器SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class,
],
'aliases' => ['App' => Illuminate\Support\Facades\App::class,'Artisan' => Illuminate\Support\Facades\Artisan::class,'Auth' => Illuminate\Support\Facades\Auth::class,'Blade' => Illuminate\Support\Facades\Blade::class,... ... ... ... ...// 谷歌验证器'Google' => Earnp\GoogleAuthenticator\Facades\GoogleAuthenticator::class,// 二维码生成器'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class
],

4、html代码:

<div class="modal-body col-sm-4"><div class="box-header"><h3 class="box-title">Google验证器设置</h3></div><div class="box-header"><label class="control-label" for="google_status">谷歌验证器:</label><label><input name="google_status" type="radio" value="1" @if($user->google_status === 1) checked @endif class="on" />启用</label><label>&nbsp;</label><label><input type="radio" name="google_status" value="0" @if($user->google_status === 0) checked @endif class="off" />禁用</label></div><div class="form-hide box-header" style="display: none"><label class="control-label" for="secret">验证器秘钥:</label><label><input type="text" class="form-control " name="secret" id="secret" value="{{$google['secret']}}" readonly="true"></label><div class="modal-body text-center"><label class="control-label"></label><?php echo $google['qrcode'] ?></div></div><div class="box-header"><button type="submit" class="pretty-btn">提交</button></div>
</div>

5、JQuery代码:

<script>$(document).ready(function () {$('#system').addClass('active');$('#change_password').addClass('active');// 点击事件(开启、关闭谷歌验证器)$(".on").click(function(e) {$(".form-hide").show().removeClass("show");});$(".off").click(function(e) {$(".form-hide").hide().removeClass("show");});});
</script>

6、PHP代码 (首先引入GoogleAuthenticator.php、QrCode.php这2个文件):

use Earnp\GoogleAuthenticator\GoogleAuthenticator;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
/*** 账号管理* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View*/
public function merchant_account(){$user = $this->getUser();// 判断该用户是否已经存在google秘钥、没有重新生成if (empty($user['secret']) && $user['google_status'] === 0) {// 获取google秘钥$google = GoogleAuthenticator::CreateSecret();// 生成二维码$google["qrcode"] = QrCode::encoding('UTF-8')->size(180)->margin(1)->generate($google["codeurl"]);} else {$google['secret'] = $user['secret'];$google_url = "otpauth://totp/?secret=" . $user['secret'];// 生成二维码$google["qrcode"] = QrCode::encoding('UTF-8')->size(180)->margin(1)->generate($google_url);}return view('merchant.system.merchant_account',compact('user','google'));
}/*** 账号管理处理方法* @param Request $request* @return \Illuminate\Http\RedirectResponse* @throws ErrorMessageException*/
public function merchant_account_do(Request $request){if($request->isMethod('post')){$param = $request->all();$user=$this->getUser();if (empty($user['secret'])) {$user->secret = $param['secret'];$user->google_status = $param['google_status'];}else{$user->google_status = $param['google_status'];}if ($user->save()) {return redirect()->back()->with('msg', 'Google验证器设置成功!');} else {throw new ErrorMessageException("Google验证器设置失败!");}}
}

7、效果图如下:

8、开启后,登录需要输入Google验证码:

则,在登录方法里,将用户输入的Google验证码与用户秘钥进行匹配,代码如下:

/*** 重写登录成功回应* Google 验证* @param Request $request* @return \Illuminate\Http\RedirectResponse* @throws \App\Exceptions\ErrorMessageException*/
public function sendLoginResponse(Request $request){$param = Request()->all();$user = $this->getUser();// 判断该用户是否开启google验证// 将用户输入的验证码与秘钥进行匹配if(1 === $user['google_status']){// Google验证码与秘钥进行匹配if(!GoogleAuthenticator::CheckCode($user['secret'],$param['secret'])){throw ValidationException::withMessages([$this->username() => [trans('auth.secret')],]);}}UserIp::create(['user_id' => $this->getUser()->id,'ip' => $request->ip(),]);$request->session()->regenerate();$this->clearLoginAttempts($request);return $this->authenticated($request, $this->guard()->user())?: redirect()->intended($this->redirectPath());
}

匹配成功,成功登录、匹配失败,友好提示!

希望能帮到大家,同事也为自己做笔记,不喜勿喷!

Laravel项目+Google验证器相关推荐

  1. 基于Google 验证器 实现内网的双因素认证

    最近的一个项目需要实现双因素强认证,平常我们都是采用 静态密码+动态短信这样的方式来实现,但用户侧并没有相应的短信接口. 后来决定采用 google身份验证器来实现.在网上找了一些资料和代码片段,经过 ...

  2. Laravel引入谷歌验证器

    一.首先安装composer包 命令如下 composer require "earnp/laravel-google-authenticator:dev-master" 二.因为 ...

  3. Java web接入google身份验证器二次验证

    实现原理参考: https://blog.seetee.me/post/2011/google-two-step-verification/ 第一步: maven工程加入依赖 <dependen ...

  4. 谷歌验证器的原理及JS实现

    阅读本篇文章你可以了解到谷歌验证器的实现原理,并且可以自己使用node.js实现支持谷歌验证器的两步验证. 这两年发现身边的很多应用和网站纷纷支持两步验证,并且呼吁用户使用两步验证. 并且发现,除了A ...

  5. google authenticator python_谷歌验证器(Google Authenticator)

    双因素身份认证就是经过你所知道再加上你所能拥有的这二个要素组合到一块儿才能发挥做用的身份认证系统.双因素认证是一种采用时间同步技术的系统,采用了基于时间.事件和密钥三变量而产生的一次性密码来代替传统的 ...

  6. PHP设置谷歌验证器(Google Authenticator)实现操作二步验证

    使用说明:开启Google的登陆二步验证(即Google Authenticator服务)后用户登陆时需要输入额外由手机客户端生成的一次性密码.实现Google Authenticator功能需要服务 ...

  7. laravel集成谷歌验证_如何将Google的两因素身份验证添加到Laravel

    laravel集成谷歌验证 Laravel is a wonderful PHP framework that makes building applications with PHP a lot o ...

  8. 使用C++实现谷歌身份验证器(Google Authenticator)

    使用C++实现谷歌身份验证器(Google Authenticator) 本机环境: windows10 x64位运行环境 1.进入网站:http://slproweb.com/products/Wi ...

  9. Laravel实现google-authenticator--Google二维码验证器

    开发前的准备 安装Laravel 安装二维码生成器QrCode,没有安装也可以,接下来会安装 安装拓展 1.运行如下代码安装拓展包: composer require "earnp/lara ...

最新文章

  1. Flash中的“插入关键帧”和“插入空白关键帧”的区别
  2. 【javascript基础】8、闭包
  3. 如何将.py文件转换为.exe
  4. 动窗口的制作暨CSizingControlBar类的使用说明
  5. 前端如何实现音乐盒胶盘的转动_郑州Web前端入门教程之如何实现图片优化?
  6. leetcode1050. 合作过至少三次的演员和导演(SQL)
  7. word 代码块_如何优雅的写好 Pythonic 代码?
  8. 计算机保研和考研复试相同吗,保研与考研有什么区别?
  9. linux 程序收到sigsegv信号_Linux基础知识(五)
  10. # heapsort
  11. html滑动验证图片,js插件实现图片滑动验证码
  12. 压力单位MPa、Psi和bar之间换算公式
  13. 计算机文件丢失系统无法启动,windows7文件丢失无法启动怎么修复_win7系统显示文件丢失无法启动修复方法-win7之家...
  14. const T 与T const(const T vs.T const的翻译 Dan Saks)
  15. 原来射极跟随器还有这个应用
  16. Java并发编程与技术内幕:ThreadFactory、ThreadLocal
  17. Pytorch的grad、backward()、zero_grad()
  18. 字符常量及字符变量(实例:大小写字母转换)
  19. python版本和Matlab版本对应的关系,python调用matlab
  20. 计算机基础题精选(一)

热门文章

  1. 2019杭州电子科技大学计算机考研经验——97天一战上岸
  2. ICO和区块链的关系
  3. 基于机器学习算法的LTE高投诉小区预判方法
  4. 手机视频投屏到电视或投影仪
  5. API获取微信小程序二维码
  6. unity的九宫格切割
  7. 黑鲨Android系统耗电高,安卓顶配,黑鲨2pro作为主力机使用四天,来聊聊使用感受...
  8. Springboot面试杀手锏-自动配置原理
  9. AI赋能智慧图书馆,能否出现真正的书天堂?
  10. 将禾赛激光雷达在rviz中采集的点云保存成pcd格式