laravel-admin加谷歌验证器
示意图
添加谷歌验证首先需要
参考:
Laravel-admin 登录添加图形验证码
构建一个自己的登录系统
安装扩展
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
],
服务注入以后,如果要使用自定义的配置,还可以发布配置文件到config/views
目录
php artisan vendor:publish
该命令会创建视图文件
这里我将两个内容分开
一个用户显示步骤, 一个用户显示操作
使用
修改html页面
resources/views/admin/login.blade.php
将图形验证码html修改为以下代码
// 替换图形验证码
<div class="form-group has-feedback {!! !$errors->has('googlecode') ?: 'has-error' !!}">@if($errors->has('googlecode'))@foreach($errors->get('googlecode') as $message)<label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i>{{$message}}</label><br>@endforeach@endif<input type="text" class="form-control" placeholder="谷歌验证码" name="googlecode" value="{{ old('googlecode') }}"><span class="glyphicon glyphicon-tags form-control-feedback"></span>
</div>
将login/google/google.blade.php
中的表单提交视图修改为
<form action="{{ empty(Config::get('google.authenticatorurl')) ? URL::current() : Config::get('google.authenticatorurl') }}"method="POST">{!! csrf_field() !!}<input name="onecode" type="text" class="verificationcode" placeholder="请输入扫描后手机显示的6位验证码"value="{{ old('onecode') }}"/><div>后台账号 :<select id="parent" name="select">@foreach($parameter as $parame)<option value="{{$parame['id']}}">{{$parame['name']}}</option>@endforeach</select></div><input type="hidden" name="google" value="{{ $createSecret['secret'] }}"/><br/><button class="submit-button">立即绑定</button>@if(Session::has('msg'))<div class="notice">{{ Session::get('msg') }}</div>@endif</form>
设置路由
Route::get('googleauth', 'Web\GoogleAuthController@index');
Route::post('googleauth', 'Web\GoogleAuthController@doadd');
后端
<?phpnamespace App\Http\Controllers\Web;use App\Http\Controllers\Controller;
use App\Models\Admin;
use Earnp\GoogleAuthenticator\GoogleAuthenticator;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;class GoogleAuthController extends Controller
{public function index(){// 创建谷歌验证码$createSecret = GoogleAuthenticator::CreateSecret();// 您自定义的参数,随表单返回$admins = Admin::get();$parameter = $admins;return view('login.google.google', ['createSecret' => $createSecret, "parameter" => $parameter]);}public function doadd(Request $request){$admininfo = Admin::find($request->select);if (empty($admininfo)) {return back()->with('msg', '绑定账号有误 !')->withInput();}if ($request->isMethod('post')) {if (empty($request->onecode) && strlen($request->onecode) != 6) return back()->with('msg', '请正确输入手机上google验证码 !')->withInput();// google密钥,绑定的时候为生成的密钥;如果是绑定后登录,从数据库取以前绑定的密钥$google = $request->google;// 验证验证码和密钥是否相同if (GoogleAuthenticator::CheckCode($google, $request->onecode)) {// 绑定场景:绑定成功,向数据库插入google参数,跳转到登录界面让用户登录$admininfo->google_code = $google;$admininfo->save();// 登录认证场景:认证成功,执行认证操作return back()->with('msg', '验证成功 !')->withInput();} else {// 绑定场景:认证失败,返回重新绑定,刷新新的二维码return back()->with('msg', '请正确输入手机上google验证码 !')->withInput();// 登录认证场景:认证失败,返回重新绑定,刷新新的二维码
// return back()->with('msg', '验证码错误,请输入正确的验证码 !')->withInput();}}return back()->with('msg', '什么操作能到我这儿 !')->withInput();}
}
在自定义的登录页修改为如下
<?phpnamespace App\Admin\Controllers;use App\Models\Admin;
use Earnp\GoogleAuthenticator\GoogleAuthenticator;
use Encore\Admin\Controllers\AuthController as BaseAuthController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;class AuthController extends BaseAuthController
{public function getLogin(){if (!Auth::guard('admin')->guest()) {return redirect(config('admin.route.prefix'));}return view('admin.login');}public function postLogin(Request $request){$credentials = $request->only(['username', 'password', 'googlecode']);$validator = Validator::make($credentials, ['username' => 'required|exists:admin_users,username','password' => 'required','googlecode' => 'required']);if ($validator->fails()) {return Redirect::back()->withInput()->withErrors($validator);}$admininfo = Admin::where('username', $credentials['username'])->get()[0];if (!GoogleAuthenticator::CheckCode($admininfo->google_code, $credentials['googlecode'])) {// 登录认证场景:认证失败,返回重新绑定,刷新新的二维码return Redirect::back()->withInput()->withErrors(['googlecode' => '谷歌验证码有误']);}unset($credentials['googlecode']);if (Auth::guard('admin')->attempt($credentials)) {admin_toastr(trans('admin.login_successful'));return redirect()->intended(config('admin.route.prefix'));}return Redirect::back()->withInput()->withErrors(['username' => $this->getFailedLoginMessage()]);}protected function getFailedLoginMessage(){return Lang::has('auth.failed')? trans('auth.failed'): 'These credentials do not match our records.';}
}
这里的 $admininfo->google_code
指的是 admin_users
表中添加一个 google_code
字段, 用于存储谷歌验证器的验证码.
想要修改扫码后的名称
修改 config/google.php
<?phpreturn [// 中文需要UrlEncode转为utf8编码"authenticatorname" => "修改内容(不能中文)","authenticatorurl" => "",];
课外
宝塔使用laravel 生成二维码图片, 需要安装 imagick 扩展
\QrCode::format('png')->generate('hello world!!!', public_path('qrcodes/qrcode.png'));
就可生成图片
laravel-admin加谷歌验证器相关推荐
- laravel 加谷歌验证
谷歌验证这里使用的Google的两步验证一样,使用 TOTP 或 HOTP 算法生成一次性密码,一次性密码只有在生成当时使用才有效. 当您登陆时,您需要输入用户名密码,同时也需要输入手机上的两步验证码 ...
- google authenticator python_谷歌验证器 Google Authenticator工作原理
很多人都听过谷歌验证 (Google Authenticator) 或用过谷歌验证 (Google Authenticator) .尤其是随着比特币等虚拟货币的兴起,各大交易所都要求绑定谷歌验证 (G ...
- 谷歌验证器的原理及JS实现
阅读本篇文章你可以了解到谷歌验证器的实现原理,并且可以自己使用node.js实现支持谷歌验证器的两步验证. 这两年发现身边的很多应用和网站纷纷支持两步验证,并且呼吁用户使用两步验证. 并且发现,除了A ...
- 计算机器怎么验证,谷歌验证器【设置步骤】
喜欢使用电脑的小伙伴们一般都会遇到win7系统谷歌验证器的问题,突然遇到win7系统谷歌验证器的问题就不知道该怎么办了,其实win7系统谷歌验证器的解决方法非常简单,按照 1:首先打开手机上的应用商店 ...
- PHP设置谷歌验证器(Google Authenticator)实现操作二步验证
使用说明:开启Google的登陆二步验证(即Google Authenticator服务)后用户登陆时需要输入额外由手机客户端生成的一次性密码.实现Google Authenticator功能需要服务 ...
- PHP实现谷歌验证器二次验证
一.什么是谷歌身份验证器? 不少网站在登陆或者操作时都需要谷歌身份验证器(Google Authenticator),就是说在输入用户名和密码之后还需要输入一个动态密码,而这个动态密码由手机APP谷歌 ...
- linux安装谷歌验证器
linux安装谷歌验证器 同步时间 下载程序并编译 设置pam.d/sshd 设置/etc/ssh/sshd_conf 重启sshd 登陆验证 同步时钟 root@li'bin:# service n ...
- DMCH支持谷歌验证器上链!
首先介绍一下谷歌验证器(不是一般的强) 谷歌验证器是一种双因素身份安全认证器,具有双重安全保章,这个是一个移动终端的app应用,也可以布置到网站上进行安全布控,安全级别不是一般的高,类似国内支付宝所用 ...
- Laravel引入谷歌验证器
一.首先安装composer包 命令如下 composer require "earnp/laravel-google-authenticator:dev-master" 二.因为 ...
最新文章
- android Binder机制(一)架构设计
- 计算机专业教研成绩,2018学年第一学期计算机组教研组工作计划
- 网关屏蔽mac地址,linux下修改mac地址方法
- 转 iOS socket
- 从零开始搭二维激光SLAM --- Karto的前端实现与解读
- VirtualBox安装RedHat7
- 建立在线社交网络中的关系权重模型
- 计算机导论的论文范例,★计算机导论论文提纲范文计算机导论论文提纲格式模板...
- Win11任务栏透明度怎么调整?Win11任务栏透明度设置教程
- 云计算是什么?云计算开发学习路线
- python使用gdal读写BIP、BSQ数据格式tif及相互转换
- iOS之深入解析App Thinning的应用瘦身优化
- pwc(普华永道)招聘.net
- z自建服务器,《守望先锋》将加入自建服务器 自定规则
- win10安装linux虚拟机并配置shell工具连接
- 苹果手机上滑动会卡顿_苹果手机Safari浏览器下滑动卡顿的问题
- oracle 汉字显示问号
- 转:沪江小d每日一句一周详解(1.26~2.1)
- GlusterFs安装部署文档
- 15-责任链模式Quarkus实现