Jus*_*tin 5 php brute-force laravel
是否可以使用Laravel对条件用户进行身份验证来防止暴力攻击?
这个PHP的答案建议在数据库中添加两列(TimeOfLastFailedLogin和NumberOfFailedAttempts),然后在每次登录尝试时检查这些值.
以下是使用条件对用户进行身份验证的Laravel语法:
if (Auth::attempt(array('email' => $email, 'password' => $password, 'active' => 1)))
{
// The user is active, not suspended, and exists.
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使用条件参数来检查指定时间段内的尝试次数?例如,在过去60秒内少于3个请求.
您可以创建与下面的类一样简单的内容,以帮助您防止:
class Login {
public function attempt($credentials)
{
if ( ! $user = User::where('email' => $credentials['email'])->first())
{
//throw new Exception user not found
}
$user->login_attempts++;
if ($user->login_attempts > 2)
{
if (Carbon::now()->diffInSeconds($user->last_login_attempt) < 60)
{
//trow new Exception to wait a while
}
$user->login_attempts = 0;
}
if ( ! Auth::attempt($credentials))
{
$user->last_login_attempt = Carbon::now();
$user->save();
//trow new Exception wrong password
}
$user->login_attempts = 0;
$user->save();
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
或者你可以使用像Sentry一样的包,它可以控制你的限制.Sentry是开源的.
我知道这是一个老问题,但由于它在 Google 上排名很高,我想澄清一下 ThrottlesLogins 特征自 Laravel 5.1 以来就已经存在,并且确实可以防止暴力攻击。
默认情况下,它通过 AuthenticatesUser 特征包含在 Auth\LoginController 中。
文档: https: //laravel.com/docs/5.6/authentication#login-throtdling
默认行为示例(请参阅方法“登录”):https://github.com/laravel/framework/blob/5.6/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php
因此,如果您使用 Laravel 附带的默认登录控制器,那么登录限制的处理将自动完成。