废话少说,直接开始。
1.下载mews/captcha
默认下载的是最新版,我这里下载的是3.2版本。
composer require mews/captcha
2.找到config\app.php中的providers,添加如下代码
关于Provider的介绍!
\Mews\Captcha\CaptchaServiceProvider::class,
3.找到config\app.php中的aliases,添加如下代码
关于aliases的介绍!
'Captcha' => Mews\Captcha\Facades\Captcha::class,
4.发布配置文件
php artisan vendor:publish
于是便可以在config\captcha.php下,配置验证码。这里使用的default
return [
'default' => [
'length' => 5,
'width' => 120,
'height' => 36,
'quality' => 90,
],
];
5.控制器代码
public function code()
{
return [
'code' => 101,
'message' => '请求成功',
'result' => app('captcha')->create('default', true)
];
}
返回结果:
{
"code": 101,
"message": "请求成功",
"result": {
"sensitive": false,
"key": "$2y$10$20a0XmywfMWedCV6GfFK2.EF0OaGtAiBTUkorbOAjqUqKoGLBhsBa",
"img": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAAkCAYAAABCKP5eAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAVkElEQVR4nI1be4xcV3n/nfu+c2f2aXvX60e8fmYd24lJIAEBVROVtqCqSdUWqaAWtWolIH2AkCgqIAqtSiXKozRIBdpIFWpFVUUtaoQKSh9JeAViJ3ZiJ+vHrl/rtb2PmdmZO/d9+sfsd+a7Z+6WHmm1M/eex3e+5+/7zhmxtLQksdmk7H8UQsC2bdRqNdi2DdM0IYQA7yeEUM+klMjzHEVRDL1L0xRhGKLb7SJJEnieh7GxMXieB9M0EUURer0e0jRFlmVI0xRFUcAwDDWPlFL9ZVmGbreLNE1hWRYcx4FpmoqOoigAAKZpwjRNtSd9b7ZtI8sy5Hle2geNp8/0jvZGdNF89ByA4gGNof+macIwDBiGgaIokOe5+k77okZ75nunvvw9rUU88DxP8ZT4lGUZLLBmGAZM04Rt27AsC4vNx9W7I1NfG1qcE8WVgL+nTeR5jjzP1eJpmiLPc/Wf/miMLhS+juu6sCxL0aszhJSMM1AXItHC90Fr0HOuIJzBfBw953vla+lCI2HzebmQqR/NSwql84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BpH8xVFARgmTG8UYlPBpZSlggZ5Cf4LTPKEVPblJ0xEA6ff4owkiE5ColyMuwbdYrdCyroVVlkM9eXnmJQK8LtGnCmkbDyhJyXh6ZKuGLr18n0TU3jqVKVU1EcXqm6tPHzoYYQEp7tTskiVv7JbqYRzeKggWfAYLYRAp9PBxsYGarUaRkdH+xffhRDqnq1hGEpj6OcnvErCtZRrJBeaLvgqC6K+ujcgV82tiVu9LmC9EKG7zCrww909f6cr51ZuXM8C+No6kKyai9eLdRq63a46+yUspP90h9w1B45UDGk2m1heXsbExAQ8z8P/AmKT5MLgUdluAAAAAElFTkSuQmCC"
}
}
sensitive:大小写是否敏感,在config\captcha.php中的default数组里可以添加这一选项,true代表区分大小写,默认false。 key:生成的key。 img:base_64的图片。
6. 后端验证代码
第一种:
$captcha = $request->input('captcha');
$key = $request->input('key');
if (captcha_api_check($v_code, $key)){
echo '验证通过';
}
}
第二种:
$data = $req->all();
$validator = Validator::make($data, [
'key' => 'required',
'captcha' => 'required|captcha_api:' . $req->input('ckey')
]);
if (!$validator->fails()) {
echo '验证通过';
}
两种方法的原理都是一样的,都是去调用了captcha_api_check方法。 第二种方法,在CaptchaServiceProvider类中的启动方法(boot)中定义了,
$validator->extend('captcha_api', function ($attribute, $value, $parameters) {
return captcha_api_check($value, $parameters[0], $parameters[1] ?? 'default');
});
7.一些问题解析
- 这里为什么调用的是captcha_api_check,而不是captcha_check?
因为调用生成图片验证码的方法create的时候,传递了第二个参数为true。 调用的其实就是在vendor\mews\captcha\src目录下的Captcha类中的create方法。第二个参数为true,代表$api = true;即为存储在Cache中的前后端分离用的验证码!而不是存储在session中的验证码。
- 如何使用session中的验证码?
调用create方法时,第二个参数不传就好了。注意要开启session! 如果使用的是api的话,应该是在Kernel中的$middlewareGroups数组中配置,这个不太确定,没有试。
- 验证码验证一次,就失效了,怎么办?
首先是在vendor\mews\captcha\src目录下的Captcha类中的check_api方法校验验证码。 这里的第一句代码就是:if (!Cache::pull($this->get_cache_key($key))) {
return false;
}
关键就是这个pull方法。Cache详解可参考 pull方法是获取到缓存以后,就把缓存清空了。所以只能验证一次。只需要把这里的pull换成get就OK了。 - 如何解决使用session存储验证码的时候,只能验证一次验证码的问题?
在vendor\mews\captcha\src目录下的Captcha类中的check方法,将以下这段代码注释掉应该就OK了。if ($check) {
$this->session->remove('captcha');
}
8.结语
当然,现在基本都是前后端分离,我更喜欢把验证码存入redis,从redis中获取key,去校验验证码即可。
参考文章
laravel mews/captcha 图形验证码 前后端分离
|