madmikeyb / throttleable
暂时禁止IP地址在您的Laravel应用程序内执行某些操作
0.5
2020-01-27 16:08 UTC
Requires
- php: >=7.0
- illuminate/database: ~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0.0|~7.0.0
- illuminate/events: ~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0.0|~7.0.0
- illuminate/http: ~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0.0|~7.0.0
- nesbot/carbon: ~1.24|~2.0
README
根据用户IP地址限制对应用程序的请求。
- 设置IP地址可以发起的请求数量阈值。
- 限制在一段时间后过期。
- 每个IP地址的限制是唯一的。
- 通过
config/throttleable.php
进行配置
安装
使用Composer引入包
composer require madmikeyb/throttleable
注意:如果您正在使用Laravel 5.5,则以下步骤是不必要的。Laravel Throttleable支持Laravel 包发现。
在 app/config/app.php
中包含服务提供者。
'providers' => [ ... MadMikeyB\Throttleable\Providers\ThrottleableServiceProvider::class, ],
迁移
您必须运行以下命令来发布 迁移
php artisan vendor:publish --provider="MadMikeyB\Throttleable\Providers\ThrottleableServiceProvider" --tag="migrations" && php artisan migrate
配置
Throttleable支持可选配置。
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="MadMikeyB\Throttleable\Providers\ThrottleableServiceProvider" --tag="config"
发布后,config/throttleable.php
配置文件 包含
<?php return [ /** * Number of attempts permitted to a single * IP address before being throttled. */ 'attempt_limit' => 10, /** * The datetime metric to use for expirations * Available options are hour, day or week. */ 'expiry_metric' => 'week', /** * The number of hours, days or weeks to * keep a throttle valid for. */ 'expiry_timelimit' => 1 ];
这些只是默认值,如果需要可以在特定情况下覆盖。
示例用法
只需在控制器中导入Throttle模型。
<?php namespace App\Http\Controllers; use MadMikeyB\Throttleable\Models\Throttle;
然后,在您想要限制的任何方法中,创建一个新的 Throttle
实例。该类所需的最小参数是 Illuminate\Http\Request
实例。
Throttle
的 check()
方法返回一个布尔值,指示IP地址是否已被限制。
public function create(Request $request) { $throttle = new Throttle($request->instance()); if (!$throttle->check()) { alert()->error('Sorry, you have made too many requests. Please try again later.'); return back(); } }
注意。此 alert()
辅助函数由 uxweb/sweet-alert 提供,不包括在此包中。
完整示例
<?php namespace App\Http\Controllers; use App\Comment; use MadMikeyB\Throttleable\Models\Throttle; class CommentsController { public function store(Request $request) { $throttle = new Throttle($request->instance()); if (!$throttle->check()) { alert()->error('Sorry, you have made too many requests. Please try again later.'); return back(); } // save comment here Comment::create($request->all()); alert()->success('Comment Created!'); return back(); } }
根据具体情况覆盖配置
在某些情况下,您可能希望覆盖配置文件中设置的默认尝试和时限。例如,创建评论的用户可能不会像尝试登录管理面板的人那样快速被限制。
Throttle
模型的构造函数的第二个和第三个参数分别是 attempt_limit
和 expiry_weeks
配置变量。
如果您需要覆盖 config/throttleable.php
中设置的默认值,可以按以下方式操作
public function store(Request $request) { $attemptLimit = 10000; $expiryWeeks = 52; $throttle = new Throttle($request->instance(), $attemptLimit, $expiryWeeks); if (!$throttle->check()) { alert()->error('Sorry, you have made too many requests. Please try again later.'); return back(); } // save comment here Comment::create($request->all()); alert()->success('Comment Created!'); return back(); }
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。