1. 思路
下面是我以前寫的代碼,沒考慮高并發(fā)場景。如果是高并發(fā)場景下,要考慮到redis的set方法覆蓋值問題,可以使用incr來替代get,set保證數(shù)據(jù)安全
通過redis記錄登錄失敗的次數(shù),以用戶的username為key
每次收到登錄的請(qǐng)求時(shí),都去redis查詢登錄次數(shù)是否已經(jīng)大于等于我們?cè)O(shè)置的限制次數(shù), 是的話直接返回
2. 代碼
前臺(tái)登錄和后臺(tái)查詢數(shù)據(jù)庫的代碼省略
2.1 controller
我這里使用的Jboot, 獲取redisTemplate的方式是Jboot.me().getRedis()
, spring的話用jedisTemplate就行.
// 如果用戶輸入賬號(hào)密碼有效登錄超過限制次數(shù),24小時(shí)禁止登錄
// 設(shè)置一天限制失敗次數(shù),默認(rèn)為10次
final int limit = 3;
JbootRedis jr = Jboot.me().getRedis();
//Constants.LOGIN_COUNT = "LOGIN_COUNT"
//account是頁面?zhèn)鬟^來的username
String key = Constants.LOGIN_COUNT + "_" + account;
Integer count = jr.get(key);
if(count == null){
count = 0;
}else {
if (count >= limit) {
//直接返回
ajaxJson.setMsg("您今天登錄失敗的次數(shù)已經(jīng)超過限制,請(qǐng)明天再試。");
ajaxJson.setSuccess(false);
logger.error("賬號(hào)為【"+account+"】的用戶單日登錄次數(shù)超過上限");
render(callback, gson.toJson(ajaxJson));
return;
}
}
//... 去數(shù)據(jù)庫根據(jù)username查詢user對(duì)象
if (user != null) {
// 往redis中增加登錄失敗的次數(shù)
Integer newCount = IncrFailLoginCount(key,count);
logger.error("賬號(hào)為【"+account+"】的用戶登錄失敗,"+ajaxJson.getMsg());
ajaxJson.setMsg(ajaxJson.getMsg() + ",剩下登錄次數(shù)為:"+(limit-newCount));
render(callback, gson.toJson(ajaxJson));
return;
}else{
// 登錄成功,清除redis失敗記錄
jr.del(key);
}
2.2 IncrFailLoginCount方法
/**
* 一天中登錄失敗的次數(shù)統(tǒng)計(jì)
* @param key redis中存儲(chǔ)的鍵
* @param count 已經(jīng)登錄失敗的次數(shù)
* @return count 登錄失敗次數(shù)
*/
private Integer IncrFailLoginCount(String key,Integer count) {
JbootRedis jr = Jboot.me().getRedis();
count++;
//設(shè)置過期時(shí)間為今晚23點(diǎn)59分59秒
long timeInMillis = DateUtils.getMillsecBeforeMoment(23, 59, 59, 999);
if (timeInMillis 100){
// 避免在最后一秒的時(shí)候登錄導(dǎo)致過期時(shí)間過小甚至為負(fù)數(shù)
timeInMillis = 1000*60;
}
// 設(shè)置過期時(shí)間
jr.set(key,count);
//這里注意順序, 先set再pexpire
jr.pexpire(key,timeInMillis);
return count;
}
這里用到了時(shí)間的一個(gè)工具類, 具體代碼如下:
/**
* 獲取當(dāng)前時(shí)間到指定時(shí)刻前的毫秒數(shù)
* @param hour 指定時(shí)刻的小時(shí)
* @param min 指定時(shí)刻的分鐘
* @param sec 指定時(shí)刻的秒
* @param mill 指定時(shí)刻的毫秒
* @return
*/
public static long getMillsecBeforeMoment(int hour,int min,int sec,int mill){
return getMillisecBetweenDate(new Date(),getMoment(hour,min,sec,mill));
}
/**
* 獲取兩個(gè)日期之間的毫秒數(shù)
* @param before
* @param after
* @return
*/
public static long getMillisecBetweenDate(Date before, Date after){
long beforeTime = before.getTime();
long afterTime = after.getTime();
return afterTime - beforeTime;
}
/**
* 獲取當(dāng)天的某一時(shí)刻Date
* @param hour 24小時(shí)
* @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. 總結(jié)
這里有個(gè)地方要注意,就是redis 設(shè)置過期時(shí)間后,重新set會(huì)清除過期效果, 重新變成永久狀態(tài), 所以需要每次都pexpire()
redis中還有一個(gè)方法:incr(),每次調(diào)用這個(gè)方法,都會(huì)讓一個(gè)鍵的值+1,如果沒有這個(gè)鍵,會(huì)初始為0再+1. 適合做計(jì)數(shù)器, 也能再這個(gè)案例中使用, 但是我這里只是希望登錄失敗的時(shí)候才計(jì)數(shù)+1 , 登錄之前直接判斷count, 所以使用了傳統(tǒng)的get(),set(). 有興趣的同學(xué)可以去詳細(xì)了解.
您可能感興趣的文章:- redis 實(shí)現(xiàn)登陸次數(shù)限制的思路詳解
- Redis實(shí)戰(zhàn)記錄之限制操作頻率
- PHP實(shí)現(xiàn)redis限制單ip、單用戶的訪問次數(shù)功能示例
- php 使用redis鎖限制并發(fā)訪問類示例
- 在Redis數(shù)據(jù)庫中實(shí)現(xiàn)分布式速率限制的方法