Laravel 9.x Jetstream Captcha 验证码
采用 mews/captcha
执行
composer require mews/captcha
在
config/app.php
中添加'providers' => [ ... Mews\Captcha\CaptchaServiceProvider::class, ]
执行
php artisan vendor:publish
配置文件在
config/captcha.php
, 看情况修改在对应的模板中添加
... <div class="mt-4"> <div class="mt-2"> <img src="{{captcha_src()}}" style="cursor: pointer" onclick="this.src='{{captcha_src()}}'+Math.random()" /> </div> <div class="mt-2"> <x-jet-input type="text" class="form-control {{$errors->has('captcha')?'parsley-error':''}}" name="captcha" placeholder="captcha" /> </div> </div> ...
新建
app/Actions/Fortify/CaptchaValidation.php
<?php namespace App\Actions\Fortify; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class CaptchaValidation { public function __invoke(Request $request, $next) { Validator::make($request->all(), [ 'captcha' => 'required|captcha' ])->validate(); return $next($request); } }
在
app/Providers/JetstreamServiceProvider.php
的boot
方法中添加public function boot() { // ... // 记得没有 use 的上面 use 一下 Fortify::authenticateThrough(function (Request $request) { return array_filter([ config('fortify.limiters.login') ? null : EnsureLoginIsNotThrottled::class, CaptchaValidation::class, Features::enabled(Features::twoFactorAuthentication()) ? RedirectIfTwoFactorAuthenticatable::class : null, AttemptToAuthenticate::class, PrepareAuthenticatedSession::class, ]); }); }
然后就应该 ok 了
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: