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 相关文章推荐
oracle资料库函式库
Oct 09 PHP
php中突破基于HTTP_REFERER的防盗链措施(stream_context_create)
Mar 29 PHP
我的php学习笔记(毕业设计)
Feb 21 PHP
PHP的异常处理类Exception的使用及说明
Jun 13 PHP
php结合ACCESS的跨库查询功能
Jun 12 PHP
实现WordPress主题侧边栏切换功能的PHP脚本详解
Dec 14 PHP
PHP文件上传处理案例分析
Oct 15 PHP
Android AsyncTack 异步任务实例详解
Nov 02 PHP
PHP基于SMTP协议实现邮件发送实例代码
Apr 27 PHP
PHP中常用的魔术方法
Apr 28 PHP
php实现的二叉树遍历算法示例
Jun 15 PHP
Laravel 的数据库迁移的方法
Jul 31 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生成静态页面分析 模板+缓存+写文件
2009/08/17 PHP
PHP三层结构(上) 简单三层结构
2010/07/04 PHP
PHP版国家代码、缩写查询函数代码
2011/08/14 PHP
PHP上传图片进行等比缩放可增加水印功能
2014/01/13 PHP
PHP输入输出流学习笔记
2015/05/12 PHP
php查询whois信息的方法
2015/06/08 PHP
Thinkphp3.2简单解决多文件上传只上传一张的问题
2017/09/26 PHP
php实现生成带二维码图片并强制下载功能
2018/02/24 PHP
php实现银联商务公众号+服务窗支付的示例代码
2019/10/12 PHP
JavaScript 程序编码规范
2010/11/23 Javascript
javascript判断是否按回车键并解决浏览器之间的差异
2014/05/13 Javascript
Node.js中创建和管理外部进程详解
2014/08/16 Javascript
如何在MVC应用程序中使用Jquery
2014/11/17 Javascript
原生JS和JQuery动态添加、删除表格行的方法
2015/05/28 Javascript
jquery实现Ctrl+Enter提交表单的方法
2015/07/21 Javascript
Bootstrap导航简单实现代码
2017/03/06 Javascript
Vuex模块化实现待办事项的状态管理
2017/03/15 Javascript
jQuery实现的简单对话框拖动功能示例
2018/06/05 jQuery
JS/HTML5游戏常用算法之碰撞检测 包围盒检测算法详解【圆形情况】
2018/12/13 Javascript
JavaScript碎片—函数闭包(模拟面向对象)
2019/03/13 Javascript
[00:17]DOTA2荣耀之路5:It’s a disastah!
2018/05/28 DOTA
python使用os.listdir和os.walk获得文件的路径的方法
2017/12/16 Python
python实现跨excel的工作表sheet之间的复制方法
2018/05/03 Python
浅谈pytorch和Numpy的区别以及相互转换方法
2018/07/26 Python
python实现反转部分单向链表
2018/09/27 Python
TensorFlow:将ckpt文件固化成pb文件教程
2020/02/11 Python
德国综合购物网站:OTTO
2018/11/13 全球购物
财务人员个人自荐信范文
2013/09/26 职场文书
网络教育自我鉴定
2013/11/01 职场文书
乌鸦喝水教学反思
2014/02/07 职场文书
大学班级干部的自我评价分享
2014/02/10 职场文书
公司寄语大全
2014/04/10 职场文书
客户经理竞聘演讲稿
2014/05/15 职场文书
网络文明传播志愿者活动方案
2014/08/20 职场文书
银行党的群众路线教育实践活动对照检查材料
2014/09/25 职场文书
刑事辩护授权委托书格式
2014/10/13 职场文书