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


Posted in PHP onAugust 26, 2020

在用户身份验证的情况下,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登录失败次数限制的文章就介绍到这了,更多相关Laravel登录失败次数限制内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

PHP 相关文章推荐
深入php define()函数以及defined()函数的用法详解
Jun 05 PHP
基于PHP读取csv文件内容的详解
Jun 18 PHP
多个PHP中文字符串截取函数
Nov 12 PHP
php环境无法上传文件的解决方法
Apr 30 PHP
使用pthreads实现真正的PHP多线程(需PHP5.3以上版本)
May 05 PHP
PHP文件缓存类示例分享
Jan 30 PHP
php中stdClass的用法分析
Feb 27 PHP
PHP使用strtotime获取上个月、下个月、本月的日期
Dec 30 PHP
PHP与jquery实时显示网站在线人数实例详解
Dec 02 PHP
php lcg_value与mt_rand生成0~1随机小数的效果对比分析
Apr 05 PHP
Thinkphp 空操作、空控制器、命名空间(详解)
May 05 PHP
如何用PHP websocket实现网页实时聊天
May 26 PHP
利用PHP计算有多少小于当前数字的数字方法示例
Aug 26 #PHP
one.php 多项目、函数库、类库 统一为一个版本的方法
Aug 24 #PHP
PHP执行普通shell命令流程解析
Aug 24 #PHP
PHP连接SQL server数据库测试脚本运行实例
Aug 24 #PHP
解决PHP Opcache 缓存刷新、代码重载出现无法更新代码的问题
Aug 24 #PHP
WordPress免插件实现面包屑导航的示例代码
Aug 20 #PHP
VSCode+PHPstudy配置PHP开发环境的步骤详解
Aug 20 #PHP
You might like
php将gd生成的图片缓存到memcache的小例子
2013/06/05 PHP
PHP安全的URL字符串base64编码和解码
2014/06/19 PHP
destoon在各个服务器下设置URL Rewrite(伪静态)的方法
2014/06/21 Servers
php+mysql数据库查询实例
2015/01/21 PHP
Yii框架结合sphinx,Ajax实现搜索分页功能示例
2016/10/18 PHP
php取出数组单个值的方法
2018/03/12 PHP
PHP操作Postgresql封装类与应用完整实例
2018/04/24 PHP
javaScript - 如何引入js代码
2021/03/09 Javascript
caller和callee的区别介绍及演示结果
2013/03/10 Javascript
JS Replace 全部替换字符的用法小结
2013/12/24 Javascript
js监听滚动条滚动事件使得某个标签内容始终位于同一位置
2014/01/24 Javascript
JS取request值以及自动执行使用示例
2014/02/24 Javascript
extjs每个组件要设置唯一的ID否则会出错
2014/06/15 Javascript
jquery实现平滑的二级下拉菜单效果
2015/08/26 Javascript
详解JavaScript的AngularJS框架中的表达式与指令
2016/03/05 Javascript
用AngularJS来实现监察表单按钮的禁用效果
2016/11/02 Javascript
jQuery网页定位导航特效实现方法
2016/12/19 Javascript
nodeJS微信分享
2017/12/20 NodeJs
Bootstrap导航菜单点击后无法自动添加active的处理方法
2018/08/10 Javascript
开发用到的js封装方法(20种)
2018/10/12 Javascript
[02:38]DOTA2亚洲邀请赛小组赛精彩集锦:Wings完美团击溃对手
2017/03/29 DOTA
Python的Django框架中消息通知的计数器实现教程
2016/06/13 Python
Python三级菜单的实例
2017/09/13 Python
Python使用Windows API创建窗口示例【基于win32gui模块】
2018/05/09 Python
详解python分布式进程
2018/10/08 Python
Python使用turtle库绘制小猪佩奇(实例代码)
2020/01/16 Python
Jupyter Notebook折叠输出的内容实例
2020/04/22 Python
纯HTML5+CSS3制作生日蛋糕代码
2016/11/16 HTML / CSS
澳大利亚首个在线预订旅游网站:Wotif
2017/07/19 全球购物
火锅店营销方案
2014/02/26 职场文书
科技馆观后感
2015/06/08 职场文书
Node.js实现断点续传
2021/06/23 Javascript
Nginx进程调度问题详解
2021/09/25 Servers
python接口测试返回数据为字典取值方式
2022/02/12 Python
搭建Yolov5服务器
2022/04/30 Servers
python热力图实现的完整实例
2022/06/25 Python