webman框架限流中間件

v1.1.0
版本
2023-09-13
版本更新時(shí)間
1996
安裝
16
star
簡介
聲明:本庫提取于 think-throttle v1.3.x
通過本中間件可限定用戶在一段時(shí)間內(nèi)的訪問次數(shù),可用于保護(hù)接口防爬防爆破的目的。
安裝
composer require yzh52521/webman-throttle
使用
開啟
插件以中間件的方式進(jìn)行工作,因此它的開啟與其他中間件一樣,例如在全局中間件中使用:
<?php
//cat config/middleware.php
return [
// 全局中間件
'' => [
// ... 這里省略其它中間件
app\middleware\Throttle::class,
]
];
配置說明
<?php
// cat config/plugin/yzh52521/throttle/app.php
// 中間件配置
return [
// 緩存鍵前綴,防止鍵值與其他應(yīng)用沖突
'prefix' => 'throttle_',
// 緩存的鍵,true 表示使用來源ip (request->getRealIp(true))
'key' => true,
// 要被限制的請求類型, eg: GET POST PUT DELETE HEAD
'visit_method' => ['GET'],
// 設(shè)置訪問頻率,例如 '10/m' 指的是允許每分鐘請求10次。值 null 表示不限制,
// eg: null 10/m 20/h 300/d 200/300
'visit_rate' => '100/m',
// 響應(yīng)體中設(shè)置速率限制的頭部信息,含義見:https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting
'visit_enable_show_rate_limit' => true,
// 訪問受限時(shí)返回的響應(yīng)( type: null|callable )
'visit_fail_response' => function (Throttle $throttle, Request $request, int $wait_seconds): Response {
return response('Too many requests, try again after ' . $wait_seconds . ' seconds.', 429);
},
/*
* 設(shè)置節(jié)流算法,組件提供了四種算法:
* - CounterFixed :計(jì)數(shù)固定窗口
* - CounterSlider: 滑動(dòng)窗口
* - TokenBucket : 令牌桶算法
* - LeakyBucket : 漏桶限流算法
*/
'driver_name' => CounterFixed::class,
//webman默認(rèn)使用 symfony/cache作為cache組件(http://wtbis.cn/doc/webman/db/cache.html)
'cache_drive' => support\Cache::class,
//使用ThinkCache
//'cache_drive' => think\facade\Cache::class,
];
當(dāng)配置項(xiàng)滿足以下條件任何一個(gè)時(shí),不會限制訪問頻率:
key === false || key === null || visit_rate === null
其中 key 用來設(shè)置緩存鍵的, 而 visit_rate 用來設(shè)置訪問頻率,單位可以是秒,分,時(shí),天。例如:1/s, 10/m, 98/h, 100/d , 也可以是 100/600 (600 秒內(nèi)最多 100 次請求)。
靈活定制
示例一:針對用戶個(gè)體做限制,key的值可以設(shè)為函數(shù),該函數(shù)返回新的緩存鍵值(需要Session支持),例如:
'key' => function($throttle, $request) {
return $request->session()->get('user_id');
},
實(shí)例二:在回調(diào)函數(shù)里針對不同控制器和方法定制生成key,中間件會進(jìn)行轉(zhuǎn)換:
'key' => function($throttle, $request) {
return implode('/', [
$request->controller,
$request->action,
$request->getRealIp($safe_mode=true)
]);
},
//'key' => 'controller/action/ip' //上述配置的快捷實(shí)現(xiàn)
示例三:在閉包內(nèi)修改本次訪問頻率或臨時(shí)更換限流策略:(PS:此示例需要本中間件在路由中間件后啟用,這樣預(yù)設(shè)的替換功能才會生效。)
'key' => function($throttle, $request) {
$throttle->setRate('5/m'); // 設(shè)置頻率
$throttle->setDriverClass(CounterSlider::class);// 設(shè)置限流策略
return true;
},
示例四:在路由中獨(dú)立配置
Route::any('/api/driver-ocr', [ app\api\controller\Ocr::class, 'driver'])->middleware([
app\middleware\Throttle::class
]);
正確使用路由向中間件傳參例子(Webman-framework >= 1.3.16)
Route::group('/path', function() {
//路由注冊
...
})->setParams(['visit_rate' => '20/m',
...
...
])->middleware(\app\middleware\Throttle::class);
注意:
1、Webman-framework >= 1.3.16 支持路由向中間件傳參 使用 $route->param() 方法
2、禁止訪問時(shí),throttle 默認(rèn)是拋出 HttpResponseException, 當(dāng)前插件場景下將正常響應(yīng)一個(gè)httpResponse(即不會主動(dòng):Throw Exception),特殊需求請?jiān)?"visit_fail_response"