【限流插件】控制API 进行速率限制限流,防止暴力攻击

Maoxp

限流类(Throttler)提供了一种非常简单的方法,可以将用户要执行的活动限制为在设定的时间段内只能进行一定次数的尝试。 这最常用于对 API 进行速率限制,或限制用户针对表单进行的尝试次数,以帮助防止暴力攻击。 该类可用于你根据设置的时间来进行限制的操作。

插件地址: https://www.workerman.net/plugin/36

限流类

  • 全局中间件,整个应用接口限流,
  • 路由中间件,某些功能接口请求速率限制

缓存依据的是Support\Cache的 instance(), 其他类只要是实现 get($key, $default = null), set($key, $value, $ttl = null), delete($key) funtion就行.

项目地址:https://github.com/nsp-team/webman-throttler

安装

composer require nsp-team/webman-throttler

基本用法

默认 开启全局中间件限流

return [
'' => [
\NspTeam\WebmanThrottler\Middleware\ThrottlerMiddleware::class,
]
];

你也可以启用路由中间件,控制接口请求速率限制
例如:

Route::group('/sys/user', static function () {
Route::post('/test', [User::class, 'test']);
})->middleware([
\NspTeam\WebmanThrottler\Middleware\ThrottlerMiddleware::class
]);
2470 5 1
5个评论

Maoxp

截图 想要限制速度,可以提升 seconds 值,它表示 “桶”完全重新装满所需的时间,capacity 表示桶的容量; 或者减少capacity的值,这样同样可以减少访问量

  • 暂无评论
jinggge

报错了是怎么回事Error: Class 'Symfony\Component\Cache\Adapter\RedisAdapter' not found in
E:\WorkSpace\webman\vendor\workerman\webman-framework\src\support\Cache.php:35<br />

  • Maoxp 2022-12-02

    框架support目录变更过😅,现在在framework下面了,需要重新适配

ikun

报错
"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)

  • 暂无评论
ikun

这个插件大家别用了 程序健壮性有问题, 作者也失联了

  • ikun 2022-11-16

    大佬 专业

  • liziyu 2022-11-16

    @yzh52521 关注大佬许久了,因为你太优秀了👍。
    本人的点不良嗜好,这种 yzh52521/webman-throttle 包名不喜欢,如yzh52521这样的,喜欢名称为纯英文字母的包名,哈哈。

  • yzh52521 2022-11-16

    可以使用 yuanzh/webman-throttle

Maoxp

最近工作太忙,在写java,非常不好意思,这个插件写的比较早,可能跟框架有不适配了。给大家造成困扰了,源码很少,原理一看就明白,开发者可以自行改动适配,欢迎 PR

  • 暂无评论
年代过于久远,无法发表评论

Maoxp

684
积分
0
获赞数
0
粉丝数
2021-04-21 加入
🔝