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 相关文章推荐
第四节--构造函数和析构函数
Nov 16 PHP
php开发过程中关于继承的使用方法分享
Jun 17 PHP
php设计模式 Visitor 访问者模式
Jun 28 PHP
php 读取文件头判断文件类型的实现代码
Aug 05 PHP
使用Discuz关键词服务器实现PHP中文分词
Mar 11 PHP
PHP FATAL ERROR: CALL TO UNDEFINED FUNCTION BCMUL()解决办法
May 04 PHP
php学习笔记之面向对象
Nov 08 PHP
php中explode函数用法分析
Nov 15 PHP
深入理解PHP中的count函数
May 31 PHP
php版微信小店API二次开发及使用示例
Nov 12 PHP
php 如何禁用eval() 函数实例详解
Dec 01 PHP
PHP实现的多维数组去重操作示例
Jul 21 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自定义大小验证码的方法详解
2013/06/07 PHP
php switch语句多个值匹配同一代码块应用示例
2014/07/29 PHP
php实现的用户查询类实例
2015/06/18 PHP
PHP实现的猴王算法(猴子选大王)示例
2018/04/30 PHP
一些常用的JS功能函数(2009-06-04更新)
2009/06/04 Javascript
jquery模拟按下回车实现代码
2011/09/20 Javascript
JS TextArea字符串长度限制代码集合
2012/10/31 Javascript
JavaScript中判断原生函数检查function是否是原生代码
2014/09/09 Javascript
javascript中DOM复选框选择用法实例
2015/05/14 Javascript
jQuery超酷平面式时钟效果代码分享
2020/03/30 Javascript
jquery原理以及学习技巧介绍
2015/11/11 Javascript
分享经典的JavaScript开发技巧
2015/11/21 Javascript
jQuery实现下拉菜单(内容为时间)的实时更新及图表的随动更新的方法
2016/07/07 Javascript
JS实现探测网站链接的方法【测试可用】
2016/11/08 Javascript
浅谈Vue.use的使用
2018/08/29 Javascript
15分钟上手vue3.0(小结)
2020/05/20 Javascript
Vue自定义多选组件使用详解
2020/09/08 Javascript
[02:14]DOTA2英雄基础教程 修补匠
2013/12/23 DOTA
[02:00]DAC2018主宣传片——龙征四海,剑问东方
2018/03/20 DOTA
[39:07]LGD vs VP 2018国际邀请赛淘汰赛BO3 第二场 8.21
2018/08/22 DOTA
[47:03]完美世界DOTA2联赛PWL S3 Galaxy Racer vs Phoenix 第二场 12.10
2020/12/13 DOTA
Python两个整数相除得到浮点数值的方法
2015/03/18 Python
Python中urllib+urllib2+cookielib模块编写爬虫实战
2016/01/20 Python
详解Django之auth模块(用户认证)
2018/04/17 Python
Python3中lambda表达式与函数式编程讲解
2019/01/14 Python
浅析Python __name__ 是什么
2020/07/07 Python
Python根据字典的值查询出对应的键的方法
2020/09/30 Python
美国宠物美容和宠物用品购物网站:Cherrybrook
2018/12/07 全球购物
光声世纪笔试题目
2012/08/25 面试题
教师找工作推荐信
2013/11/23 职场文书
省三好学生申请材料
2014/01/22 职场文书
保卫钓鱼岛口号
2014/06/20 职场文书
捐助倡议书
2015/01/19 职场文书
2019年中,最受大众欢迎的6本新书
2019/08/07 职场文书
Nginx使用X-Accel-Redirect实现静态文件下载的统计、鉴权、防盗链、限速等
2021/04/04 Servers
深入理解python多线程编程
2021/04/18 Python