当前位置:网站首页>Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
2022-07-06 20:42:00 【IT1995】
The login used here is to read foreigners' custom login and registration functions , It's using Laravel8, If you use this online directly , Not very safe. . If it is brutally cracked , It's troublesome to keep trying , There are too many script boys now , The threshold is low , Ordinary people can learn to disgust others in a few days . Here I write a train of thought , I don't know and php Is the mainstream the same . Anyway, I write SpringBoot This idea is used in the project .
First build a users_lock surface

Among them the users_email and users Table correspondence , There is no foreign key relationship , It's equivalent to being independent , The design here is not very good , But I feel that small sites are enough .
Corresponding SQL That's true :
CREATE TABLE `users_lock` (
`user_email` varchar(255) NOT NULL,
`login_num` int(11) DEFAULT 5,
`last_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`lock_time` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`user_email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;Fill in this table when registering , Just fine .
The key is to log in , My logic is like this :
public function customLogin(Request $request)
{
$request->validate([
'email' => 'required|email',
'password' => 'required|min:6|max:128',
'captcha' => 'required|captcha'
]);
// verification
date_default_timezone_set('Asia/Shanghai');
$userLock = UserLock::find($request['email']);
if(!$userLock){
return redirect()->back()->withErrors(' Username or password incorrect ');
}
if($userLock['last_time'] < date('Y-m-d H:i:s',strtotime('-5 minute')) && $userLock['login_num'] <= 0){
$userLock['login_num'] = 5;
$userLock->save();
}
// lock
if($userLock['login_num'] <= 0){
return redirect()->back()->withErrors(' Account lock , Unlock time ' . $userLock['lock_time']);
}
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
return redirect()->intended('dashboard')
->withSuccess('Signed in');
}
// frequency
$userLock['login_num'] -= 1;
if($userLock['login_num'] <= 0){
$userLock['lock_time'] = date('Y-m-d H:i:s',strtotime('+5 minute'));
}
$userLock->save();
return redirect()->back()->withErrors(' Wrong user name or password ');
}Logic :
① First detect users_lock Whether this user exists in , If so, continue , without , Go straight back ;
② Determine whether the number of attempts is 0, If 0, also last_time, Be overdue ( Than the current time -5 Minutes should be small ), Just count the number of attempts , Reset to 5.
( There is no way here , If you have conditional friends , It is suggested to use the scheduling thread to do , Every time 5 Run every minute , Or directly use the timer of the database )
③ When login_time by 0 when , It means that the account has been locked .
④ Use Laravel Of Auth To verify the username and password .
⑤ Login times -5, If the number of logins <=0 Just lock the account , Lock to the current time +5 minute .
here UserLock Class is like this :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class UserLock extends Model
{
protected $table = "users_lock";
protected $primaryKey = 'user_email';
protected $keyType = 'string';
public $timestamps = false;
}It can be used :

边栏推荐
- Spiral square PTA
- 15 millions d'employés sont faciles à gérer et la base de données native du cloud gaussdb rend le Bureau des RH plus efficace
- 看过很多教程,却依然写不好一个程序,怎么破?
- Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
- [diy] how to make a personalized radio
- Special topic of rotor position estimation of permanent magnet synchronous motor -- Summary of position estimation of fundamental wave model
- PHP online examination system version 4.0 source code computer + mobile terminal
- 【每周一坑】输出三角形
- 硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
- Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
猜你喜欢
![[DSP] [Part 1] start DSP learning](/img/81/051059958dfb050cb04b8116d3d2a8.png)
[DSP] [Part 1] start DSP learning

What programming do children learn?

【每周一坑】信息加密 +【解答】正整数分解质因数

Basic knowledge of lists

“罚点球”小游戏

逻辑是个好东西
![[weekly pit] calculate the sum of primes within 100 + [answer] output triangle](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[weekly pit] calculate the sum of primes within 100 + [answer] output triangle
![[DSP] [Part 2] understand c6678 and create project](/img/06/54b1cf1f5b3308fffb4f84dcf7db9b.png)
[DSP] [Part 2] understand c6678 and create project
Tencent T4 architect, Android interview Foundation

硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
随机推荐
小孩子学什么编程?
棋盘左上角到右下角方案数(2)
B-jiege's tree (pressed tree DP)
7. Data permission annotation
Xcode6 error: "no matching provisioning profiles found for application"
为什么新手在编程社区提问经常得不到回答,甚至还会被嘲讽?
Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
Value of APS application in food industry
自定义限流注解
Review questions of anatomy and physiology · VIII blood system
Core principles of video games
看过很多教程,却依然写不好一个程序,怎么破?
[DIY]如何制作一款个性的收音机
In unity space, an object moves around a fixed point on the sphere at a fixed speed
Summary of different configurations of PHP Xdebug 3 and xdebug2
use. Net drives the OLED display of Jetson nano
What key progress has been made in deep learning in 2021?
Is it difficult for small and micro enterprises to make accounts? Smart accounting gadget quick to use
Leetcode question 283 Move zero
电子游戏的核心原理