Laravel登录失败次数限制的实现方法

 更新时间:2020年08月26日 10:16:55   作者:Lenix  
这篇文章主要给大家介绍了关于Laravel登录失败次数限制的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

在用户身份验证的情况下,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统计当前在线用户数实例讲解

    我们要统计在一段时间内访问站点的人数,有多种解决方案,你可以使用cookie,session结合文本或者数据库来记录用户访问数。本文将使用PHP,结合Mysql以及jQuery,展示一个统计在线人数以及访客地区分布的示例。
    2015-10-10
  • PHP中针对区域语言标记信息的操作

    PHP中针对区域语言标记信息的操作

    大家都知道Locale 类就是操作区域语言相关内容的,它无法被实例化,所有全部功能方法都是静态的。接下来通过本文给大家分享PHP中针对区域语言标记信息的操作,需要的朋友参考下吧
    2021-07-07
  • PHP利用百度ai实现文本和图片审核

    PHP利用百度ai实现文本和图片审核

    这篇文章主要介绍了PHP利用百度ai实现文本和图片审核,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • Zend Framework教程之Zend_Db_Table表关联实例详解

    Zend Framework教程之Zend_Db_Table表关联实例详解

    这篇文章主要介绍了Zend Framework教程之Zend_Db_Table表关联用法,结合实例形式较为详细的分析了Zend_Db_Table表关联的定义,实现方法与相关注意事项,需要的朋友可以参考下
    2016-03-03
  • laravel Validator ajax返回错误信息的方法

    laravel Validator ajax返回错误信息的方法

    今天小编就为大家分享一篇laravel Validator ajax返回错误信息的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-09-09
  • PHP中的流(streams)浅析

    PHP中的流(streams)浅析

    这篇文章主要介绍了PHP中的流(streams)浅析,本文讲解了流的概述 、流基础知识、php://包装器、流上下文(Stream Contexts)等内容,需要的朋友可以参考下
    2015-07-07
  • thinkphp利用模型通用数据编辑添加和删除的实例代码

    thinkphp利用模型通用数据编辑添加和删除的实例代码

    下面小编就为大家带来一篇thinkphp利用模型通用数据编辑添加和删除的实例代码。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • PHP如何将图片文件上传到另外一台服务器上

    PHP如何将图片文件上传到另外一台服务器上

    这篇文章主要介绍了PHP如何将图片文件上传到另外一台服务器上,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • PHP5.4起内置web服务器使用方法

    PHP5.4起内置web服务器使用方法

    这篇文章主要为大家详细介绍了PHP5.4内置web服务器,内置的Web服务器只是提供开发测试使用,不推荐使用中生产环境中,具体内容请阅读下文
    2016-08-08
  • php中curl使用指南

    php中curl使用指南

    这篇文章主要介绍了php中curl使用指南,十分详细,需要的朋友可以参考下
    2015-02-02

最新评论