Laravel 5.7中的限制登录尝试

问题描述 投票:0回答:1

我有具有自定义登录名的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";
    }
}
php laravel laravel-5 laravel-5.7
1个回答
3
投票

您可以通过两种方式来做

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

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

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

2。使用内置在Trait ThrottlesLogins

首先在loginController中添加ThrottlesLogins trait,然后在登录方法中添加此行

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);

© www.soinside.com 2019 - 2024. All rights reserved.