danharrin/livewire-rate-limiting

将速率限制应用于Laravel Livewire操作。

v1.3.1 2024-05-06 09:10 UTC

This package is auto-updated.

Last update: 2024-09-06 09:53:17 UTC


README

Package banner

Tests passing Laravel v8.x, v9.x, v10.x, v11.x PHP 8.0+

此包允许您将速率限制应用于Laravel Livewire操作。这对于限制登录尝试和其他暴力攻击、减少垃圾邮件等非常有用。

安装

您可以使用Composer将此包安装到您的应用程序中

composer require danharrin/livewire-rate-limiting

此包需要至少Laravel v8.x版本,当引入速率限制改进时。

此包已测试支持fileredis缓存驱动程序,但不支持array

用法

DanHarrin\LivewireRateLimiting\WithRateLimiting特质应用到您的Livewire组件

<?php

namespace App\Http\Livewire\Login;

use DanHarrin\LivewireRateLimiting\WithRateLimiting;
use Livewire\Component;

class Login extends Component
{
    use WithRateLimiting;
    
    // ...
}

在本例中,我们将设置submit操作的速率限制。

用户每分钟只能调用此操作10次。

如果超出此限制,将抛出TooManyRequestsException异常。用户将看到验证错误,并被告知何时解除限制

<?php

namespace App\Http\Livewire\Login;

use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
use DanHarrin\LivewireRateLimiting\WithRateLimiting;
use Illuminate\Validation\ValidationException;
use Livewire\Component;

class Login extends Component
{
    use WithRateLimiting;
    
    public function submit()
    {
        try {
            $this->rateLimit(10);
        } catch (TooManyRequestsException $exception) {
            throw ValidationException::withMessages([
                'email' => "Slow down! Please wait another {$exception->secondsUntilAvailable} seconds to log in.",
            ]);
        }
        
        // ...
    }
}

API参考

组件方法

use DanHarrin\LivewireRateLimiting\WithRateLimiting;

/**
 * Rate limit a Livewire method, `$maxAttempts` times every `$decaySeconds` seconds.
 * 
 * @throws DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException
 */
$this->rateLimit(
    $maxAttempts, // The number of times that the rate limit can be hit in the given decay period.
    $decaySeconds = 60, // The length of the decay period in seconds. By default, this is a minute.
    $method, // The name of the method that is being rate limited. By default, this is set to the method that `$this->rateLimit()` is called from.
);

/**
 * Hit a method's rate limiter without consequence.
 */
$this->hitRateLimiter(
    $method, // The name of the method that is being rate limited. By default, this is set to the method that `$this->hitRateLimiter()` is called from.
    $decaySeconds = 60, // The length of the decay period in seconds. By default, this is a minute.
);

/**
 * Clear a method's rate limiter.
 */
$this->clearRateLimiter(
    $method, // The name of the method that is being rate limited. By default, this is set to the method that `$this->clearRateLimiter()` is called from.
);

异常

use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;

try {
    $this->rateLimit(10);
} catch (TooManyRequestsException $exception) {
    $exception->component; // Class of the component that the rate limit was hit within.
    $exception->ip; // IP of the user that has hit the rate limit.
    $exception->method; // Name of the method that has hit the rate limit.
    $exception->minutesUntilAvailable; // Number of minutes until the rate limit is lifted, rounded up.
    $exception->secondsUntilAvailable; // Number of seconds until the rate limit is lifted.
}

需要帮助?

🐞 如果您在此包中发现错误,请提交详细问题,并等待帮助。

🤔 如果您有疑问或功能请求,请开始新的讨论

🔐 如果您在此包中发现漏洞,请查阅我们的安全策略