gpt4 book ai didi

使用 JWT Auth 时 Laravel 5.3 节流登录

转载 作者:行者123 更新时间:2023-12-01 12:24:00 25 4
gpt4 key购买 nike

我正在为我的 Laravel + Angular 应用程序使用 JWT。我看到 Laravel 5.3 具有开箱即用的登录限制功能。但是在使用 JWT Auth 时如何让它工作呢?我有以下代码,但登录限制不起作用。尽管有无数次失败的尝试,我只收到 Invalid Login Details 错误,但它没有节流并显示 Too many logins 错误:

class LoginController extends Controller
{
use AuthenticatesUsers;

protected $maxLoginAttempts=5;
protected $lockoutTime=300;

public function login(Request $request)
{
$credentials = $request->only('email', 'password');

$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);

if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return response()->json(['error' => 'Too many logins'], 400);
}

try {
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'Invalid Login Details'], 401);
}
} catch (JWTException $e) {
// something went wrong
$this->incrementLoginAttempts($request);
return response()->json(['error' => 'Could Not Create Token'], 500);
}

// if no errors are encountered we can return a JWT
return response()->json(compact('token'));
}
}

如何使用 JWTAuth 使用此功能?有人能帮帮我吗?

最佳答案

这是因为您 JWTAuth 没有抛出异常来增加您的登录尝试。只需将 $this->incrementLoginAttempts($request); 放入 auth 失败的条件中,如下所示:

if (! $token = JWTAuth::attempt($credentials)) {
$this->incrementLoginAttempts($request);
return response()->json(['error' => 'Invalid Login Details'], 401);
}

关于使用 JWT Auth 时 Laravel 5.3 节流登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41654153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com