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
PHP学习笔记 IIS7下安装配置php环境
Oct 29 PHP
PHP strip_tags()去除HTML、XML以及PHP的标签介绍
Feb 18 PHP
PHP CURL 内存泄露问题解决方法
Feb 12 PHP
ThinkPHP里用U方法调用js文件实例
Jun 18 PHP
使用Appcan客户端自动更新PHP版本号(全)
Jul 31 PHP
jQuery+Ajax+PHP“喜欢”评级功能实现代码
Oct 08 PHP
php实现点击可刷新验证码
Nov 07 PHP
PHP下使用mysqli的函数连接mysql出现warning: mysqli::real_connect(): (hy000/1040): ...
Feb 14 PHP
php微信公众号开发模式详解
Nov 28 PHP
详解php与ethereum客户端交互
Apr 28 PHP
php设计模式之工厂模式用法经典实例分析
Sep 20 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
让你同时上传 1000 个文件 (一)
2006/10/09 PHP
php文件打包 下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
2012/06/13 PHP
解析PHP可变函数的经典用法
2013/06/20 PHP
php 把数字转换成汉字的代码
2015/07/21 PHP
Laravel中注册Facades的步骤详解
2016/03/16 PHP
thinkPHP5.0框架环境变量配置方法
2017/03/17 PHP
基于PHP实现的多元线性回归模拟曲线算法
2018/01/30 PHP
Js,alert出现乱码问题的解决方法
2013/06/19 Javascript
js获取url中指定参数值的示例代码
2013/12/14 Javascript
ExtJS4给Combobox设置列表中的默认值示例
2014/05/02 Javascript
jquery实现左右无缝轮播图
2020/07/31 Javascript
react router 4.0以上的路由应用详解
2017/09/21 Javascript
JavaScript代码执行的先后顺序问题
2017/10/29 Javascript
基于IView中on-change属性的使用详解
2018/03/15 Javascript
Nodejs实现爬虫抓取数据实例解析
2018/07/05 NodeJs
ionic+html5+API实现双击返回键退出应用
2019/09/17 Javascript
webpack优化之代码分割与公共代码提取详解
2019/11/22 Javascript
JavaScript实现多球运动效果
2020/09/07 Javascript
Python的collections模块中namedtuple结构使用示例
2016/07/07 Python
在Python程序员面试中被问的最多的10道题
2017/12/05 Python
python实现读Excel写入.txt的方法
2018/04/29 Python
tensorflow实现简单的卷积神经网络
2018/05/24 Python
Python运维自动化之nginx配置文件对比操作示例
2018/08/29 Python
使用python绘制二维图形示例
2019/11/22 Python
python_array[0][0]与array[0,0]的区别详解
2020/02/18 Python
Python如何转换字符串大小写
2020/06/04 Python
Python实现爬取并分析电商评论
2020/06/19 Python
如何用Python徒手写线性回归
2021/01/25 Python
scrapy-splash简单使用详解
2021/02/21 Python
简单聊聊H5的pushState与replaceState的用法
2018/04/03 HTML / CSS
梅西百货澳大利亚:Macy’s Australia
2017/07/26 全球购物
酒店开业策划方案
2014/06/02 职场文书
财会专业毕业生自荐信
2014/07/09 职场文书
2015年志愿者服务工作总结
2015/04/20 职场文书
PostgreSQL存储过程实用脚本(二):创建函数入门
2021/04/05 PostgreSQL
Mysql如何实现不存在则插入,存在则更新
2022/03/25 MySQL