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 相关文章推荐
phpMyAdmin下载、安装和使用入门教程
May 31 PHP
php 遍历显示文件夹下所有目录、所有文件的函数,没有分页的代码
Nov 14 PHP
PHP 程序员也要学会使用“异常”
Jun 16 PHP
php生成局部唯一识别码LUID的代码
Oct 06 PHP
smarty模板中拼接字符串的方法
Feb 14 PHP
PHP中可以自动分割查询字符的Parse_str函数使用示例
Jul 25 PHP
php中define用法实例
Jul 30 PHP
给WordPress中的留言加上楼层号的PHP代码实例
Dec 14 PHP
php 使用html5实现多文件上传实例
Oct 24 PHP
ThinkPHP实现简单登陆功能
Apr 28 PHP
laravel如何开启跨域功能示例详解
Aug 31 PHP
PHP实现对图片的反色处理功能【测试可用】
Feb 01 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
dede3.1分页文字采集过滤规则详说(图文教程)
2007/04/03 PHP
PHP中is_dir()函数使用指南
2015/05/08 PHP
php版微信返回用户text输入的方法
2016/11/14 PHP
php实现文件管理与基础功能操作
2017/03/21 PHP
关于ThinkPHP中的异常处理详解
2018/05/11 PHP
在JavaScript中获取请求的URL参数
2010/12/22 Javascript
jQuery动态创建html元素的常用方法汇总
2014/09/05 Javascript
使用js画图之画切线
2015/01/12 Javascript
jQuery实现鼠标滑过点击事件音效试听
2015/08/31 Javascript
基于JS实现省市联动效果代码分享
2016/06/06 Javascript
JavaScript中数组Array方法详解
2017/02/27 Javascript
基于VUE移动音乐WEBAPP跨域请求失败的解决方法
2018/01/16 Javascript
如何基于js判断浏览器版本
2020/02/20 Javascript
[01:51]2018年度CS GO最具人气外援-完美盛典
2018/12/16 DOTA
python脚本监控docker容器
2016/04/27 Python
python3中int(整型)的使用教程
2017/03/23 Python
python+mongodb数据抓取详细介绍
2017/10/25 Python
django上传图片并生成缩略图方法示例
2017/12/11 Python
Python实现制度转换(货币,温度,长度)
2019/07/14 Python
在Pytorch中使用样本权重(sample_weight)的正确方法
2019/08/17 Python
使用python写一个自动浏览文章的脚本实例
2019/12/05 Python
Python random模块制作简易的四位数验证码
2020/02/01 Python
零基础小白多久能学会python
2020/06/22 Python
哪种Python框架适合你?简单介绍几种主流Python框架
2020/08/04 Python
使用Python将语音转换为文本的方法
2020/08/10 Python
matplotlib常见函数之plt.rcParams、matshow的使用(坐标轴设置)
2021/01/05 Python
Python实现网络聊天室的示例代码(支持多人聊天与私聊)
2021/01/27 Python
做一个有道德的人演讲稿
2014/05/14 职场文书
2014年巴西世界杯口号
2014/06/05 职场文书
大学拉赞助协议书范文
2014/09/26 职场文书
涨价通知怎么写
2015/04/23 职场文书
优秀党员先进事迹材料2016
2016/02/29 职场文书
2016年离婚协议书范文
2016/03/18 职场文书
委托书范本格式
2019/04/18 职场文书
导游词之河北野三坡
2019/12/11 职场文书
简述Java中throw-throws异常抛出
2021/08/07 Java/Android