Laravel 无痛使用 recaptcha 不用科学 sw 
                                                    
                        
                    
                    
  
                    
                    以 laravel5.7 登录为例。
步骤
首先
composer require 233sec/laravel-recaptchav3
修改 App\Http\Controllers\Auth\LoginController, 添加如下方法
    /**
     * Validate the user login request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function validateLogin(Request $request)
    {
        $request->validate([
            $this->username() => 'required|string',
            'password' => 'required|string',
            'g-recaptcha-response' => 'required|recaptchav3:login,0.5', // 重点在这一行
        ]);
    }
修改 resources/views/auth/login.blade.php
在 <button type="submit"...> 之前加入如下代码
            <div class="form-group
            @if ($errors->has('g-recaptcha-response'))
            has-error
            has-feedback
            @endif
            ">
                {!! RecaptchaV3::field('login') !!}
                @if ($errors->has('g-recaptcha-response'))
                    <span class="glyphicon glyphicon-warning-sign form-control-feedback" aria-hidden="true"></span>
                    <span class="help-block">{{ $errors->first('g-recaptcha-response') }}</span>
                @endif
            </div>
并在你的模板 javascript 处
@push('script')
    {!! RecaptchaV3::initJs() !!}
@endpush
在 .env 加入如下
RECAPTCHAV3_SITEKEY=#你的sitekey#
RECAPTCHAV3_SECRET=#你的secret#
RECAPTCHAV3_ORIGIN=https://www.recaptcha.net #这一行特别重要,否则在大陆无法使用
验证消息自定义
修改 resources/lang/#你的语言设置#/validation.php
<?php
return [
    ...,
    'custom' => [
        'g-recaptcha-response' => [
            'recaptchav3' => '验证码错误'
        ]
    ],
    'attributes' => [
        'g-recaptcha-response' => '验证码',
    ]
];
效果


来源
- 233sec/laravel-recaptchav3: https://github.com/233sec/laravel-recaptch... (基于 josiasmontag/laravel-recaptchav3)
 - josiasmontag/laravel-recaptchav3 https://github.com/josiasmontag/laravel-re...
 
本作品采用《CC 协议》,转载必须注明作者和本文链接
          
                    
                    
          
          
                关于 LearnKu
              
                    
                    
                    
 
推荐文章: