当前位置:网站首页>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 :

边栏推荐
- B-jiege's tree (pressed tree DP)
- Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
- Is it difficult for small and micro enterprises to make accounts? Smart accounting gadget quick to use
- Redis insert data garbled solution
- 自定义限流注解
- Is it safe to open an account in flush? Which securities company is good at opening an account? Low handling charges
- 小孩子学什么编程?
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)
- Web开发小妙招:巧用ThreadLocal规避层层传值
- Catch ball game 1
猜你喜欢

PHP online examination system version 4.0 source code computer + mobile terminal

Comment faire une radio personnalisée

Number of schemes from the upper left corner to the lower right corner of the chessboard (2)

Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution

电子游戏的核心原理

Deep learning classification network -- zfnet
![[DSP] [Part 2] understand c6678 and create project](/img/06/54b1cf1f5b3308fffb4f84dcf7db9b.png)
[DSP] [Part 2] understand c6678 and create project

Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache

Error analysis ~csdn rebound shell error
![[DIY]如何制作一款个性的收音机](/img/fc/a371322258131d1dc617ce18490baf.jpg)
[DIY]如何制作一款个性的收音机
随机推荐
Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution
7. Data permission annotation
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
JMeter server resource indicator monitoring (CPU, memory, etc.)
(工作记录)2020年3月11日至2021年3月15日
Summary of different configurations of PHP Xdebug 3 and xdebug2
Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
Leetcode question 448 Find all missing numbers in the array
动态切换数据源
Recyclerview GridLayout bisects the middle blank area
Rhcsa Road
Unity making plug-ins
2022 construction electrician (special type of construction work) free test questions and construction electrician (special type of construction work) certificate examination
1_ Introduction to go language
[DIY]如何制作一款个性的收音机
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
强化学习-学习笔记5 | AlphaGo
Minimum cut edge set of undirected graph
OLED屏幕的使用
Error analysis ~csdn rebound shell error