簡體   English   中英

Laravel:在自定義登錄控制器中使用節流閥

[英]Laravel: using throttle in a custom Login controller

這是我的登錄控制器功能

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

沒用 我已經嘗試了3次以上的失敗登錄嘗試,但仍然沒有受到限制。 並且即使我發布了正確的憑據,登錄和重定向仍然有效,但是當我檢查請求時,我得到了

302 FOUND錯誤

在網絡標簽中

您需要通過調用$this->incrementLoginAttempts($request)請參見代碼 ),讓trait知道您正在執行登錄嘗試。 您可以在進行現有油門檢查后立即撥打此電話:

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

$this->incrementLoginAttempts($request);

// other code

嘗試

use Illuminate\Foundation\Auth\ThrottlesLogins;

恢復原狀

use ThrottlesLogins;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM