![](/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.