我正在尝试在没有任何软件包的情况下使用 Laravel 8 安装 Google reCaptcha v2。我正在尝试关注this教程。我尝试过 this还有教程。
但验证码未显示在 Blade 文件中。
提交表单
后如何验证验证码?
最佳答案
Laravel 8 Google Captcha 没有任何第三方包。
首先在.env
文件中添加以下键
GOOGLE_CAPTCHA_SITE_KEY=6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI
GOOGLE_CAPTCHA_SECRET_KEY=6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe
GOOGLE_CAPTCHA_VERIFICATION_URL=https://www.google.com/recaptcha/api/siteverify
注意:这些是来自谷歌文档的测试 key 。
然后在config
文件夹中创建一个名为google_captcha.php
的文件
<?php
return [
'site_key' => env('GOOGLE_CAPTCHA_SITE_KEY'),
'secret_key' => env('GOOGLE_CAPTCHA_SECRET_KEY'),
'gc_verification_url' => env('GOOGLE_CAPTCHA_VERIFICATION_URL'),
'error_codes' => [
"missing-input-secret" => "The secret parameter is missing.",
"invalid-input-secret" => "The secret parameter is invalid or malformed.",
"missing-input-response" => "The response parameter is missing.",
"invalid-input-response" => "The response parameter is invalid or malformed.",
"bad-request" => "The request is invalid or malformed.",
"timeout-or-duplicate" => "The response is no longer valid: either is too old or has been used previously.",
],
];
为了在服务器端验证谷歌验证码,我们首先创建验证规则。我们可以采取多种方法,但我选择两种方法
1.在AppServiceProvider
中创建自定义规则,如下所示
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Validator::extend('google_captcha', function ($attribute, $value, $parameters, $validator){
$http=Http::asForm()->post(config('google_captcha.gc_verification_url'),[
'secret' => config('google_captcha.secret_key'),
'response' =>$value,
]);
if(!$http->object()->success){
$errorMessage=null;
collect($http->object()->{"error-codes"})->each(function ($item)use(&$errorMessage){
$errorMessage.=config('google_captcha.error_codes')[$item];
});
$validator->addReplacer('google_captcha',
function($message, $attribute, $rule, $parameters) use ($errorMessage) {
return \str_replace(':message', $errorMessage, $message);
}
);
}
return $http->object()->success;
},":message");
}
}
验证规则如下所示
$validator=Validator::make($request->all(),[
'g-recaptcha-response'=>'required|google_captcha'
]);
html 表单将是
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
<!-- Styles -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<form method="POST" action="{{ route('test') }}">
@csrf
<div class="g-recaptcha" data-sitekey="{{config('google_captcha.site_key')}}"></div>
<button type="submit" class="btn btn-primary pull-right"
style="margin-right: 15px;">Submit</button>
</form>
<script src="https://www.google.com/recaptcha/api.js"></script>
</body>
</html>
假设如果您不想在应用服务提供商中编写自定义验证规则,那么您可以使用如下所示的内联验证
$validator=Validator::make($request->all(),[
'g-recaptcha-response'=>['required', function ($attribute, $value, $fail) {
$http=Http::asForm()->post(config('google_captcha.gc_verification_url'),[
'secret' => config('google_captcha.secret_key'),
'response' =>$value,
]);
if(!$http->object()->success){
$errorMessage=null;
collect($http->object()->{"error-codes"})->each(function ($item)use(&$errorMessage){
$errorMessage.=config('google_captcha.error_codes')[$item];
});
$fail($errorMessage);
}
}]
]);
文档引用链接
1.验证码验证: https://developers.google.com/recaptcha/docs/verify
2.测试键:https://developers.google.com/recaptcha/docs/faq
3.自定义验证规则:https://laravel.com/docs/8.x/validation#custom-validation-rules
4.Laravel Http 客户端:https://laravel.com/docs/8.x/http-client#introduction
5.谷歌验证码管理链接:https://www.google.com/recaptcha/about/
关于laravel - 谷歌 reCaptcha 与 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66720254/