好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > redis 實現登陸次數限制的思路詳解

redis 實現登陸次數限制的思路詳解

熱門標簽:高碑店市地圖標注app 南京手機外呼系統廠家 400電話辦理的口碑 b2b外呼系統 廊坊外呼系統在哪買 地圖標注工廠入駐 四川穩定外呼系統軟件 一個地圖標注多少錢 臺灣電銷

title: redis-login-limitation 

利用 redis 實現登陸次數限制, 注解 + aop, 核心代碼很簡單.

基本思路

比如希望達到的要求是這樣: 在 1min 內登陸異常次數達到5次, 鎖定該用戶 1h

那么登陸請求的參數中, 會有一個參數唯一標識一個 user, 比如 郵箱/手機號/userName

用這個參數作為key存入redis, 對應的value為登陸錯誤的次數, string 類型, 并設置過期時間為 1min. 當獲取到的 value == "4" , 說明當前請求為第 5 次登陸異常, 鎖定.

所謂的鎖定, 就是將對應的value設置為某個標識符, 比如"lock", 并設置過期時間為 1h

核心代碼

定義一個注解, 用來標識需要登陸次數校驗的方法

package io.github.xiaoyureed.redispractice.anno;
import java.lang.annotation.*;
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RedisLimit {
  /**
   * 標識參數名, 必須是請求參數中的一個
   */
  String identifier();
  /**
   * 在多長時間內監控, 如希望在 60s 內嘗試
   * 次數限制為5次, 那么 watch=60; unit: s
   */
  long watch();
  /**
   * 鎖定時長, unit: s
   */
  long lock();
  /**
   * 錯誤的嘗試次數
   */
  int times();
}

編寫切面, 在目標方法前后進行校驗, 處理...

