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 相关文章推荐
无JS,完全php面向过程数据分页实现代码
Aug 27 PHP
PHP模板引擎Smarty的缓存使用总结
Apr 24 PHP
浅析PHP编程中10个最常见的错误
Aug 08 PHP
PHP利用func_get_args和func_num_args函数实现函数重载实例
Nov 12 PHP
Laravel模板引擎Blade中section的一些标签的区别介绍
Feb 10 PHP
8个PHP数组面试题
Jun 23 PHP
什么是PEAR?什么是PECL?PHP中两个容易混淆的概念解释
Jul 01 PHP
php中删除、清空session的方式总结
Oct 09 PHP
php实现当前页面点击下载文件的简单方法
Sep 22 PHP
Laravel接收前端ajax传来的数据的实例代码
Jul 20 PHP
PHP弱类型语言中类型判断操作实例详解
Aug 10 PHP
详解thinkphp5+swoole实现异步邮件群发(SMTP方式)
Oct 13 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 sprintf()函数让你的sql操作更安全
2008/07/23 PHP
php 将bmp图片转为jpg等其他任意格式的图片
2009/06/29 PHP
php自定义函数call_user_func和call_user_func_array详解
2011/07/14 PHP
如何判断php数组的维度
2013/06/10 PHP
php实现编辑和保存文件的方法
2015/07/20 PHP
记录一次排查PHP脚本执行卡住的问题
2016/12/27 PHP
thinkPHP5.0框架事务处理操作简单示例
2018/09/07 PHP
说说掌握JavaScript语言的思想前提想学习js的朋友可以看看
2009/04/01 Javascript
基于jquery实现的移入页面上空文本框时,让它变为焦点,移出清除焦点
2011/07/26 Javascript
在Windows上安装Node.js模块的方法
2011/09/25 Javascript
实现局部遮罩与关闭原理及代码
2013/02/04 Javascript
关于js中for in的缺陷浅析
2013/12/02 Javascript
使用javascript为网页增加夜间模式
2014/01/26 Javascript
网站内容禁止复制和粘贴、另存为的js代码
2014/02/26 Javascript
node.js中的buffer.Buffer.isEncoding方法使用说明
2014/12/14 Javascript
jQuery中extend函数的实现原理详解
2015/02/03 Javascript
使用AngularJS实现可伸缩的页面切换的方法
2015/06/19 Javascript
zTree插件下拉树使用入门教程
2016/04/11 Javascript
原生nodejs使用websocket代码分享
2018/04/07 NodeJs
typescript nodejs 依赖注入实现方法代码详解
2019/07/21 NodeJs
Vue+ElementUI使用vue-pdf实现预览功能
2019/11/26 Javascript
详解vue-router 动态路由下子页面多页共活的解决方案
2019/12/22 Javascript
javascript实现移动端触屏拖拽功能
2020/07/29 Javascript
javascript实现京东快递单号的查询效果
2020/11/30 Javascript
python操作摄像头截图实现远程监控的例子
2014/03/25 Python
python中os和sys模块的区别与常用方法总结
2017/11/14 Python
浅谈Django的缓存机制
2018/08/23 Python
Python3的介绍、安装和命令行的认识(推荐)
2018/10/20 Python
解决Python运行文件出现out of memory框的问题
2018/12/03 Python
魅力惠奢品线上平台:MEI.COM
2016/11/29 全球购物
学前教育专业毕业生自荐信
2013/10/03 职场文书
光荣入党自我鉴定
2014/01/22 职场文书
募捐倡议书怎么写
2014/05/14 职场文书
镇党政领导班子民主生活会思想汇报
2014/10/11 职场文书
检讨书格式范文
2015/05/07 职场文书
Python机器学习应用之基于线性判别模型的分类篇详解
2022/01/18 Python