Laravel 5.5 ThrottleRequest中间件
问题描述:
有没有人有关于如何在Laravel 5.5中实现ThrottleRequest
中间件的信息?Laravel 5.5 ThrottleRequest中间件
我不是在decayMinutes
参数的意思很清楚,特别是:https://laravel.com/api/5.5/Illuminate/Routing/Middleware/ThrottleRequests.html
我知道如何将它应用到路线,我只是我不知道什么reaosnable参数会。
答
decayMinutes
- 这是您的限制内的时间将被计数。技术上的限制是缓存中的TTL(生存时间)$decayMinutes * 60
秒的值,每次命中时都会增加。当TTL超过值时,将自动在缓存中销毁,新的点击数将启动。
看看RateLimit::hit()的代码。这是很清楚的:
/**
* Increment the counter for a given key for a given decay time.
*
* @param string $key
* @param float|int $decayMinutes
* @return int
*/
public function hit($key, $decayMinutes = 1)
{
$this->cache->add(
$key.':timer', $this->availableAt($decayMinutes * 60), $decayMinutes
);
$added = $this->cache->add($key, 0, $decayMinutes);
$hits = (int) $this->cache->increment($key);
if (! $added && $hits == 1) {
$this->cache->put($key, 1, $decayMinutes);
}
return $hits;
}
如果您想通过10次命中每5分钟,以限制某些活动,比decayMinutes
必须为5
答
我了解decayMinutes
作为保留时间。对于实际情况,如果您想尝试使用错误的密码进行登录,但如果他尝试了11次,则用户将被阻止达到decayMinutes
中指定的分钟数。如果您指定10分钟作为您的decayMinutes
,用户被阻止10分钟
从具有阅读源代码,这是我的理解了。 – fubar
这是错误的答案。 'decayMinutes'(在Laravel 5.5)不是一个阻止时间。 –
我猜登录块时间就是一个例子,它的工作方式大部分与节流相同。只需将“登录尝试”替换为“请求的唯一网址”即可。如果您没有为$ decayMinutes分钟发出任何页面或API请求,那么油门计数器将重置。 –