当前位置:首页 > CMS教程 > 其它CMS > 列表

Laravel登录失败次数限制的实现方法

发布:smiling 来源: PHP粉丝网  添加日期:2022-03-23 11:33:41 浏览: 评论:0 

这篇文章主要给大家介绍了关于Laravel登录失败次数限制的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

在用户身份验证的情况下,Laravel 具有内置的身份验证系统,我们可以根据要求轻松修改它,身份验证中包含的功能之一是Throttling.

为什么我们需要throttling保护?

基本上,throttling是用来保护暴力攻击的。它将在一定时间内检查登录尝试。在短登录中,throttling会计算用户或机器人尝试失败的登录尝试次数。

使用自定义登录实现限制

默认情况下,在内置身份验证控制器中实现限制。但是,如果我们需要实现它到自定义登录呢?

实现自定义登录限制非常容易。首先,我们必须将ThrottlesLogins trait包含到您的控制器中。

use Illuminate\Foundation\Auth\ThrottlesLogins;

现在,将此ThrottlesLogins trait 加到控制器中。

  1. namespace App\Http\Controllers; 
  2. use Illuminate\Http\Request; 
  3. use Illuminate\Foundation\Auth\ThrottlesLogins; 
  4. class AuthController extends Controller 
  5.  use ThrottlesLogins; 
  6.  ...... 

现在转到用于对用户进行身份验证的方法,在我的例子中,我使用了 login() POST 方法,并粘贴以下代码:

  1. public function login(Request $request
  2.  // Authenticate Inputs 
  3.  $request->validate([ 
  4.  'username' => 'required',  
  5.  'password' => 'required|min:6|max:18' 
  6.  ]); 
  7.  // If the class is using the ThrottlesLogins trait, we can automatically throttle 
  8.  // the login attempts for this application. We'll key this by the username and 
  9.  // the IP address of the client making these requests into this application. 
  10.  if (method_exists($this'hasTooManyLoginAttempts') && 
  11.  $this->hasTooManyLoginAttempts($request)) { 
  12.  $this->fireLockoutEvent($request); 
  13.  return $this->sendLockoutResponse($request); 
  14.  } 
  15.    
  16.  ....... 

首先,我们验证了用户提交的输入,然后实现了hasTooManyLoginAttempts() 方法,此方法将检查用户在某个时间是否执行过一定数量的失败尝试,然后系统将通过sendLockoutResponse()  方法阻止该用户。

现在,我们必须通过incrementLoginAttempts()方法指示对ThrottlesLogins trait的失败登录尝试。

  1. if( Auth::attempt(['username' => $username'password' => $password]) ){ 
  2.  // Redirect to appropriate dashboard  
  3. else { 
  4.  // If the login attempt was unsuccessful we will increment the number of attempts 
  5.  // to login and redirect the user back to the login form. Of course, when this 
  6.  // user surpasses their maximum number of attempts they will get locked out. 
  7.  $this->incrementLoginAttempts($request); 
  8.  return redirect()->back() 
  9.   ->withInput($request->all()) 
  10.   ->withErrors(['error' => 'Please check your username / password.']); 

您还可以通过$maxAttempts和$decayMinutes属性更改允许的最大尝试次数和限制的分钟数,在这里,您可以找到完整的代码。

  1. <?php 
  2. namespace App\Http\Controllers; 
  3. use Illuminate\Http\Request; 
  4. use Illuminate\Foundation\Auth\ThrottlesLogins; 
  5. class AuthController extends Controller 
  6.  use ThrottlesLogins; 
  7.  /** 
  8.   * The maximum number of attempts to allow. 
  9.   * 
  10.   * @return int 
  11.   */ 
  12.  protected $maxAttempts = 5; 
  13.  /** 
  14.   * The number of minutes to throttle for. 
  15.   * 
  16.   * @return int 
  17.   */ 
  18.  protected $decayMinutes = 1; 
  19.  public function login(Request $request
  20.  { 
  21.   // Authenticate Inputs 
  22.   $request->validate([ 
  23.    'username' => 'required',  
  24.    'password' => 'required|min:6|max:18' 
  25.   ]); 
  26.   // If the class is using the ThrottlesLogins trait, we can automatically throttle 
  27.   // the login attempts for this application. We'll key this by the username and 
  28.   // the IP address of the client making these requests into this application. 
  29.   if (method_exists($this'hasTooManyLoginAttempts') && 
  30.    $this->hasTooManyLoginAttempts($request)) { 
  31.    $this->fireLockoutEvent($request); 
  32.    return $this->sendLockoutResponse($request); 
  33.   } 
  34.   $username = $request->username; 
  35.   $password = $request->password; 
  36.     
  37.   if( Auth::attempt(['username' => $username'password' => $password]) ){ 
  38.    // Redirect to appropriate dashboard  
  39.   } 
  40.   else { 
  41.    // If the login attempt was unsuccessful we will increment the number of attempts 
  42.    // to login and redirect the user back to the login form. Of course, when this 
  43.    // user surpasses their maximum number of attempts they will get locked out. 
  44.    $this->incrementLoginAttempts($request); 
  45.    return redirect()->back() 
  46.     ->withInput($request->all()) 
  47.     ->withErrors(['error' => 'Please check your username / password.']); 
  48.   } 
  49.  } 
  50. Related Posts:

Tags: Laravel登录失败次数限制

分享到: