Laravel项目+Google验证器

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

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

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

      # 二维码生成器:

      composer require simplesoftwareio/simple-qrcode 1.3.*

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

Laravel项目+Google验证器_第1张图片

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

 

Laravel项目+Google验证器_第2张图片

Laravel项目+Google验证器_第3张图片

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代码:

5、JQuery代码:

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、效果图如下:

Laravel项目+Google验证器_第4张图片

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,php)