tourze / captcha-challenge-bundle
图片验证码模块
Installs: 6
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.1
- ext-gd: *
- gregwar/captcha: ^1.2
- nzo/url-encryptor-bundle: ^6.4
- psr/simple-cache: ^1.0|^2.0|^3.0
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-foundation: ^6.4
- symfony/http-kernel: ^6.4
- symfony/routing: ^6.4
- symfony/uid: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/json-rpc-core: ^0.0.6
- tourze/json-rpc-lock-bundle: 0.1.*
- tourze/json-rpc-log-bundle: 0.0.*
- tourze/symfony-routing-auto-loader-bundle: 0.0.*
Requires (Dev)
- maglnet/composer-require-checker: ^4
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-05-24 17:08:20 UTC
README
这是一个Symfony Bundle,提供图片验证码功能,用于网站安全防护。
特性
- 生成随机验证码,并将挑战值存储在缓存中
- 提供图片验证码的图像生成和显示
- 提供验证码校验和一次性消费功能
- 支持JSON-RPC接口
安装
使用Composer安装:
composer require tourze/captcha-challenge-bundle
配置
在您的Symfony应用程序中注册Bundle:
// config/bundles.php return [ // ... Tourze\CaptchaChallengeBundle\CaptchaChallengeBundle::class => ['all' => true], ];
确保配置了环境变量:
# .env
LOGIN_CHALLENGE_TYPE=captcha # 启用验证码功能,设置为'null'则禁用
使用方法
生成验证码和获取图片
// 在控制器中 use Tourze\CaptchaChallengeBundle\Service\ChallengeService; class YourController extends AbstractController { public function generateCaptcha(ChallengeService $challengeService): Response { // 生成验证码 $challengeKey = $challengeService->generateChallenge(); // 获取验证码图片URL $imageUrl = $challengeService->generateChallengeCaptchaImageUrl($challengeKey); return $this->json([ 'challengeKey' => $challengeKey, 'imageUrl' => $imageUrl, ]); } }
验证用户输入的验证码
// 在控制器中 use Tourze\CaptchaChallengeBundle\Service\ChallengeService; class YourController extends AbstractController { public function verifyCode( Request $request, ChallengeService $challengeService ): Response { $challengeKey = $request->request->get('challengeKey'); $userInput = $request->request->get('captchaCode'); // 验证并消费验证码 $isValid = $challengeService->checkAndConsume($challengeKey, $userInput); if (!$isValid) { return $this->json(['success' => false, 'message' => '验证码错误']); } return $this->json(['success' => true]); } }
技术细节
- 验证码使用5位随机数字生成
- 验证码在缓存中保存5分钟
- 验证成功后,会立即从缓存中删除,防止重复使用
- 使用 GD 库生成图片,支持反OCR功能
依赖
- PHP 8.1+
- GD 扩展
- Symfony 6.4 框架
- PSR-16 缓存实现
- Gregwar/Captcha 库
许可
本Bundle使用MIT许可证。详情请参阅 LICENSE 文件。