1. 根底的装置 / 用法请参照我的项目文档
github: https://github.com/mewebstudi…
2. api 调用接口
http://localhost/captcha/api/math
math 能够替换为配置文件的所定义的验证码类型
该接口返回 json, 次要应用两个参数 key 和 img
key 须要一起传递给后盾做验证
img 是图片 base64 用作前端显示
后盾验证
$rules = ['captcha' => 'required|captcha_api:'. request('key') . ',default'];
$validator = validator()->make(request()->all(), $rules);
3. 集成 passport 做登录验证
通过 middleware 形式拦挡验证
创立 CheckLoginCaptchaMiddleware 中间件
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class CheckLoginCaptchaMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{if($request->path()==="oauth/token"){ // 判断是否是 passport 登录接口
$check = validator($request->all(),['captcha' => 'required|captcha_api:'. request('key') . ',login'
]);
if(!$check->passes()){return response(["message"=>$check->errors()->first()],400);
}
}
return $next($request);
}
}
在 Http/kernel.php
protected $middleware = [
...
\App\Http\Middleware\CheckLoginCaptchaMiddleware::class,
];