Php Laravel:在自定义登录控制器中使用油门

Php Laravel:在自定义登录控制器中使用油门,php,laravel,throttling,Php,Laravel,Throttling,这是我的登录控制器功能 use ThrottlesLogins; protected $maxLoginAttempts = 3; protected $lockoutTime = 300; public function login(Request $request) { if ($this->hasTooManyLoginAttempts($request)) { $this->fire

这是我的登录控制器功能

   use ThrottlesLogins;
   protected $maxLoginAttempts = 3;
   protected $lockoutTime = 300;

 public function login(Request $request)
    {       

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

        $validator = Validator::make(Input::all() , ['credential' => 'required|min:2|max:255', 'password' => 'required|string|min:8', ]);
        $cred = $request->credential;
        $pw = $request->password;
        $remember = (Input::has('remember')) ? true : false;

        if (filter_var($cred, FILTER_VALIDATE_EMAIL))
          {
          if (Auth::guard('customers')->attempt(['email' => $cred, 'password' => $pw, 'verified' => 1], $remember))
            {
            return redirect()->route('front');
            }
            else
            {
            return redirect()->route('customer-login-page')->with('error', 'Your credentials do not match');
            }
          }
          else
          {
            if (Auth::guard('customers')->attempt(['contact' => $cred, 'password' => $pw], $remember))
             {
              return redirect()->intended(route('front'));
             }
            else
            {
            return redirect()->route('customer-login-page')->with('error', 'Your credentials do not match');
            }
          }

    }



  protected function hasTooManyLoginAttempts(Request $request)
    {
       return $this->limiter()->tooManyAttempts(
           $this->throttleKey($request), $this->maxLoginAttempts, $this->lockoutTime
       );
    }
它不起作用了。我尝试了三次以上失败的登录尝试,但仍然没有被阻止。及 即使我发布了正确的凭据,登录和重定向仍然有效,但是当我检查请求时,我得到了一个

302发现错误


在网络选项卡中,您需要通过调用
$this->incrementLoginAttents($request)
()让trait知道您正在进行登录尝试。您可以在现有油门检查后立即拨打此电话:

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

$this->incrementLoginAttempts($request);

// other code
试一试

分期付款

use ThrottlesLogins;

非常感谢。我试过这个。它起作用了。但是在尝试之后,我得到了一个空白页,这不是一条有趣的消息
sendLockoutResponse($request)
似乎抛出了一个
ValidationException
,这意味着
return
将什么也不做。也许您可以使用
try{}catch(ValidationException$e){}
来代替它?虽然这并不能解释trait中的原始代码是如何工作的。好吧,在仔细考虑之后,
ValidationException
似乎被内部异常处理程序捕获并相应地转换为
响应。异常前面的
返回值
似乎没有任何效果。因此,我最好的猜测是,您对异常处理程序进行了一些更改,或者其他内容被破坏。请看一下你的日志。
use ThrottlesLogins;