当前位置:网站首页>If the account number or password is entered incorrectly for many times, the account will be banned.
If the account number or password is entered incorrectly for many times, the account will be banned.
2022-07-31 05:32:00 【The flowers are blooming in the south of the city^】
目录
How to apply it in real scenarios
Account ban plan
Flowchart of account ban
How to apply it in real scenarios
使用mysql作为计数器
- My specific implementation is based on tornado框架,django,flaskIts interface methods are similar
- tornadoIt is an interface written in an asynchronous non-blocking way
- Which is uploaded after successful logintoken,Please see the detailed introduction of the blogger's homepage
- 使用mysqlDoing a counting operation will compareredis麻烦许多,And a separate table needs to be defined to store the time of each wrong input,to determine whether the specified time has been exceeded,并且计数,但是redisThere will be no time subtraction problem involved,redisIt can be set to expire and delete itself,而mysqlManual deletion is required(代码删除)
- mysqlis commonly used,redis对性能有一定要求,存储在磁盘上,会有一定的消耗
# 计数器表(这是在tornadoTables defined in the framework) class CheckNumModel(peewee.Model): id = peewee.IntegerField(primary_key = True,unique=True,constraints=[peewee.SQL('AUTO_INCREMENT')]) #入库时间 create_time = peewee.DateTimeField(default=datetime.datetime.now(),help_text='入库时间') email = peewee.CharField(null=False,unique=True) num = peewee.IntegerField(null=False,default=0) class Meta: # 声明表名 db_table = 'check_num'
# 登录页面
class UserLogin(BaseHandler):
async def get(self):
# 接收参数
email = self.get_argument('email', None)
password = self.get_argument('password', None)
print('The obtained email and password:',email,password)
#判断是否获取到数据
if not all([email,password]):
self.finish({'msg':'Email or verification code is empty','errcode':0})
try:
# 获取计数器(mysql版本)
try:
#从数据库表中查询
num_mysql =await self.application.objects.get(CheckNumModel.select().where(CheckNumModel.email==email))
except Exception as e:
num_mysql = None
# Get the counter
if num_mysql:
# 获取当前时间
now_time = int(time.time())
# Get the point in time when the wrong password was entered for the first time
num_time = num_mysql.create_time.timestamp()
print(now_time,num_time)
# 减法运算
mytime = now_time - num_time
print(mytime)
if mytime <= 30:
# 判断计数器
if num_mysql.num>5:
self.finish({'msg':'You have exceeded the number of errors','errcode':3})
# 超过30seconds to delete from the database
else:
# 清除计数器
await self.application.objects.delete(num_mysql)
# 查找用户是否存在
user = await self.application.objects.get(UserModel.select().where((UserModel.email==email ) & (UserModel.password == make_password(password)) ))
# Determine whether the account is activated
if user.state == 0:
self.finish({'msg':'Account has not been activated','errcode':400})
else:
# 生成jwt
myjwt=MyJwt()
self.finish({"msg":"邮箱已激活 登陆成功", 'email':user.email,"errcode": 1,'token':myjwt.encode({'id':user.id})})
except Exception as e:
# print('登录失败的原因:',e)
self.finish({'msg':'用户名或密码错误','errcode':2}
# mysql逻辑
if num_mysql:
# The number of errors accumulates
num_mysql.num += 1
# 修改数据库
await self.application.objects.update(num_mysql)
# 第一次输错,插入计数器
else:
await self.application.objects.create(CheckNumModel,email=email,num=1)
使用redis作为计数器
class UserLogin(BaseHandler):
async def get(self):
# 接收前端传递的参数
email = self.get_argument('email', None)
password = self.get_argument('password', None)
print('The obtained email and password:',email,password)
#判断是否获取到数据
if not all([email,password]):
self.finish({'msg':'Email or verification code is empty','errcode':0})
try:
# 获取计数器(redis)
num=self.application.redis.get('num_'+email)
# if the counter exists
if num:
if int(num) >= 5:
#Of course, when testing the interface yourself,The time can be set shorter
self.finish({'msg':'You have exceeded the number of errors,请30try in minutes','errcode':3})
# 查找用户是否存在
user = await self.application.objects.get(UserModel.select().where((UserModel.email==email ) & (UserModel.password == make_password(password)) ))
# Determine whether the account is activated
if user.state == 0:
self.finish({'msg':'Account has not been activated','errcode':400})
else:
# 生成jwt
myjwt=MyJwt()
self.finish({"msg":"邮箱已激活 登陆成功", 'email':user.email,"errcode": 1,'token':myjwt.encode({'id':user.id})})
except Exception as e:
# print('登录失败的原因:',e)
self.finish({'msg':'用户名或密码错误','errcode':2})
# redis版本
# if the counter exists
if num:
# Accumulate the number of errors
self.application.redis.incr('num_'+email)
# 计数器不存在,It means that the user made a mistake for the first time
else:
# 30s以内,Accumulate by mistake
self.application.redis.setex('num_'+email,30,1)
边栏推荐
- 详解扫雷游戏(C语言)
- About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
- Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?
- Input length must be multiple of 8 when decrypting with padded cipher
- Mysql application cannot find my.ini file after installation
- MYSQL一站式学习,看完即学完
- Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
- 为什么要用Flink,怎么入门使用Flink?
- C语言实验三 选择结构程序设计
- mysql 的简单运用命令
猜你喜欢
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
数据库上机实验5 数据库安全性
Kubernetes 证书可用年限修改
Refinement of the four major collection frameworks: Summary of List core knowledge
第7章 网络层第3次练习题答案(第三版)
Temporal客户端模型
再见了繁琐的Excel,掌握数据分析处理技术就靠它了
Flask-based three-party login process
剑指offer专项突击版 --- 第 3 天
随机推荐
Simple command of mysql
Mysql——字符串函数
mysql5.7.35安装配置教程【超级详细安装教程】
再见了繁琐的Excel,掌握数据分析处理技术就靠它了
详解扫雷游戏(C语言)
数据库上机实验1 数据库定义语言
对list集合进行分页,并将数据显示在页面中
有了MVC,为什么还要DDD?
数据库上机实验4 数据更新和视图
Centos7 install mysql5.7
matlab simulink欠驱动水面船舶航迹自抗扰控制研究
[MQ I can speak for an hour]
MySQL8--Windows下使用压缩包安装的方法
.NET-6.WinForm2.NanUI learning and summary
mysql stored procedure
MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
CentOS7 - yum install mysql
Redis的初识
tf.keras.utils.get_file()
Anaconda configure environment directives