php怎么对登录失败的次数进行限制
Admin 2021-04-28 群英技术资讯 1422 次浏览
php怎么对登录失败的次数进行限制?如果同一站号在同一IP下连续登陆失败多次,出于用户的账号安全问题考虑,我们需要对登陆失败的次进行限制,超出次数,账号应该被锁定。那么要如何实现这一操作呢?
在用户身份验证的情况下,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:
总结
以上就是关于php对登录失败次数进行限制的介绍,上文有具体代码,有需要的朋友可以参考,更多PHP内容大家可以继续关注其他文章。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
经常会有人问我, PHP的数组, 如果用foreach来访问, 遍历的顺序是固定的么? 以什么顺序遍历呢?php$arr[‘laruence’] = ‘huixinchen’;$arr[‘yahoo’] = 2007;$a
我们在做开发的时候,有一些需求需要我们根据身份来获取年龄,那么这要如何实现呢?下面小编就给大家分享一下php身份证获取年龄的代码,感兴趣朋友可以参考。
PHP CLI模式开发不需要任何一种Web服务器(包括Apache或MS IIS等),这样,CLI可以运行在各种场合。有两种方法可以运行PHP CLI脚本。
对象初始化要创建一个新的对象object,使用new语句实例化一个类:<?phpclassfoo{ functiondo_foo() { echo
面向对象的三大特点是封装、继承、多态。本文将通过示例详细讲讲这三者的使用,文中示例代码讲解详细,需要的可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008