|
|
|
@ -13,6 +13,7 @@ use Illuminate\Http\Request; |
|
|
|
use Illuminate\Support\Facades\App; |
|
|
|
use Illuminate\Support\Facades\Auth; |
|
|
|
use Illuminate\Support\Facades\Hash; |
|
|
|
use Illuminate\Support\Facades\RateLimiter; |
|
|
|
use Illuminate\Support\Facades\Validator; |
|
|
|
use Illuminate\Validation\ValidationException; |
|
|
|
use Tymon\JWTAuth\Facades\JWTAuth; |
|
|
|
@ -28,6 +29,22 @@ class AuthController extends Controller |
|
|
|
*/ |
|
|
|
protected OperationLogService $logService; |
|
|
|
|
|
|
|
/** |
|
|
|
* 最大允许的失败尝试次数 |
|
|
|
*/ |
|
|
|
protected function maxAttempts(): int |
|
|
|
{ |
|
|
|
return 5; // 5次失败后锁定 |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 锁定时间(分钟) |
|
|
|
*/ |
|
|
|
protected function decayMinutes(): int |
|
|
|
{ |
|
|
|
return 60; // 锁定60秒 |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 构造函数 |
|
|
|
* @param ApiResponseService $responseService |
|
|
|
@ -51,7 +68,21 @@ class AuthController extends Controller |
|
|
|
public function login(Request $request): JsonResponse |
|
|
|
{ |
|
|
|
$credentials = $request->only('username', 'password'); |
|
|
|
$throttleKey = strtolower($request->input('username')) . '|' |
|
|
|
. $request->ip(); |
|
|
|
try { |
|
|
|
if (RateLimiter::tooManyAttempts( |
|
|
|
$throttleKey, |
|
|
|
$this->maxAttempts() |
|
|
|
) |
|
|
|
) { |
|
|
|
$seconds = RateLimiter::availableIn($throttleKey); |
|
|
|
throw new \Exception( |
|
|
|
__validation('auth.count1') . ' ' . ceil( |
|
|
|
$seconds / $this->decayMinutes() |
|
|
|
) . ' ' . __validation('auth.count2') |
|
|
|
); |
|
|
|
} |
|
|
|
$validator = Validator::make($request->all(), [ |
|
|
|
'username' => 'required', |
|
|
|
'password' => 'required' |
|
|
|
@ -93,8 +124,10 @@ class AuthController extends Controller |
|
|
|
'token' => $token, |
|
|
|
]); |
|
|
|
} catch (ValidationException|CustomException $e) { |
|
|
|
RateLimiter::hit($throttleKey, $this->decayMinutes()); |
|
|
|
throw $e; |
|
|
|
} catch (\Exception $e) { |
|
|
|
RateLimiter::hit($throttleKey, $this->decayMinutes()); |
|
|
|
$m_prefix = __('admin.login') . __('admin.failed'); |
|
|
|
return $this->responseService->systemError($m_prefix . ':' . $e->getMessage()); |
|
|
|
} |
|
|
|
|