zjsy / captcha-lumen5
为 Lumen 的验证码
dev-master
2019-08-03 13:51 UTC
Requires
- php: >=7.1.3
- ext-gd: *
- illuminate/config: ~5.0
- illuminate/filesystem: ~5.0
- illuminate/hashing: ~5.0
- illuminate/support: ~5.0
- intervention/image: ~2.2
This package is auto-updated.
Last update: 2024-09-29 06:14:20 UTC
README
本项目修改了 Captcha for Laravel 5 和 lumen-captcha,使其适用于5.8以上版本
预览
安装
- 此包必须开启缓存才能使用,因为验证码是保存在缓存中的。
- 由于5.8版本以上缓存存储的时间单位从分钟变为秒,所以这里做了调整
composer require zjsy/captcha-lumen5
如何使用
在bootstrap/app.php
中注册Captcha Service Provider:
$app->register(zjsy\CaptchaLumen5\CaptchaServiceProvider::class); class_alias('zjsy\CaptchaLumen5\Facades\Captcha','Captcha');
设置
在bootstrap/app.php
中可以设置各种自定义类型的验证码属性,更多详细设置请查看 Captcha for Laravel 5
'characters' => '2346789abcdefghjmnpqrtuxyzABCDEFGHJMNPQRTUXYZ', 'useful_time' => 300, //驗證碼有效時間(秒) 'login' => [ //驗證碼樣式 'length' => 4, //驗證碼字數 'width' => 100, //圖片寬度 'height' => 44, //字體大小和圖片高度 'angle' => 10, //字體傾斜度 'lines' => 2, //橫線數 'quality' => 90, //品質 'invert' =>false, //反相 'bgImage' =>true, //背景圖 'bgColor' =>'#ffffff', 'fontColors'=>['#339900','#ff3300','#9966ff','#3333ff'],//字體顏色 ], 'flat' => [ 'length' => 6, 'width' => 160, 'height' => 46, 'quality' => 90, 'lines' => 6, 'bgImage' => false, 'bgColor' => '#ecf2f4', 'fontColors'=> ['#2c3e50', '#c0392b', '#16a085', '#c0392b', '#8e44ad', '#303f9f', '#f57c00', '#795548'], 'contrast' => -5, ], 'mini' => [ 'length' => 3, 'width' => 60, 'height' => 32, ], 'inverse' => [ 'length' => 5, 'width' => 120, 'height' => 36, 'quality' => 90, 'sensitive' => true, 'angle' => 12, 'sharpen' => 10, 'blur' => 2, 'invert' => true, 'contrast' => -5, ]
如果不配置配置文件,默认就是default,验证码有效期为5分钟。
示例
因为 Lumen 都是无状态的 API,所以验证码图片都会绑定一个 UUID,先获得验证码的 UUID 和图片的 URL,验证时再一并发送验证码与 UUID。
生成
获得验证码:
{Domain}/captchaInfo/{type?}
type
就是在 config 中定义的 Type,如果不指定type
,默认为default
样式,Response:
{ "captchaUrl": "http://{Domain}/captcha-image/login/782fdc90-3406-f2a9-9573-444ea3dc4d5c", "captchaUuid": "782fdc90-3406-f2a9-9573-444ea3dc4d5c" }
captchaUrl
为验证码图片的 URL,captchaUuid
为绑定验证码图片的uuid。
验证
在发送 Request 时将验证码与 UUID 一并送回 Server 端,在接收参数时做验证即可:
public function checkCaptcha(Request $request, $type = 'default',$captchaUuid) { $this->validate($request,[ 'captcha'=>'required|captcha:'.$captchaUuid ],[ 'captcha.required'=>'验证码不能为空!' 'captcha.captcha'=>'验证码错误!' ]); ... }