在 Laravel 5.7 中限制登录尝试

Awa*_*zer 4 php laravel laravel-5 laravel-5.7

我有带有自定义登录的 Laravel 5.7 项目。在重定向页面等待 2 或 3 分钟等之后,如何让 Laravel 接受三次登录尝试?

public function loginPost(LoginRequest $request)
{
    if (Auth::attempt(array('user_name' => $request->user_name, 'password' => $request->user_pass)))
    {
        if(Auth::check())
            return redirect('/');
        else
            return back();
    }
    else
    {
        return "login faled call administrator";
    }
}
Run Code Online (Sandbox Code Playgroud)

Jig*_*sar 14

你可以通过两种方式做

  1. throttle middleware例如在路由中添加laravel bulit

    Route::post("/user/login","LoginController@login")->middleware("throttle:10,2");
    
    Run Code Online (Sandbox Code Playgroud)

它将每 2 分钟发送 10 个请求

2.使用内置 Trait ThrottlesLogins

首先添加ThrottlesLogins traitloginController 和 login 方法中的这一行

if ($this->hasTooManyLoginAttempts($request)) {
    $this->fireLockoutEvent($request);
    return $this->sendLockoutResponse($request);
}

if(attempt()) {
    $this->clearLoginAttempts($request);
}else {
  $this->incrementLoginAttempts($request);
}
Run Code Online (Sandbox Code Playgroud)

如果尝试成功,则在尝试方法中添加此行

$this->clearLoginAttempts($request);

else 登录失败,然后在 else 条件中添加此行

$this->incrementLoginAttempts($request);


Mah*_*joy 5

打开你的登录控制器

App\Http\Controllers\Auth\LoginController.php
Run Code Online (Sandbox Code Playgroud)

并粘贴它

protected $maxAttempts = 1;
protected $decayMinutes = 1;
Run Code Online (Sandbox Code Playgroud)