avtonom / limit-number-calls-bundle
用于 Symfony 2 的防御器,针对大量请求。它可以防止多次处理同一个值。守卫,旨在防止暴力破解和字典攻击。
v1.1.2
2019-02-11 11:23 UTC
Requires
- php: >=5.3.2
- predis/predis: ~1.1
- snc/redis-bundle: ~2.0
- symfony/dependency-injection: ~2.3|3.*
This package is not auto-updated.
Last update: 2024-09-17 13:44:56 UTC
README
用于 Symfony 2 的防御器,针对大量请求。它可以防止多次处理同一个值。守卫,旨在防止暴力破解和字典攻击。
页面包: https://github.com/Avtonom/limit-number-calls-bundle
功能
- 多种阻止规则
- 将规则组合成组
- 具有 Symfony Security Voter
- 准备好的控制台命令(CLI)以查看、添加和删除统计信息和锁定
- 易于扩展
- 使用快速 pRedis
- 支持高达微秒级
也许在将来
- 使控制台命令更方便地执行工作
- 以简单的版本暴露应用程序的核心,该版本不依赖于 Symfony
- 小编辑。我将非常感谢您的帮助
列出控制台命令(CLI)
- avtonom:limit-calls:add - 在统计中添加请求的执行(不建立锁定。但检查阻止)
- avtonom:limit-calls:block - 将值添加到值锁定列表中
- avtonom:limit-calls:clear - 删除值的统计信息
- avtonom:limit-calls:rules - 打开用于检查锁定的当前单词列表
- avtonom:limit-calls:status - 查看被阻止的值列表和统计信息列表
详细信息,在命令名称后添加 "-h"
规则设置参数
- enabled: true - [可选] 关闭规则
- maximum_number: 1 - 值的最大请求数量
- time_period: 60000000 # 微秒(1s = 1000000 微秒)- 允许执行指定数量的操作的周期
- blocking_duration: 600 # 秒(1m = 60s)- [可选] 阻止持续时间
- group: sms_group 或 [sms_group, other_group] - [可选] 在列表或多个组中关联的列表或组
- subject_class: Avtonom*****\ObjectInterface - 获取要运行 Symfony Security Voter 的类或接口
- subject_method: getParameter 或 [getParameter, phone] 或 [ [getParameter, phone], [getParameter, ip] ]- [可选] 获取对象属性值的方法或方法
安装
在你的项目根目录中运行以下命令,假设你的项目已设置 composer
composer.phar require avtonom/limit-number-calls-bundle ~1.1
切换 ~1.1 为最新的标签。
将包添加到 app/AppKernel.php
$bundles( ... new Snc\RedisBundle\SncRedisBundle(), new Avtonom\LimitNumberCallsBundle\AvtonomLimitNumberCallsBundle(), ... );
配置选项(config.yaml)
snc_redis: clients: default: type: predis logging: true # OPTIONAL alias: snc_redis_lnc dsn: redis:// options: # OPTIONAL throw_errors: true # OPTIONAL avtonom_limit_number_calls: voter_default: false # OPTIONAL. default true - include %avtonom_limit_number_calls.voter.class% rules: "%avtonom_limit_number_calls.rules%" # REQUIRED
配置选项(parameters.yaml)
parameters: avtonom_limit_number_calls.rules: sms_1m_10_rule: time_period: 60000000 # microsecond ( 1m = 60s * 1000 000 microsecond ) maximum_number: 10 blocking_duration: 600 # second ( 1m = 60s ) group: sms_group subject_class: *****\ObjectInterface subject_method: [getParameter, phone] sms_1m_rule: time_period: 60000000 # microsecond ( 1m = 60s * 1000 000 microsecond ) maximum_number: 1 blocking_duration: 600 # second ( 1m = 60s ) group: sms_group subject_class: *****\ObjectInterface subject_method: - [getParameter, phone] - [getParameter, text] sms_30m_30_rule: time_period: 1800000000 # microsecond ( 30m = 1m * 30 = 30 * 60s * 1000 000 microsecond ) maximum_number: 30 blocking_duration: 86400 # second ( 1d = 86400 second = 25h * 60m * 60s ) group: [sms_group, other] subject_class: *****\ObjectInterface subject_method: [getParameter, phone] sms_30m_rule: time_period: 1800000000 # microsecond ( 30m = 1m * 30 = 30 * 60s * 1000 000 microsecond ) maximum_number: 3 blocking_duration: 86400 # second ( 1d = 86400 second = 25h * 60m * 60s ) group: [sms_group, other] subject_class: *****\ObjectInterface subject_method: - [getParameter, phone] - [getParameter, text] test_minimum: time_period: 1800 maximum_number: 3 subject_class: *****\Object test_minimum_disabled: enabled: true time_period: 1800 maximum_number: 3 subject_class: *****\Object
使用
使用规则或规则组的名称来检查是否超出限制
# for Symfony > 3.0 if (!$this->get('security.authorization_checker')->isGranted('sms_group', $Object)) { throw new \Exception('Too Many Requests', 429); } # for Symfony < 3.0 if (!$this->get('security.context')->isGranted('sms_1m_rule', $Object)) { throw new \Exception('Too Many Requests', 429); }
需要帮助?
- 如果你发现了一个错误,请创建一个问题