package io.github.xiaoyureed.redispractice.aop;
@Component
@Aspect
// Ensure that current advice is outer compared with ControllerAOP
// so we can handling login limitation Exception in this aop advice.
//@Order(9)
@Slf4j
public class RedisLimitAOP {
  @Autowired
  private StringRedisTemplate stringRedisTemplate;
  @Around("@annotation(io.github.xiaoyureed.redispractice.anno.RedisLimit)")
  public Object handleLimit(ProceedingJoinPoint joinPoint) {
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    final Method   method     = methodSignature.getMethod();
    final RedisLimit redisLimitAnno = method.getAnnotation(RedisLimit.class);// 貌似可以直接在方法參數中注入 todo
    final String identifier = redisLimitAnno.identifier();
    final long  watch   = redisLimitAnno.watch();
    final int  times   = redisLimitAnno.times();
    final long  lock    = redisLimitAnno.lock();
    // final ServletRequestAttributes att       = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
    // final HttpServletRequest    request     = att.getRequest();
    // final String          identifierValue = request.getParameter(identifier);
    String identifierValue = null;
    try {
      final Object arg      = joinPoint.getArgs()[0];
      final Field declaredField = arg.getClass().getDeclaredField(identifier);
      declaredField.setAccessible(true);
      identifierValue = (String) declaredField.get(arg);
    } catch (NoSuchFieldException e) {
      log.error(">>> invalid identifier [{}], cannot find this field in request params", identifier);
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    if (StringUtils.isBlank(identifierValue)) {
      log.error(">>> the value of RedisLimit.identifier cannot be blank, invalid identifier: {}", identifier);
    }
    // check User locked
    final ValueOperationsString, String> ssOps = stringRedisTemplate.opsForValue();
    final String             flag = ssOps.get(identifierValue);
    if (flag != null  "lock".contentEquals(flag)) {
      final BaseResp result = new BaseResp();
      result.setErrMsg("user locked");
      result.setCode("1");
      return new ResponseEntity>(result, HttpStatus.OK);
    }
    ResponseEntity result;
    try {
      result = (ResponseEntity) joinPoint.proceed();
    } catch (Throwable e) {
      result = handleLoginException(e, identifierValue, watch, times, lock);
    }
    return result;
  }
  private ResponseEntity handleLoginException(Throwable e, String identifierValue, long watch, int times, long lock) {
    final BaseResp result = new BaseResp();
    result.setCode("1");
    if (e instanceof LoginException) {
      log.info(">>> handle login exception...");
      final ValueOperationsString, String> ssOps = stringRedisTemplate.opsForValue();
      Boolean                exist = stringRedisTemplate.hasKey(identifierValue);
      // key doesn't exist, so it is the first login failure
      if (exist == null || !exist) {
        ssOps.set(identifierValue, "1", watch, TimeUnit.SECONDS);
        result.setErrMsg(e.getMessage());
        return new ResponseEntity>(result, HttpStatus.OK);
      }
      String count = ssOps.get(identifierValue);
      // has been reached the limitation
      if (Integer.parseInt(count) + 1 == times) {
        log.info(">>> [{}] has been reached the limitation and will be locked for {}s", identifierValue, lock);
        ssOps.set(identifierValue, "lock", lock, TimeUnit.SECONDS);
        result.setErrMsg("user locked");
        return new ResponseEntity>(result, HttpStatus.OK);
      }
      ssOps.increment(identifierValue);
      result.setErrMsg(e.getMessage() + "; you have try " + ssOps.get(identifierValue) + "times.");
    }
    log.error(">>> RedisLimitAOP cannot handle {}", e.getClass().getName());
    return new ResponseEntity>(result, HttpStatus.OK);
  }
}

這樣使用:

package io.github.xiaoyureed.redispractice.web;
@RestController
public class SessionResources {
  @Autowired
  private SessionService sessionService;
  /**
   * 1 min 之內嘗試超過5次, 鎖定 user 1h
   */
  @RedisLimit(identifier = "name", watch = 30, times = 5, lock = 10)
  @RequestMapping(value = "/session", method = RequestMethod.POST)
  public ResponseEntityLoginResp> login(@Validated @RequestBody LoginReq req) {
    return new ResponseEntity>(sessionService.login(req), HttpStatus.OK);
  }
}

references

https://github.com/xiaoyureed/redis-login-limitation

總結

以上所述是小編給大家介紹的redis 實現登陸次數限制的思路詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

您可能感興趣的文章:
  • PHP+Redis 消息隊列 實現高并發下注冊人數統計的實例
  • 利用Redis統計網站在線活躍用戶的方法
  • PHP使用redis實現統計緩存mysql壓力的方法
  • Redis中統計各種數據大小的方法
  • 基于redis實現token驗證用戶是否登陸
  • redis開啟和禁用登陸密碼校驗的方法
  • 基于Redis位圖實現系統用戶登錄統計

標簽:泰州 定州 畢節 南寧 甘南 拉薩 伊春 河源

巨人網絡通訊聲明:本文標題《redis 實現登陸次數限制的思路詳解》,本文關鍵詞  redis,實現,登陸,次數,限制,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《redis 實現登陸次數限制的思路詳解》相關的同類信息!
  • 本頁收集關于redis 實現登陸次數限制的思路詳解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 国产猛男GayGayXXgv| 美女的隐私部位的视频软件| 被老师摁在教室cao到爽| h嗯啊~乱女h| 国产白袜男gaychina霸道太子| 久久久久无码精品国产高潮| 国内精品视频免费观看| 97综合久久| 日本女人www| 男男污污文| 高潮A片www张柏芝陈冠希| 国产精品久久久久精品综合紧男同| 妲己秀人网| 法国啄木鸟大尺度mp4| 亚洲精品一区二区三区2023年最新| 亚洲黄色成人| 黄毛片在线观看| 泳池激情2韩国无删减版 | 国产91??在线播放九色000| 日韩丝袜亚洲国产欧美一区| 91日韩一区二区三区| 亚洲黄色影视| 免费看污的网站| 大胸美女www| 51福利视频| 欧美性视频在线| 丰满女人大陰户XXX| 久久久国产成人精品| 日韩久操| 狠狠干香蕉| 爽?好紧?别夹?喷水在线观看 | 国产最爽乱婬视频播放| 男女激情很爽很黄很色视频| 国产一级特黄A片毛片小网站| 公交车上扒开腿做爰H漫画| 黄色仓库在线播放| av线上观看| 好爽?要高潮了?深点快陈澄视频| 精品在线网站| 粉色视频免费下载观看| 文轩河马的秘密河润滑剂|