日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

e盾网络验证源码_Laravel [mews/captcha] 图片验证码

發布時間:2024/9/27 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 e盾网络验证源码_Laravel [mews/captcha] 图片验证码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 安裝配置

1.1 使用 composer 安裝 [mews/captcha] 擴展

composer require mews/captcha項目根目錄 composer.json -> require 會新增一行(我的 laravel 版本是 5.6)"mews/captcha": "^3.0"

1.2 config/app.php 添加相應代碼

'providers'=>[ // ... Mews\Captcha\CaptchaServiceProvider::class]'aliases'=>[ // ... 'Captcha' => Mews\Captcha\Facades\Captcha::class]

1.3 發布配置文件(不發布即使用默認配置)

// 生成 config/captcha.php 文件php?artisan?vendor:publish

1.4 (可選)配置自己的驗證碼

return [ // 生成的驗證碼字符集 'characters' => ['2', '3', '4', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'm', 'n', 'p', 'q', 'r', 't', 'u', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'M', 'N', 'P', 'Q', 'R', 'T', 'U', 'X', 'Y', 'Z'], 'default' => [ 'length' => 4, // 驗證碼字符長度 'width' => 160, // 圖片寬度 'height' => 46, // 圖片高度 'quality' => 90, // 圖片質量 'math' => false, // 數學規則 'expire' => 60, // 過期時間 ], // ...]

2 生成驗證碼

2.1 Return Image

return captcha();// orCaptcha::create();

返回如下:

看一下源碼,這里我加了整體的邏輯注釋,詳細的內部方法實現可以訪問項目內的 /vendor/mews/captcha/src/Captcha.php

/** * Create captcha image * * @param string $config * @param bool $api * @return array|mixed * @throws Exception */public function create(string $config = 'default', bool $api = false){ $this->backgrounds = $this->files->files(__DIR__ . '/../assets/backgrounds'); // 背景圖片文件 $this->fonts = $this->files->files($this->fontsDirectory); // 字體文件 // 根據不同的 laravel 版本配置字體文件路徑 if (version_compare(app()->version(), '5.5.0', '>=')) { $this->fonts = array_map(function ($file) { /* @var File $file */ return $file->getPathName(); }, $this->fonts); } $this->fonts = array_values($this->fonts); //reset fonts array index // 加載 config/captcha.php 的配置項 $this->configure($config); $generator = $this->generate(); // 生成驗證碼文本,并session存儲 $this->text = $generator['value']; // 根據配置,創建驗證碼圖片 $this->canvas = $this->imageManager->canvas( $this->width, $this->height, $this->bgColor ); if ($this->bgImage) { $this->image = $this->imageManager->make($this->background())->resize( $this->width, $this->height ); $this->canvas->insert($this->image); } else { $this->image = $this->canvas; } if ($this->contrast != 0) { $this->image->contrast($this->contrast); } $this->text(); // 將文本寫入圖片 $this->lines(); // 添加干擾線 if ($this->sharpen) { $this->image->sharpen($this->sharpen); // 圖片銳化 } if ($this->invert) { $this->image->invert(); // 應該是對文本進行顛倒處理 } if ($this->blur) { $this->image->blur($this->blur); // 模糊 } if ($api) { Cache::put('captcha_record_' . $generator['key'], $generator['value'], $this->expire); // 緩存 } // 如果 $api 是 true,則返回二進制編碼格式;如果是 false,則直接輸出一張圖片 return $api ? [ 'sensitive' => $generator['sensitive'], 'key' => $generator['key'], 'img' => $this->image->encode('data-url')->encoded ] : $this->image->response('png', $this->quality);}

2.2 Return URL

返回 url 地址

captcha_src();// orCaptcha::src('default');

源碼如下:

/** * Generate captcha image source * * @param string $config * @return string */public function src(string $config = 'default'): string{ return url('captcha/' . $config) . '?' . $this->str->random(8);}

2.3 Return HTML

返回一個攜帶 src 的 img 標簽

captcha_img();// orCaptcha::img();

源碼如下:

/** * Generate captcha image html tag * * @param string $config * @param array $attrs * $attrs -> HTML attributes supplied to the image tag where key is the attribute and the value is the attribute value * @return string */public function img(string $config = 'default', array $attrs = []): string{ $attrs_str = ''; foreach ($attrs as $attr => $value) { if ($attr == 'src') { //Neglect src attribute continue; } $attrs_str .= $attr . '="' . $value . '" '; } return new HtmlString('');}

3 驗證 : 使用 laravel validatesRequest

3.1 session 模式(代碼來自官方文檔)

// [your site path]/Http/routes.phpRoute::any('captcha-test', function() { if (request()->getMethod() == 'POST') { $rules = ['captcha' => 'required|captcha']; $validator = validator()->make(request()->all(), $rules); if ($validator->fails()) { echo '

Incorrect!

'; } else { echo '

Matched :)

'; } } $form = ''; $form .= ''; $form .= '

' . captcha_img() . '

'; $form .= ' $form .= '

Check

'; $form .= ''; return $form;});

3.2 無狀態模式 : 當你從請求網址拿到 key 和 img,可以使用下面方法來驗證(代碼來自官方文檔)

//key is the one that you got from json response// fix validator// $rules = ['captcha' => 'required|captcha_api:'. request('key')];$rules = ['captcha' => 'required|captcha_api:'. request('key') . ',default'];$validator = validator()->make(request()->all(), $rules);if ($validator->fails()) { return response()->json([ 'message' => 'invalid captcha', ]);} else { //do the job}

這里我們追一下 check_api() 方法,還是在 captcha.php 里:

/** * Captcha check * * @param string $value * @param string $key * @param string $config * @return bool */public function check_api($value, $key, $config = 'default'): bool{ // pull : 從緩存中獲取并刪除 if (!Cache::pull('captcha_record_' . $key)) { return false; } // 加載 config/captcha.php 的配置項 $this->configure($config); if($this->encrypt) $key = Crypt::decrypt($key); // 如果加密就解密 return $this->hasher->check($value, $key); // 返回 hash 驗證結果,使用 password_verify() 函數,如果未輸入,則直接返回 false}

官方 packagist 文檔:?https://packagist.org/packages/mews/captcha

總結

以上是生活随笔為你收集整理的e盾网络验证源码_Laravel [mews/captcha] 图片验证码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。