llmbackend/app/Rules/ThrottleAuthToken.php
Jethro Lin c44c25d86f 4. [API 详细说明](#4-api-详细说明)
- [4.1 认证 API](#41-认证-api)
     - [4.1.1 获取访问令牌](#411-获取访问令牌)
   - [4.2 客户用户 API](#42-客户用户-api)
     - [4.2.1 发送提示词请求](#421-发送提示词请求)
   - [4.3 管理员 API](#43-管理员-api)
     - [4.3.1 LLM 提供商管理](#431-llm-提供商管理)
       - [4.3.1.1 新增 LLM 提供商](#4311-新增-llm-提供商)
       - [4.3.1.2 修改 LLM 提供商](#4312-修改-llm-提供商)
       - [4.3.1.3 删除 LLM 提供商](#4313-删除-llm-提供商)
       - [4.3.1.4 获取 LLM 提供商列表](#4314-获取-llm-提供商列表)
2024-12-04 12:01:56 +08:00

28 lines
754 B
PHP

<?php
declare(strict_types=1);
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\RateLimiter;
use Closure;
class ThrottleAuthToken implements Rule
{
private const MAX_ATTEMPTS = 5; // 最大尝试次数
private const DECAY_MINUTES = 1; // 重置时间(分钟)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$key = 'auth_token_' . $value;
if (RateLimiter::tooManyAttempts($key, self::MAX_ATTEMPTS)) {
$seconds = RateLimiter::availableIn($key);
$fail("请求过于频繁,请在 {$seconds} 秒后重试。");
return;
}
RateLimiter::hit($key, self::DECAY_MINUTES * 60);
}
}