233sec / laravel-recaptchav3
Laravel 的 Recaptcha V3 包
1.0
2019-06-04 05:08 UTC
Requires
- php: >=7.0.0
- illuminate/framework: >=5.0
Requires (Dev)
- mockery/mockery: ^1.2
- orchestra/testbench: ~3.7.0
- phpunit/phpunit: 6.2|^7.0
README
Laravel 包,用于集成 Google 的 Recaptcha V3。
安装
要开始使用,请使用 Composer 将包添加到项目的依赖中
composer require josiasmontag/laravel-recaptchav3
将 RECAPTCHAV3_SITEKEY
和 RECAPTCHAV3_SECRET
添加到您的 .env
文件中。(您可以在 这里 获取它们)
RECAPTCHAV3_SITEKEY=sitekey
RECAPTCHAV3_SECRET=secret
可选地,您可以发布配置文件
php artisan vendor:publish --provider="Lunaweb\RecaptchaV3\RecaptchaV3ServiceProvider"
使用方法
初始化 Recaptcha Javascript
Recaptcha v3 在加载在每个页面上以获取最多交互上下文时表现最佳。因此,将其添加到您的头部或尾部模板中
{!! RecaptchaV3::initJs() !!}
表单
RecaptchaV3::field($action, $name='g-recaptcha-response')
创建一个不可见的输入字段,在加载时填充 Recaptcha 令牌。
<form method="post" action="/register"> {!! RecaptchaV3::field('register') !!} <input type="submit" value="Register"></input> </form>
验证
将 recaptchav3
验证器添加到规则数组中。该规则接受两个参数:动作名称和最小所需的 score
(默认为 0.5)。
$validate = Validator::make(Input::all(), [ 'g-recaptcha-response' => 'required|recaptchav3:register,0.5' ]);
获取分数
或者,您也可以获取分数并采取变量动作
// RecaptchaV3::verify($token, $action) $score = RecaptchaV3::verify($request->get('g-recaptcha-response'), 'register') if($score > 0.7) { // go } elseif($score > 0.3) { // require additional email verification } else { return abort(400, 'You are most likely a bot'); }
测试
为了使表单可测试,您可以模拟 RecaptchaV3
门面
RecaptchaV3::shouldReceive('verify') ->once() ->andReturn(1.0);