ggedde/spry-rate-limits

Spry的速率限制提供者

安装: 20

依赖: 1

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:spry-provider

1.0.9 2022-04-27 02:53 UTC

This package is auto-updated.

Last update: 2024-09-27 07:50:59 UTC


README

这是一个Spry提供者,用于为您的路由添加速率限制或为所有请求添加全局速率限制

安装

composer require ggedde/spry-rate-limits

激活

为了激活速率限制,您需要在您的配置中初始化提供者并在配置中设置速率限制设置。

Spry配置示例

$config->rateLimits = [
  'driver' => 'file',
  'fileDirectory' =>  __DIR__.'/rate_limits',
];

Spry::addHook('initialized', 'Spry\\SpryProvider\\SpryRateLimits::initiate');

* 默认情况下,速率限制是不活跃的,但您可以通过向Spry配置添加“default”设置或为每个路由单独添加限制来设置全局速率限制。

添加全局速率限制

$config->rateLimits = [
  'driver' => 'file',
  'fileDirectory' => __DIR__.'/rate_limits',
  'excludeTests' => false,
  'default' => [
      'by' => 'ip',
      'limit' => 10,
      'within' => 1,
      'hook' => 'configure',
      'excludeTests' => false
  ]
];

按路由添加限制

$config->routes = [
    '/auth/login' => [
        'label' => 'Auth Login',
        'controller' => 'Auth::login',
        'access' => 'public',
        'methods' => 'POST',
        'limits' => [
            'by' => 'ip',
            'limit' => 1,
            'within' => 3,
            'excludeTests' => false
        ],
        'params' => [
            'email' => [
                'required' => true,
                'type' => 'string',
            ],
            'password' => [
                'required' => true,
                'type' => 'string',
            ],
        ],
    ],
];

全局设置

速率限制设置

添加自己的速率限制(通过)键

默认的by键是ip,但很多时候这并不是最佳选择。因此,您可以添加自己的键和值,并过滤速率限制以更改要检查的值。

Spry::addFilter('spryRateLimitKeys', function($keys){
  $keys['my_key'] = 'some_unique_value';
  return $keys;
});

示例:从Srpy的getAuth()方法检索值。

Spry::addHook('setAuth', function($auth){
    Spry::addFilter('spryRateLimitKeys', function($keys) use ($auth){
        $keys['user_id'] = $auth->user_id;
        $keys['account_id'] = $auth->account_id;
        return $keys;
    });
});

扩展组件示例

public static function setup()
{
    Spry::addHook('setAuth', function($auth){
        Spry::addFilter('spryRateLimitKeys', [__CLASS__, 'myMethod'], $auth);
    });
}

public static myMethod($keys, $meta, $auth) 
{
    $keys['user_id'] = $auth->user_id;
    $keys['account_id'] = $auth->account_id;
    return $keys;
}

在您的路由中使用您的新键

$config->routes = [
    '/data/get' => [
        'label' => 'Get Data',
        'controller' => 'SomeComponent::get',
        'access' => 'public',
        'methods' => 'GET',
        'limits' => [
            'limit' => 15,
            'within' => 15,
            'by' => 'user_id'
        ],
        'params' => [
            'id' => [
                'type' => 'string',
            ],
        ],
    ],
    '/users/get' => [
        'label' => 'Get User',
        'controller' => 'SomeUserComponent::get',
        'access' => 'public',
        'methods' => 'GET',
        'limits' => [
            'limit' => 1,
            'within' => 1,
            'by' => 'account_id'
        ],
        'params' => [
            'id' => [
                'type' => 'string',
            ],
        ],
    ],
];