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 MYSQL乱码问题,使用SET NAMES utf8校正
Nov 30 PHP
php jquery 实现新闻标签分类与无刷新分页
Dec 18 PHP
phpmyadmin出现Cannot start session without errors问题解决方法
Aug 14 PHP
PHP判断一个gif图片是否为动态图片的方法
Nov 19 PHP
php+mysql大量用户登录解决方案分析
Dec 29 PHP
递归实现php数组转xml的代码分享
May 14 PHP
PHP实现在线阅读PDF文件的方法
Jun 17 PHP
ThinkPHP中order()使用方法详解
Apr 19 PHP
PHP实现的统计数据功能详解
Dec 06 PHP
PHP命名空间用法实例分析
Sep 04 PHP
PHP中通过getopt解析GNU C风格命令行选项
Nov 18 PHP
php实现根据身份证获取精准年龄
Feb 26 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连接Oracle数据库
2006/10/09 PHP
如何判断php数组的维度
2013/06/10 PHP
Yii2组件之多图上传插件FileInput的详细使用教程
2016/06/20 PHP
在Thinkphp中使用ajax实现无刷新分页的方法
2016/10/25 PHP
Yii框架弹出窗口组件CJuiDialog用法分析
2017/01/07 PHP
PHP中的日期时间处理利器实例(Carbon)
2017/06/09 PHP
windows下的WAMP环境搭建图文教程(推荐)
2017/07/27 PHP
对采用动态原型方式无法展示继承机制得思考
2009/12/04 Javascript
jquery刷新页面的实现代码(局部及全页面刷新)
2011/07/11 Javascript
jQuery源码分析之jQuery中的循环技巧详解
2014/09/06 Javascript
使用jQuery仿苹果官网焦点图特效
2014/12/23 Javascript
js实现select下拉框菜单
2015/12/08 Javascript
JS在Chrome浏览器中showModalDialog函数返回值为undefined的解决方法
2016/08/03 Javascript
jQuery布局组件EasyUI Layout使用方法详解
2017/02/28 Javascript
JS实现移动端实时监听输入框变化的实例代码
2017/04/12 Javascript
微信小程序如何获知用户运行小程序的场景教程
2017/05/17 Javascript
编写React组件项目实践分析
2018/03/04 Javascript
JS实现左边列表移到到右边列表功能
2018/03/28 Javascript
js正则取值的结果数组调试方法
2018/10/10 Javascript
一份超级详细的Vue-cli3.0使用教程【推荐】
2018/11/15 Javascript
React优化子组件render的使用
2019/05/12 Javascript
浅谈vue项目用到的mock数据接口的两种方式
2019/10/09 Javascript
Vue中实现回车键切换焦点的方法
2020/02/19 Javascript
vue项目配置使用flow类型检查的步骤
2020/03/18 Javascript
Python实现二维曲线拟合的方法
2018/12/29 Python
Django之创建引擎索引报错及解决详解
2019/07/17 Python
python 动态迁移solr数据过程解析
2019/09/04 Python
Python3操作读写CSV文件使用包过程解析
2020/04/10 Python
使用Keras实现简单线性回归模型操作
2020/06/12 Python
python3.7中安装paddleocr及paddlepaddle包的多种方法
2020/11/27 Python
Champs Sports加拿大:北美最大的以商场为基础的专业运动鞋和服装零售商之一
2018/05/01 全球购物
五一服装活动方案
2014/01/11 职场文书
美容院考勤制度
2014/01/30 职场文书
《春雨》教学反思
2014/04/24 职场文书
2014年小学图书室工作总结
2014/12/09 职场文书
MySQL数字类型自增的坑
2021/05/07 MySQL