webman框架限流中間件

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