php - Laravel:在自定义登录控制器中使用节流阀
问题描述
这是我的登录控制器功能
use ThrottlesLogins;
protected $maxLoginAttempts = 3;
protected $lockoutTime = 300;
public function login(Request $request)
{
if ($this->hasTooManyLoginAttempts($request))
{
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
$validator = Validator::make(Input::all() , ['credential' => 'required|min:2|max:255', 'password' => 'required|string|min:8', ]);
$cred = $request->credential;
$pw = $request->password;
$remember = (Input::has('remember')) ? true : false;
if (filter_var($cred, FILTER_VALIDATE_EMAIL))
{
if (Auth::guard('customers')->attempt(['email' => $cred, 'password' => $pw, 'verified' => 1], $remember))
{
return redirect()->route('front');
}
else
{
return redirect()->route('customer-login-page')->with('error', 'Your credentials do not match');
}
}
else
{
if (Auth::guard('customers')->attempt(['contact' => $cred, 'password' => $pw], $remember))
{
return redirect()->intended(route('front'));
}
else
{
return redirect()->route('customer-login-page')->with('error', 'Your credentials do not match');
}
}
}
protected function hasTooManyLoginAttempts(Request $request)
{
return $this->limiter()->tooManyAttempts(
$this->throttleKey($request), $this->maxLoginAttempts, $this->lockoutTime
);
}
它不工作。我已经尝试了超过 3 次失败的登录尝试,但仍然没有受到限制。并且即使我发布了正确的凭据,登录和重定向仍然有效,但是当我检查请求时,我得到了
302 发现错误
在网络选项卡中
解决方案
$this->incrementLoginAttempts($request)
您需要通过调用(参见代码)让 trait 知道您正在执行登录尝试。您可以在现有的油门检查之后立即拨打此电话:
if ($this->hasTooManyLoginAttempts($request))
{
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
$this->incrementLoginAttempts($request);
// other code
推荐阅读
- c# - 从派生类到基类 C# 的指针
- go - 如何使用反射设置切片接口值
- fortran - Fortran 错误 LNK2019 - 函数 MAIN_ 中引用的未解析外部符号 D_ZANLY
- excel - Excell VBA Shell 命令不起作用,但它可以从 Windows 命令窗口工作
- solr - 折叠来自 solr 的结果后过滤
- javascript - 只有一个 blob 可以播放,使用媒体记录器用电子在磁盘上写入
- vue.js - 使用 VueJS 从选择下拉菜单中删除某些项目
- windows - 没有凭据管理器的 Windows 共享身份验证
- javascript - 通过 mailto 在正文中发送不可执行的脚本:
- python - 如何配置 python 私有 repo 版本