5

我有自定义登录的 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";
    }
}
4

5 回答 5

17

你可以通过两种方式来做

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

    Route::post("/user/login","LoginController@login")->middleware("throttle:10,2");
    

它将每 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);
}

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

$this->clearLoginAttempts($request);

否则登录失败然后在其他条件下添加此行

$this->incrementLoginAttempts($request);

于 2018-10-05T20:15:50.673 回答
5

打开你的登录控制器

App\Http\Controllers\Auth\LoginController.php

并粘贴它

protected $maxAttempts = 1;
protected $decayMinutes = 1;
于 2020-03-24T10:42:01.937 回答
1

对于 Laravel 8 开发人员,您不需要提供 trait 或任何东西,因为它是一个内置功能,您所要做的就是将中间件链接到您想要保护的路线,并使用如下限制率

Route::post("/user/login",[LoginController::class,'login'])->middleware("throttle:10,2");

与@Jignesh Joisar 解释一样

于 2021-09-19T12:45:42.227 回答
0

你需要ThrottlesLogins在你的控制器中使用特征,然后你可以通过属性控制它maxAttempts/decayMinutes

....
class TagController extends Controller
{
  use ThrottlesLogins;

  protected $maxAttempts = 5;
  protected $decayMinutes = 1;
...
于 2020-10-01T08:12:05.647 回答
0

打开 App\Http\Controllers\Auth\AuthController.php 并添加以下行:

protected $maxLoginAttempts = 10; 
protected $lockoutTime = 120; 
于 2021-01-27T18:38:52.890 回答