当前位置: 首页 > news >正文

Laravel - API 项目适用的图片验证码

1. 安装 gregwar/captcha

图片验证码接口的流程是:

生成图片验证码
生成随机的 key,将验证码文本存入缓存。
返回随机的 key,以及验证码图片

# 不限于 laravel 普通 php 项目也可以使用额
$ composer require gregwar/captcha

2. 开发接口

1). 新建路由

routes/web.php

oute::prefix('auth')->group(function (){Route::post('captchas', [CaptchasController::class, 'store']);    
});

2). 新建控制器和表单验证类

创建 CaptchasController 以及 CaptchaRequest

$ php artisan make:controller CaptchasController
$ php artisan make:request Api/CaptchaRequest

修改文件如下

app/Http/Requests/Api/CaptchaRequest.php

<?phpnamespace App\Http\Requests\Api;use Illuminate\Foundation\Http\FormRequest;class CaptchaRequest extends FormRequest
{/*** Determine if the user is authorized to make this request.** @return bool*/public function authorize(){return true;}/*** Get the validation rules that apply to the request.** @return array*/public function rules(){return [// 'phone' => 'required|regex:/^1[34578]\d{9}$/|unique:users',];}
}

app/Http/Controllers/CaptchasController.php


<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests\Api\CaptchaRequest;
class CaptchasController extends Controller
{public function store(CaptchaRequest $request, CaptchaBuilder $captchaBuilder){   // $key = 'captcha-'.str_random(15);$key = 'captcha-'.time();$phone = $request->phone;$captcha = $captchaBuilder->build();$expiredAt = now()->addMinutes(2);\Cache::put($key, ['phone' => $phone, 'code' => $captcha->getPhrase()], $expiredAt);$result = ['captcha_key' => $key,'expired_at' => $expiredAt->toDateTimeString(),'captcha_image_content' => $captcha->inline()];return $result;return $this->response->$result->setStatusCode(201);}
}

代码分析

use Gregwar\Captcha\CaptchaBuilder;
创建验证码实例: $captcha = $captchaBuilder->build();
获取验证码值: $captcha->getPhrase(); // abcd…
获取验证码图片的 base64: $captcha->inline() // base64 xxxxx

3). 代码分解

分析一下代码:

增加了 CaptchaRequest 要求用户必须通过手机号调用图片验证码接口。
controller 中,注入CaptchaBuilder,通过它的 build 方法,创建出来验证码图片
使用 getPhrase 方法获取验证码文本,跟手机号一同存入缓存。
返回 captcha_key,过期时间以及 inline 方法获取的 base64 图片验证码
这里给图片验证码设置为 2 分钟过期,

4).请求结果

在这里插入图片描述

在这里插入图片描述

http://www.lryc.cn/news/307728.html

相关文章:

  • iMazing3安全吗?好不好用?值不值得下载
  • 韩国突发:将批准比特币ETF
  • Kubernetes IoTDB系列 | IoTDB数据库同步|IoTDB数据库高可用
  • 重拾前端基础知识:CSS
  • 综合实战(volume and Compose)
  • 国际黄金价格要具体市况具体分析
  • 【python】0、超详细介绍:json、http
  • 可观测性在威胁检测和取证日志分析中的作用
  • win32com打开带密码excel
  • IntelliJ IDEA 的常用快捷键
  • C语言统计成绩
  • LVS做集群四层负载均衡的简单理解
  • 2.1_6 线程的实现方式和多线程模型
  • 4.5 MongoDB 文档存储
  • 什么是服务级别协议(SLA)?
  • 使用Python进行Sentinel-2 图像聚类
  • SNZ资本的首席信息官Gavin确认出席Hack .Summit() 2024香港开发者大会!
  • js里面有引用传递吗?
  • C 语言 math.h 库介绍
  • Eigen-Matrix矩阵
  • 蓝桥杯14届计算思维国赛U8组包含真题和答案
  • opencv内存溢出del释放变量 (python)
  • 【算法与数据结构】复杂度深度解析(超详解)
  • Upload-Labs-Linux1【CTF】
  • 搜维尔科技:OptiTrack 提供了性能最佳的动作捕捉平台
  • java设计模式之职责链模式
  • 连不上网的解决办法集--持续更新
  • Unity之PUN2插件实现多人联机射击游戏
  • 72_Pandas.DataFrame保存并读取带pickle的系列(to_pickle、read_pickle)
  • Redis哨兵模式和Redis Cluster模式