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扩展” 的解决方法
Apr 16 PHP
php session_start()关于Cannot send session cache limiter - headers already sent错误解决方法
Nov 27 PHP
让PHP更快的提供文件下载的代码
Jun 13 PHP
php ios推送(代码)
Jul 01 PHP
PHP传参之传值与传址的区别
Apr 24 PHP
php版微信公众平台入门教程之开发者认证的方法
Sep 26 PHP
PHP读取zip文件的方法示例
Nov 17 PHP
php使用PDO执行SQL语句的方法分析
Feb 16 PHP
PHP实现的简单排列组合算法应用示例
Jun 20 PHP
PHP编译configure时常见错误的总结
Aug 17 PHP
PHP开发api接口安全验证操作实例详解
Mar 26 PHP
Laravel框架使用技巧之使用url()全局函数返回前一个页面的地址方法详解
Apr 06 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数组的概述及分类与声明代码演示
2013/02/26 PHP
php中的Base62类(适用于数值转字符串)
2013/08/12 PHP
根据中文裁减字符串函数的php代码
2013/12/03 PHP
PHP开发框架kohana3 自定义路由设置示例
2014/07/14 PHP
使用xampp搭建运行php虚拟主机的详细步骤
2015/10/21 PHP
php+mysql查询实现无限下级分类树输出示例
2016/10/03 PHP
PHP 网站修改默认访问文件的nginx配置
2017/05/27 PHP
php实现解析xml并生成sql语句的方法
2018/02/03 PHP
jquery $.ajax()取xml数据的小问题解决方法
2010/11/20 Javascript
$.format,jquery.format 使用说明
2011/07/13 Javascript
javascript面向对象编程代码
2011/12/19 Javascript
WEB前端开发都应知道的jquery小技巧及jquery三个简写
2015/11/15 Javascript
利用jquery制作滚动到指定位置触发动画
2016/03/26 Javascript
js HTML5多图片上传及预览实例解析(不含前端的文件分割)
2016/08/26 Javascript
vue路由中前进后退的一些事儿
2019/05/18 Javascript
Nuxt配置Element-UI按需引入的操作方法
2020/07/06 Javascript
Python实现的选择排序算法示例
2017/11/29 Python
Python tkinter实现的图片移动碰撞动画效果【附源码下载】
2018/01/04 Python
python实现pdf转换成word/txt纯文本文件
2018/06/07 Python
Python List cmp()知识点总结
2019/02/18 Python
余弦相似性计算及python代码实现过程解析
2019/09/18 Python
将matplotlib绘图嵌入pyqt的方法示例
2020/01/08 Python
pytorch GAN伪造手写体mnist数据集方式
2020/01/10 Python
使用sublime text3搭建Python编辑环境的实现
2021/01/12 Python
关于box-sizing的全面理解
2016/07/28 HTML / CSS
英国领先的高级美容和在线皮肤诊所:Face the Future
2020/06/17 全球购物
介绍Java的内部类
2012/10/27 面试题
企业授权委托书范本
2014/04/02 职场文书
经济管理专业求职信
2014/06/09 职场文书
民事诉讼授权委托书范文
2014/08/02 职场文书
就业协议书
2014/09/12 职场文书
热血教师观后感
2015/06/10 职场文书
golang 在windows中设置环境变量的操作
2021/04/29 Golang
Python卷积神经网络图片分类框架详解分析
2021/11/07 Python
Java实现二分搜索树的示例代码
2022/03/17 Java/Android
Go语言特点及基本数据类型使用详解
2022/03/21 Golang