public ResponseResult login(LoginDto loginDto) {
/**\
* 如果用户名与密码存在则进行正常登录
*/
if(StringUtils.isNotBlank(loginDto.getPhone()) && StringUtils.isNotBlank(loginDto.getPassword())){
//1.查看是否达到限流阈值(5分钟之内3次请求)
//先对5分钟之前推积的无用数据进行清除
long timestamp = System.currentTimeMillis() - (5 * 60 * 1000); // 五分钟前的时间戳
cacheService.zRemoveRangeByScore("User:Login:"+loginDto.getPhone(),Double.NEGATIVE_INFINITY,timestamp);
System.err.println("当前五分钟之前的时间戳"+timestamp);
//查询5分钟之内的数据
Set<String> strings = cacheService.zReverseRangeByScore("User:Login:" + loginDto.getPhone(), timestamp, new Date().getTime());
if(strings.size()<3){
cacheService.zAdd("User:Login:"+loginDto.getPhone(), String.valueOf(new Date().getTime()),new Date().getTime());
}else{
return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"当前登录次数过多,请于一分钟后重试");
}
}
文章讲述了在用户登录过程中,通过Redis实现的限流策略,每5分钟内限制3次登录请求,超过则返回错误提示。系统首先清除5分钟前的无用数据,然后检查最近登录尝试,确保限流效果。
4788

被折叠的 条评论
为什么被折叠?



