限流類(Throttler)提供了一種非常簡單的方法,可以將用戶要執(zhí)行的活動限制為在設(shè)定的時間段內(nèi)只能進行一定次數(shù)的嘗試。 這最常用于對 API 進行速率限制,或限制用戶針對表單進行的嘗試次數(shù),以幫助防止暴力攻擊。 該類可用于你根據(jù)設(shè)置的時間來進行限制的操作。
插件地址: http://wtbis.cn/plugin/36
限流類
緩存依據(jù)的是Support\Cache的 instance()
, 其他類只要是實現(xiàn) get($key, $default = null)
, set($key, $value, $ttl = null)
, delete($key)
funtion就行.
composer require nsp-team/webman-throttler
默認(rèn) 開啟全局中間件限流
return [ '' => [ \NspTeam\WebmanThrottler\Middleware\ThrottlerMiddleware::class, ] ];
你也可以啟用路由中間件,控制接口請求速率限制
例如:Route::group('/sys/user', static function () { Route::post('/test', [User::class, 'test']); })->middleware([ \NspTeam\WebmanThrottler\Middleware\ThrottlerMiddleware::class ]);
想要限制速度,可以提升 seconds 值,它表示 “桶”完全重新裝滿所需的時間,capacity 表示桶的容量; 或者減少capacity的值,這樣同樣可以減少訪問量
報錯
"Entry \"NspTeam\WebmanThrottler\Throttle\Throttler\" cannot be resolved: Parameter $cache of __construct() has no value defined or guessable\nFull definition:\nObject (\n class = NspTeam\WebmanThrottler\Throttle\Throttler\n lazy = false\n __construct(\n $cache = #UNDEFINED#\n )\n)
最近工作太忙,在寫java,非常不好意思,這個插件寫的比較早,可能跟框架有不適配了。給大家造成困擾了,源碼很少,原理一看就明白,開發(fā)者可以自行改動適配,歡迎 PR