欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Laravel登錄失敗次數(shù)限制的實(shí)現(xiàn)方法

 更新時(shí)間:2020年08月26日 10:16:55   作者:Lenix  
這篇文章主要給大家介紹了關(guān)于Laravel登錄失敗次數(shù)限制的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

在用戶身份驗(yàn)證的情況下,Laravel 具有內(nèi)置的身份驗(yàn)證系統(tǒng)。我們可以根據(jù)要求輕松修改它。身份驗(yàn)證中包含的功能之一是Throttling.

為什么我們需要throttling保護(hù)?

基本上,throttling是用來(lái)保護(hù)暴力攻擊的。它將在一定時(shí)間內(nèi)檢查登錄嘗試。在短登錄中,throttling會(huì)計(jì)算用戶或機(jī)器人嘗試失敗的登錄嘗試次數(shù)。

使用自定義登錄實(shí)現(xiàn)限制

默認(rèn)情況下,在內(nèi)置身份驗(yàn)證控制器中實(shí)現(xiàn)限制。但是,如果我們需要實(shí)現(xiàn)它到自定義登錄呢?

實(shí)現(xiàn)自定義登錄限制非常容易。首先,我們必須將ThrottlesLogins trait包含到您的控制器中。

use Illuminate\Foundation\Auth\ThrottlesLogins;

現(xiàn)在,將此ThrottlesLogins trait 加到控制器中。

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\ThrottlesLogins;
class AuthController extends Controller
{
 use ThrottlesLogins;
 ......

現(xiàn)在轉(zhuǎn)到用于對(duì)用戶進(jìn)行身份驗(yàn)證的方法。在我的例子中,我使用了 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);
 }
 
 .......

首先,我們驗(yàn)證了用戶提交的輸入,然后實(shí)現(xiàn)了hasTooManyLoginAttempts() 方法。此方法將檢查用戶在某個(gè)時(shí)間是否執(zhí)行過(guò)一定數(shù)量的失敗嘗試,然后系統(tǒng)將通過(guò)sendLockoutResponse()  方法阻止該用戶。

現(xiàn)在,我們必須通過(guò)incrementLoginAttempts()方法指示對(duì)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.']);
}

您還可以通過(guò)$maxAttempts和$decayMinutes屬性更改允許的最大嘗試次數(shù)和限制的分鐘數(shù)。在這里,您可以找到完整的代碼。

<?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:

總結(jié)

到此這篇關(guān)于Laravel登錄失敗次數(shù)限制的文章就介紹到這了,更多相關(guān)Laravel登錄失敗次數(shù)限制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • thinkPHP框架單元測(cè)試庫(kù)tpunit用法示例

    thinkPHP框架單元測(cè)試庫(kù)tpunit用法示例

    這篇文章主要介紹了thinkPHP框架單元測(cè)試庫(kù)tpunit用法,結(jié)合實(shí)例形式分析了tpunit簡(jiǎn)單使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2018-08-08
  • thinkphp中空模板與空模塊的用法實(shí)例

    thinkphp中空模板與空模塊的用法實(shí)例

    這篇文章主要介紹了thinkphp中空模板與空模塊的用法,以實(shí)例形式演示了空模板與空模塊具體實(shí)現(xiàn)方法,一般空模塊多用于系統(tǒng)找不到指定模塊的情況,如定位錯(cuò)誤頁(yè)面與URL優(yōu)化等情況,具有一定的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-11-11
  • Zend Framework教程之Zend_Form組件實(shí)現(xiàn)表單提交并顯示錯(cuò)誤提示的方法

    Zend Framework教程之Zend_Form組件實(shí)現(xiàn)表單提交并顯示錯(cuò)誤提示的方法

    這篇文章主要介紹了Zend Framework教程之Zend_Form組件實(shí)現(xiàn)表單提交并顯示錯(cuò)誤提示的方法,結(jié)合實(shí)例形式詳細(xì)分析了Zend_Form組件的使用方法與實(shí)現(xiàn)表單提交的具體操作步驟,需要的朋友可以參考下
    2016-03-03
  • PHP array_reverse() 函數(shù)原理及實(shí)例解析

    PHP array_reverse() 函數(shù)原理及實(shí)例解析

    這篇文章主要介紹了PHP array_reverse() 函數(shù)原理及實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 分享微信掃碼支付開(kāi)發(fā)遇到問(wèn)題及解決方案-附Ecshop微信支付插件

    分享微信掃碼支付開(kāi)發(fā)遇到問(wèn)題及解決方案-附Ecshop微信支付插件

    微信掃碼支付在購(gòu)物商城非常流行,本篇文章給大家分享微信掃碼支付開(kāi)發(fā)遇到問(wèn)題及解決方案-附Ecshop微信支付插件,需要的朋友可以參考下
    2015-08-08
  • php的mkdir()函數(shù)創(chuàng)建文件夾比較安全的權(quán)限設(shè)置方法

    php的mkdir()函數(shù)創(chuàng)建文件夾比較安全的權(quán)限設(shè)置方法

    這篇文章主要介紹了php的mkdir()函數(shù)創(chuàng)建文件夾比較安全的權(quán)限設(shè)置方法,遇到的情況是系統(tǒng)umask影響了mkdir的指定權(quán)限參數(shù)比期望要小,使用chmod函數(shù)則沒(méi)有這個(gè)問(wèn)題,需要的朋友可以參考下
    2014-07-07
  • php實(shí)現(xiàn)文本數(shù)據(jù)導(dǎo)入SQL SERVER

    php實(shí)現(xiàn)文本數(shù)據(jù)導(dǎo)入SQL SERVER

    php將文本文件導(dǎo)入mysql我們經(jīng)常遇到,但是如果是導(dǎo)入到sqlserver又應(yīng)該如何操作呢,下面就給大家分享一下本人的操作方法,感覺(jué)效率還不錯(cuò),這里推薦給大家。
    2015-05-05
  • 學(xué)習(xí)php中的正則表達(dá)式

    學(xué)習(xí)php中的正則表達(dá)式

    簡(jiǎn)單的說(shuō),正則表達(dá)式是一種可以用于模式匹配和替換的強(qiáng)有力的工具。我們可以在幾乎所有的基于UNIX系統(tǒng)的工具中找到ta的身影。此外,象JavaScript這種客戶端的腳本語(yǔ)言也提供了支持。正則表達(dá)式已經(jīng)超出了某種語(yǔ)言或某個(gè)系統(tǒng)的局限,成為人們廣為接受的概念和功能。
    2014-08-08
  • CTF中的PHP特性函數(shù)解析之中篇

    CTF中的PHP特性函數(shù)解析之中篇

    這篇文章主要為大家介紹了CTF中的PHP特性函數(shù)解析,本文分三篇此篇為中篇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Laravel ORM 數(shù)據(jù)model操作教程

    Laravel ORM 數(shù)據(jù)model操作教程

    今天小編就為大家分享一篇Laravel ORM 數(shù)據(jù)model操作教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-10-10

最新評(píng)論