承接国内外服务器租用托管、定制开发、网站代运营、网站seo优化托管接单、网站代更新,新老站点皆可!!咨询QQ:3787320601

基于Redis实现逐日登录失败次数限制

管理员 2023-06-22 06:57:51 互联网圈 11 ℃ 0 评论 5164字 收藏

基于Redis实现逐日登录失败次数限制

1. 思路

下面是我之前写的代码,没斟酌高并发场景。如果是高并发场景下,要斟酌到redis的set方法覆盖值问题,可使用incr来替换get,set保证数据安全

通过redis记录登录失败的次数,以用户的username为key

每次收到登录的要求时,都去redis查询登录次数会不会已大于等于我们设置的限制次数, 是的话直接返回

2. 代码

前台登录和后台查询数据库的代码省略

2.1 controller

我这里使用的Jboot, 获得redisTemplate的方式是Jboot.me().getRedis(), spring的话用jedisTemplate就行.

// 如果用户输入账号密码有效登录超过限制次数,24小时制止登录
// 设置一天限制失败次数,默许为10次
final int limit = 3;
JbootRedis jr = Jboot.me().getRedis();
//Constants.LOGIN_COUNT = “LOGIN_COUNT”
//account是页面传过来的username
String key = Constants.LOGIN_COUNT + “_” + account;
Integer count = jr.get(key);
if(count == null){
count = 0;
}else {
if (count >= limit) {
//直接返回
ajaxJson.setMsg(“您今天登录失败的次数已超过限制,请明天再试。”);
ajaxJson.setSuccess(false);
logger.error(“账号为【”+account+”】的用户单日登录次数超过上限”);
render(callback, gson.toJson(ajaxJson));
return;
}
}
//… 去数据库根据username查询user对象
if (user != null) {
// 往redis中增加登录失败的次数
Integer newCount = IncrFailLoginCount(key,count);
logger.error(“账号为【”+account+”】的用户登录失败,”+ajaxJson.getMsg());
ajaxJson.setMsg(ajaxJson.getMsg() + “,剩下登录次数为:”+(limit-newCount));
render(callback, gson.toJson(ajaxJson));
return;
}else{
// 登录成功,清除redis失败记录
jr.del(key);
}

2.2 IncrFailLoginCount方法

/**
* 一天中登录失败的次数统计
* @param key redis中存储的键
* @param count 已登录失败的次数
* @return count 登录失败次数
*/
private Integer IncrFailLoginCount(String key,Integer count) {
JbootRedis jr = Jboot.me().getRedis();
count++;
//设置过期时间为今晚23点59分59秒
long timeInMillis = DateUtils.getMillsecBeforeMoment(23, 59, 59, 999);
if (timeInMillis < 100){
// 避免在最后一秒的时候登录致使过期时间太小乃至为负数
timeInMillis = 1000*60;
}
// 设置过期时间
jr.set(key,count);
//这里注意顺序, 先set再pexpire
jr.pexpire(key,timeInMillis);
return count;
}

这里用到了时间的一个工具类, 具体代码以下:

/**
* 获得当前时间到指定时刻前的毫秒数
* @param hour 指定时刻的小时
* @param min 指定时刻的分钟
* @param sec 指定时刻的秒
* @param mill 指定时刻的毫秒
* @return
*/
public static long getMillsecBeforeMoment(int hour,int min,int sec,int mill){
return getMillisecBetweenDate(new Date(),getMoment(hour,min,sec,mill));
}
/**
* 获得两个日期之间的毫秒数
* @param before
* @param after
* @return
*/
public static long getMillisecBetweenDate(Date before, Date after){
long beforeTime = before.getTime();
long afterTime = after.getTime();
return afterTime – beforeTime;
}
/**
* 获得当天的某一时刻Date
* @param hour 24小时
* @param min 分钟
* @param sec 秒
* @param mill 毫秒
* @return
*/
public static Date getMoment(int hour,int min,int sec,int mill){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY,hour);
calendar.set(Calendar.MINUTE,min);
calendar.set(Calendar.SECOND,sec);
calendar.set(Calendar.MILLISECOND,mill);
return calendar.getTime();
}

3. 总结

这里有个地方要注意,就是redis 设置过期时间后,重新set会清除过期效果, 重新变成永久状态, 所以需要每次都pexpire()
redis中还有一个方法:incr(),每次调用这个方法,都会让一个键的值+1,如果没有这个键,会初始为0再+1. 合适做计数器, 也能再这个案例中使用, 但是我这里只是希望登录失败的时候才计数+1 , 登录之前直接判断count, 所以使用了传统的get(),set(). 有兴趣的同学可以去详细了解.

文章来源:丸子建站

文章标题:基于Redis实现逐日登录失败次数限制

https://www.wanzijz.com/view/57869.html

X

截屏,微信识别二维码

微信号:weimawl

(点击微信号复制,添加好友)

打开微信