溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于Redis如何實(shí)現(xiàn)每日登錄失敗次數(shù)限制的方法

發(fā)布時間:2021-02-05 14:19:16 來源:億速云 閱讀:887 作者:小新 欄目:數(shù)據(jù)庫

這篇文章主要介紹基于Redis如何實(shí)現(xiàn)每日登錄失敗次數(shù)限制的方法,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

1. 思路

下面是我以前寫的代碼,沒考慮高并發(fā)場景。如果是高并發(fā)場景下,要考慮到redis的set方法覆蓋值問題,可以使用incr來替代get,set保證數(shù)據(jù)安全

通過redis記錄登錄失敗的次數(shù),以用戶的username為key

每次收到登錄的請求時,都去redis查詢登錄次數(shù)是否已經(jīng)大于等于我們設(shè)置的限制次數(shù), 是的話直接返回

2. 代碼

前臺登錄和后臺查詢數(shù)據(jù)庫的代碼省略

2.1 controller

我這里使用的Jboot, 獲取redisTemplate的方式是Jboot.me().getRedis(), spring的話用jedisTemplate就行.

// 如果用戶輸入賬號密碼有效登錄超過限制次數(shù),24小時禁止登錄
 // 設(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)超過限制,請明天再試。");
     ajaxJson.setSuccess(false);
     logger.error("賬號為【"+account+"】的用戶單日登錄次數(shù)超過上限");
     render(callback, gson.toJson(ajaxJson));
     return;
   }
 }
//... 去數(shù)據(jù)庫根據(jù)username查詢user對象
 if (user != null) {
   // 往redis中增加登錄失敗的次數(shù)
   Integer newCount = IncrFailLoginCount(key,count);
   logger.error("賬號為【"+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中存儲的鍵
 * @param count 已經(jīng)登錄失敗的次數(shù)
 * @return count 登錄失敗次數(shù)
 */
private Integer IncrFailLoginCount(String key,Integer count) {
  JbootRedis jr = Jboot.me().getRedis();
  count++;
  //設(shè)置過期時間為今晚23點(diǎn)59分59秒
  long timeInMillis = DateUtils.getMillsecBeforeMoment(23, 59, 59, 999);
  if (timeInMillis < 100){
    // 避免在最后一秒的時候登錄導(dǎo)致過期時間過小甚至為負(fù)數(shù)
    timeInMillis = 1000*60;
  }
  // 設(shè)置過期時間
  jr.set(key,count);
  //這里注意順序, 先set再pexpire
  jr.pexpire(key,timeInMillis);
  return count;
}

這里用到了時間的一個工具類, 具體代碼如下:

/**
* 獲取當(dāng)前時間到指定時刻前的毫秒數(shù)
* @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));
}
/**
* 獲取兩個日期之間的毫秒數(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)天的某一時刻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();
}

這里有個地方要注意,就是redis 設(shè)置過期時間后,重新set會清除過期效果, 重新變成永久狀態(tài), 所以需要每次都pexpire()
redis中還有一個方法:incr(),每次調(diào)用這個方法,都會讓一個鍵的值+1,如果沒有這個鍵,會初始為0再+1. 適合做計(jì)數(shù)器, 也能再這個案例中使用, 但是我這里只是希望登錄失敗的時候才計(jì)數(shù)+1 , 登錄之前直接判斷count, 所以使用了傳統(tǒng)的get(),set(). 

以上是“基于Redis如何實(shí)現(xiàn)每日登錄失敗次數(shù)限制的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI