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 相关文章推荐
让你同时上传 1000 个文件 (一)
Oct 09 PHP
Windows下利用Gvim写PHP产生中文乱码问题解决方法
Apr 20 PHP
解析php多线程下载远程多个文件
Jun 25 PHP
基于PHP创建Cookie数组的详解
Jul 03 PHP
浅析51个PHP处理字符串的函数
Aug 02 PHP
php class类的用法详细总结
Oct 17 PHP
php的crc32函数使用时需要注意的问题(不然就是坑)
Apr 21 PHP
ThinkPHP连接Oracle数据库
Apr 22 PHP
PHP编辑器PhpStrom运行缓慢问题
Feb 21 PHP
PHP redis实现超迷你全文检索
Mar 04 PHP
PHP实现网页内容html标签补全和过滤的方法小结【2种方法】
Apr 27 PHP
PHP基于SPL实现的迭代器模式示例
Apr 22 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
Zend Framework实现多服务器共享SESSION数据的方法
2016/03/22 PHP
php学习笔记之mb_strstr的基本使用
2018/02/03 PHP
php连接mysql数据库最简单的实现方法
2019/09/24 PHP
nginx 设置多个站跨域
2021/03/09 Servers
用JAVASCRIPT如何给&amp;lt;textarea&amp;gt;&amp;lt;/textarea&amp;gt;赋值
2007/04/20 Javascript
JAVASCRIPT IE 与 FF中兼容问题小结
2009/02/18 Javascript
预加载css或javascript的js代码
2010/04/23 Javascript
原生js实现查找/添加/删除/指定元素的class
2013/04/12 Javascript
js/jquery去掉空格,回车,换行示例代码
2013/11/05 Javascript
javascript文本模板用法实例
2015/07/31 Javascript
js实现表单多按钮提交action的处理方法
2015/10/24 Javascript
mui 打开新窗口的方式总结及注意事项
2017/08/20 Javascript
vue :src 文件路径错误问题的解决方法
2018/05/15 Javascript
JS实现的视频弹幕效果示例
2018/08/17 Javascript
vue 刷新之后 嵌套路由不变 重新渲染页面的方法
2018/09/13 Javascript
详解webpack打包第三方类库的正确姿势
2018/10/20 Javascript
微信小程序框架的页面布局代码
2019/08/17 Javascript
vue实现评论列表功能
2019/10/25 Javascript
vue+vuex+axios从后台获取数据存入vuex,组件之间共享数据操作
2020/07/31 Javascript
[02:40]DOTA2英雄基础教程 先知
2013/11/29 DOTA
[00:09]DOTA2新版本PA至宝特效动作展示
2014/11/19 DOTA
Python排序算法之选择排序定义与用法示例
2018/04/29 Python
python实现决策树ID3算法的示例代码
2018/05/30 Python
python处理数据,存进hive表的方法
2018/07/04 Python
python实现高斯(Gauss)迭代法的例子
2019/11/20 Python
CSS3 简单又实用的5个属性
2010/03/04 HTML / CSS
CSS3的resize属性使用初探
2015/09/27 HTML / CSS
html5 兼容IE6结构的实现代码
2012/05/14 HTML / CSS
英国旅游额外服务市场领导者:Holiday Extras(机场停车场、酒店、接送等)
2017/10/07 全球购物
人力资源管理专业应届生求职信
2014/04/24 职场文书
珍惜时间演讲稿
2014/05/14 职场文书
村居抓节水倡议书
2014/05/19 职场文书
个人委托书范本汇总
2014/10/01 职场文书
市级三好学生评语
2014/12/29 职场文书
推广普通话主题班会
2015/08/17 职场文书
图文详解matlab原始处理图像几何变换
2021/07/09 Python