nw/request-limit-bundle

此包提供了一种轻量级的方法,限制用户在特定时间段内对某些操作的访问

安装: 1

依赖: 0

建议者: 0

安全: 0

星标: 2

关注者: 1

分支: 1

公开问题: 0

语言:Twig

类型:symfony-bundle

v1.0 2020-12-16 08:38 UTC

This package is auto-updated.

Last update: 2024-09-17 02:34:02 UTC


README

SensioLabsInsight Build Status Scrutinizer Code Quality Maintainability

RequestLimitBundle

此包为限制用户对某些控制器在指定时间范围内的访问提供了一个简单解决方案。

此功能可用于您需要以下情况的不同场景

  • 防止洪水 - 推送不相关数据给用户;
  • 防止用户频繁访问特定端点等。

安装

  1. 通过以下方式安装包
    composer require nw/request-limit-bundle
  1. 注册包

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]
];
  1. 根据您想要使用的提供商配置该包。默认情况下,我们提供了Memcached和MySQL提供商。有关配置选项,请参阅下面的文档。

如果您想使用其他存储,您可以实现自己的提供商。

  1. 指定 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秒)。