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数组
Oct 09 PHP
Win9x/ME下Apache+PHP安装配置
Oct 09 PHP
用PHP和ACCESS写聊天室(二)
Oct 09 PHP
php 数组的合并、拆分、区别取值函数集
Feb 15 PHP
PHP验证码函数代码(简单实用)
Sep 29 PHP
php中隐形字符65279(utf-8的BOM头)问题
Aug 16 PHP
PHP实现绘制3D扇形统计图及图片缩放实例
Oct 01 PHP
php实现的mysqldb读写分离操作类示例
Feb 07 PHP
PHP编程实现阳历转换为阴历的方法实例
Aug 08 PHP
kindeditor 加入七牛云上传的实例讲解
Nov 12 PHP
php生成word并下载代码实例
Mar 15 PHP
Linux下源码包安装Swoole及基本使用操作图文详解
Apr 02 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
MYSQL数据库初学者使用指南
2006/11/16 PHP
php中理解print EOT分界符和echo EOT的用法区别小结
2010/02/21 PHP
ThinkPHP中的create方法与自动令牌验证实例教程
2014/08/22 PHP
Sublime里直接运行PHP配置方法
2014/11/28 PHP
javascript的对话框详解与参数
2007/03/08 Javascript
JS图片浏览组件PhotoLook的公开属性方法介绍和进阶实例代码
2010/11/09 Javascript
仅img元素创建后不添加到文档中会执行onload事件的解决方法
2011/07/31 Javascript
关于JavaScript与HTML的交互事件
2013/04/12 Javascript
JavaScript的事件绑定(方便不支持js的时候)
2013/10/01 Javascript
JS通过ajax动态读取xml文件内容的方法
2015/03/24 Javascript
jQuery 监控键盘一段时间没输入
2016/04/22 Javascript
jQuery实用小技巧_输入框文字获取和失去焦点的简单实例
2016/08/25 Javascript
Bootstrap3 多选和单选框(checkbox)
2016/12/29 Javascript
微信小程序 tabs选项卡效果的实现
2017/01/05 Javascript
利用vue.js插入dom节点的方法
2017/03/15 Javascript
vue项目中使用tinymce编辑器的步骤详解
2018/09/11 Javascript
NodeJS搭建HTTP服务器的实现步骤
2018/10/12 NodeJs
layui 对弹窗 form表单赋值的实现方法
2019/09/04 Javascript
微信小程序实现简单文字跑马灯
2020/05/26 Javascript
Python守护进程(daemon)代码实例
2015/03/06 Python
基于ID3决策树算法的实现(Python版)
2017/05/31 Python
利用Python2下载单张图片与爬取网页图片实例代码
2017/12/25 Python
Python操作MySQL数据库的三种方法总结
2018/01/30 Python
Python元组及文件核心对象类型详解
2018/02/11 Python
Python图像处理之识别图像中的文字(实例讲解)
2018/05/10 Python
情人节快乐! python绘制漂亮玫瑰
2020/08/18 Python
Python 继承,重写,super()调用父类方法操作示例
2019/09/29 Python
Python基于pip实现离线打包过程详解
2020/05/15 Python
基于Modernizr 让网站进行优雅降级的分析
2013/04/21 HTML / CSS
餐饮业会计岗位职责
2013/12/19 职场文书
项目考察欢迎辞
2014/01/17 职场文书
竞聘上岗演讲稿
2014/05/16 职场文书
2014党员批评和自我批评思想汇报
2014/09/21 职场文书
行政执法作风整顿剖析材料
2014/10/11 职场文书
领导个人查摆剖析材料
2014/10/29 职场文书