nw / request-limit-bundle
此包提供了一种轻量级的方法,限制用户在特定时间段内对某些操作的访问
v1.0
2020-12-16 08:38 UTC
Requires
- php: ^7
- ext-memcached: *
- ext-pdo: *
- doctrine/common: ~2.4
- symfony/symfony: >=3.0
Requires (Dev)
- doctrine/dbal: ~2.4
- doctrine/doctrine-bundle: ~1.4
- doctrine/orm: ~2.4,>=2.4.5
- friendsofphp/php-cs-fixer: *
- phpspec/phpspec: *
- phpunit/phpunit: ~5.0
This package is auto-updated.
Last update: 2024-09-17 02:34:02 UTC
README
RequestLimitBundle
此包为限制用户对某些控制器在指定时间范围内的访问提供了一个简单解决方案。
此功能可用于您需要以下情况的不同场景
- 防止洪水 - 推送不相关数据给用户;
- 防止用户频繁访问特定端点等。
安装
- 通过以下方式安装包
composer require nw/request-limit-bundle
- 注册包
在 app/AppKernel.php 中注册包,在Symfony版本低于 4.0 之前
public function registerBundles() { $bundles = [ // ... , new NW\RequestLimitBundle\NWRequestLimitBundle() ]; // ... return $bundles; }
在 config/bundles.php 中注册包,当Symfony版本为 4.0 或更高时
return [ //... other bundles NW\RequestLimitBundle\NWRequestLimitBundle::class => ['all' => true] ];
- 根据您想要使用的提供商配置该包。默认情况下,我们提供了Memcached和MySQL提供商。有关配置选项,请参阅下面的文档。
如果您想使用其他存储,您可以实现自己的提供商。
- 指定
restriction_time(以秒为单位)
nw_request_limit: #... options for provider configuration restriction_time: 5 # 5 seconds
用法
在您的操作中,添加以下行以通过某些特定的应用程序用户对象(例如,用户ID、用户IP等)限制访问
$artifact = 'e.g. get user id or IP here'; $this->get('nw.request_limit.restrictor')->blockBy($artifact);
这将根据您的配置限制用户访问指定的时间段(例如,5秒)。