您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Laravel如何實現登錄失敗次數限制,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
在用戶身份驗證的情況下,Laravel 具有內置的身份驗證系統。我們可以根據要求輕松修改它。身份驗證中包含的功能之一是Throttling.
為什么我們需要throttling保護?
基本上,throttling是用來保護暴力攻擊的。它將在一定時間內檢查登錄嘗試。在短登錄中,throttling會計算用戶或機器人嘗試失敗的登錄嘗試次數。
使用自定義登錄實現限制
默認情況下,在內置身份驗證控制器中實現限制。但是,如果我們需要實現它到自定義登錄呢?
實現自定義登錄限制非常容易。首先,我們必須將ThrottlesLogins trait包含到您的控制器中。
use Illuminate\Foundation\Auth\ThrottlesLogins;
現在,將此ThrottlesLogins trait 加到控制器中。
namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Foundation\Auth\ThrottlesLogins; class AuthController extends Controller { use ThrottlesLogins; ......
現在轉到用于對用戶進行身份驗證的方法。在我的例子中,我使用了 login() POST 方法。并粘貼以下代碼:
public function login(Request $request) { // Authenticate Inputs $request->validate([ 'username' => 'required', 'password' => 'required|min:6|max:18' ]); // If the class is using the ThrottlesLogins trait, we can automatically throttle // the login attempts for this application. We'll key this by the username and // the IP address of the client making these requests into this application. if (method_exists($this, 'hasTooManyLoginAttempts') && $this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); return $this->sendLockoutResponse($request); } .......
首先,我們驗證了用戶提交的輸入,然后實現了hasTooManyLoginAttempts() 方法。此方法將檢查用戶在某個時間是否執行過一定數量的失敗嘗試,然后系統將通過sendLockoutResponse() 方法阻止該用戶。
現在,我們必須通過incrementLoginAttempts()方法指示對ThrottlesLogins trait的失敗登錄嘗試。
if( Auth::attempt(['username' => $username, 'password' => $password]) ){ // Redirect to appropriate dashboard } else { // If the login attempt was unsuccessful we will increment the number of attempts // to login and redirect the user back to the login form. Of course, when this // user surpasses their maximum number of attempts they will get locked out. $this->incrementLoginAttempts($request); return redirect()->back() ->withInput($request->all()) ->withErrors(['error' => 'Please check your username / password.']); }
您還可以通過$maxAttempts和$decayMinutes屬性更改允許的最大嘗試次數和限制的分鐘數。在這里,您可以找到完整的代碼。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Foundation\Auth\ThrottlesLogins; class AuthController extends Controller { use ThrottlesLogins; /** * The maximum number of attempts to allow. * * @return int */ protected $maxAttempts = 5; /** * The number of minutes to throttle for. * * @return int */ protected $decayMinutes = 1; public function login(Request $request) { // Authenticate Inputs $request->validate([ 'username' => 'required', 'password' => 'required|min:6|max:18' ]); // If the class is using the ThrottlesLogins trait, we can automatically throttle // the login attempts for this application. We'll key this by the username and // the IP address of the client making these requests into this application. if (method_exists($this, 'hasTooManyLoginAttempts') && $this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); return $this->sendLockoutResponse($request); } $username = $request->username; $password = $request->password; if( Auth::attempt(['username' => $username, 'password' => $password]) ){ // Redirect to appropriate dashboard } else { // If the login attempt was unsuccessful we will increment the number of attempts // to login and redirect the user back to the login form. Of course, when this // user surpasses their maximum number of attempts they will get locked out. $this->incrementLoginAttempts($request); return redirect()->back() ->withInput($request->all()) ->withErrors(['error' => 'Please check your username / password.']); } } } Related Posts:
關于“Laravel如何實現登錄失敗次數限制”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。