![](/img/trans.png)
[英]Increase X-RateLimit-Limit in laravel using throttle middleware
[英]Laravel Throttle Middleware: is there a callback / handler for it?
我想使用Laravel的Throttle Middleware ,但是我也想记录用户进行过多尝试的任何情况。
是否有任何回调/处理程序可以检测到并对其进行处理?
假设我有一条这样的路线:
Route::get('foo', array('before' => 'throttle:5,10', function () {
return 'You shall pass!';
}, ));
如果达到最大计数,它将触发此事件:
Illuminate\Auth\Events\Lockout
所以你大概可以听
protected $listen = [
...
'Illuminate\Auth\Events\Lockout' => [
'App\Listeners\LogLockout',
],
];
您可以扩展ThrottleRequests
类并重新声明其buildException
,或尝试在app/Exceptions/Handler.php
处理其异常( \\Symfony\\Component\\HttpKernel\\Exception\\HttpException
与\\Symfony\\Component\\HttpKernel\\Exception\\HttpException
Many Attempts。')。
仅在Illuminate\\Foundation\\Auth\\ThrottlesLogins
触发Illuminate\\Auth\\Events\\Lockout
事件,但是您仍然可以手动触发它。
这就是我这样做的方式:
app/Exceptions/Handler.php
添加以下use
语句: use Illuminate\Auth\Events\Lockout;
use Symfony\Component\HttpKernel\Exception\HttpException;
report
方法中: if ($exception instanceof HttpException) {
if ('Too Many Attempts.' == $exception->getMessage()) {
event(new Lockout(request()));
}
}
php artisan make:listener LogThrottleLimitReached
并将此代码添加到app/Listeners/LogThrottleLimitReached.php
文件的handle
方法中: $request = $event->request;
\Log::error(sprintf(
'Throttling rate limit reached. URL: %s, Body: %s, IP: %s',
$request->url(),
json_encode($request->all()),
$request->ip()
));
app/Providers/EventServiceProvider.php
注册该侦听器: use App\Listeners\LogThrottleLimitReached;
use Illuminate\Auth\Events\Lockout;
和
protected $listen = [
Lockout::class => [
LogThrottleLimitReached::class,
],
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.