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登录失败次数限制内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Laravel搭建后台登录系统步骤详解

    本文实例讲述了Laravel搭建后台登录系统的方法.分享给大家供大家参考,具体如下: 今天想用laravel搭建一个后台系统,就需要最简单的那种,有用户登录系统,试用了下,觉得laravel的用户登录这块做的还真happy.当然,前提就是,你要的用户管理系统是最简单的那种,就是没有用户权限,能登录就好. 我这里就不用默认的user表做例子了,那样很容易和laravel的一些默认设置混淆. 首先确认,后台的用户表,我设计表叫做badmin,每个管理员有用户名(username),有昵称(nickn

  • Laravel实现用户注册和登录

    Laravel身为最优雅的PHP框架,很多学习PHP的小伙伴造就对Laravel垂涎欲滴.今天就来实现你的愿望,让我们一起从零开始,利用Laravel实现Web应用最常见的注册和登录功能!所有的课程源码已放在Github上:laravel-start. Race Start ! 首先我们来明确一下我们这个课程需要的东西: Laravel 4.2 Bootstrap 3.3 Laravel就是我们关心的核心部分,Bootstrap用来快速设置一些前端的CSS样式. 1.安装Laravel 简单说明

  • php的laravel框架快速集成微信登录的方法

    本文面向的是php语言laravel框架的用户,介绍的是基于该框架实现的一个简易集成微信登录的方法.使用方法如下: 1. 安装php_weixin_provider 在项目下运行composer require thirdproviders/weixin,即可完成安装.安装成功后,在项目的vendor目录下应该能看到php_weixin_provider的库文件: 2. 配置微信登录的参数 一共有7个参数可以配置,分别是: client_id:对应公众号创建的应用appid client_sec

  • Laravel重写用户登录简单示例

    本文实例讲述了Laravel重写用户登录的方法.分享给大家供大家参考,具体如下: class AuthController extends Controller { // use ThrottlesLogins, AuthenticatesAndRegistersUsers; protected $redirectTo = 'admin/index'; protected $loginView = 'admin/login'; protected $guard = 'admin'; protec

  • SSO单点登录的PHP实现方法(Laravel框架)

    Laravel是一套简洁.优雅的PHP Web开发框架(PHP Web Framework).它可以让你从面条一样杂乱的代码中解脱出来:它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁.富于表达力. 简单说一下我的逻辑,我也不知道我理解sso对不对. 假如三个站点 a.baidu.com b.baidu.com c.baidu.com a.baidu.com 作为验证用户登录账户. b和c作为客户端(子系统). b和c需要登录的时候跳转到a,并且携带参数source指明登陆后跳转的链接

  • Laravel5.2使用Captcha生成验证码实现登录(session巨坑)

    最近有朋友要我帮忙弄一下laravel的验证码登陆,所以稍稍研究了一下.(本人都快忘了咋使用laravel了) 首先,安装laravel就不用在下赘述了吧,我的版本是5.2.45(注:laravel5.2.6以上的版本中间件可以自动加载),这还是挺重要的. 安装完成之后,你需要使用composer来加载你的Captcha,具体方法就是在你的composer.json中的require数组中加上"gregwar/captcha":"1.*"这行代码.然后嘞,就在你的项

  • Laravel 自带的Auth验证登录方法

    在laravel有自带的登录验证.只要建立对应的表和配置一些文件就能够使用,无需开发者自己去实现登录逻辑. 第一步:配置方面 在config下的auth.php配置guards 和 providers . 之后新建文件和数据库名称要和这里的对应. 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provid

  • laravel5.2实现区分前后台用户登录的方法

    1.前台登录 直接使用laravel自带的auth php artisan make:auth 然后可以查看路由文件: Route::group(['middleware' => 'web'], function () { Route::auth(); Route::get('/home', 'HomeController@index'); }); 执行php artisan migrate 会发现生成了两张表. 2.后台登录 编辑配置文件 config\auth.php 添加guards中的a

  • Laravel 5.4重新登录实现跳转到登录前页面的原理和方法

    前言 本文主要给大家介绍的是关于Laravel5.4重新登录跳转到登录前页面的相关内容,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍: 一.应用场景: 用户登陆后存在过期时间,超时用户需重新登录.例:当用户在/user/2 页面,登陆过期后跳转到登陆页面,登陆后用户还应在/user/2而不是home/index. 二.实现原理 在判断用户过期后,存储用户当前的url地址到session中,下次登陆后跳转到此url地址. 三.laravel中的具体实现 路由中间件(判断登陆状态) 这

  • 利用Laravel事件系统如何实现登录日志的记录详解

    本文介绍的是利用Laravel事件系统实现登录日志记录的相关内容,分享出来给大家参考,下面来看看详细的介绍: 明确需求 记录一个登录日志,通常需要下列信息: 客户端Agent信息 客户端IP地址 访问IP地点 登录时间 登录用户信息 确立工具 明确完需求后,根据每个需求查找自己所需的工具吧. 需求1 jenssegers/agent就可以满足我们要求 需求2 Laravel下直接Request::getClientIp() 需求3 zhuzhichao/ip-location-zh这个包可以满足

随机推荐