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 协议》,转载必须注明作者和本文链接
什么原理不用梯子
@webstar
因为 https://www.recaptcha.net 是 Google 在北京的服务器
https://www.recaptcha.net 打开显示404 又被墙了?
@CorePlusPlus
这个域名是解析在了北京,所以不可能提供google其他的服务的,不然也被封了。
@XiaohuiLam 好的 感谢分享
看到标题 无痛 第一个想到了 人流~
@someonelikeyou
同九义,汝何秀