很多地方都要用到重试次数限制,不然就会被暴力破解。比如登录密码。

下面不是完整代码,只是伪代码,提供一个思路。

第一种(先声明,这样写有个bug)

import java.text.MessageFormat;

public class Demo {

    /**
* 限制次数
*/
private static final Integer MAX_TIMES = 5;
/**
* 锁定时间(也是key的失效时间)
*/
private static final Integer LIMIT_TIME = 3;
/**
* key
*/
private static final String LIMIT_TIMES_KEY = "LimitTimesKey:%s"; public void deal(String phone, String password){
// 用户id
Long userId = 6815356L;
// 组装key
String key = MessageFormat.format(LIMIT_TIMES_KEY, userId);
// 先获取key对应的value
String s = redisService.get(key);
int currentTimes = s != null ? Integer.parseInt(s) : 0;
// 如果当前次数为[LIMIT_TIMES]次或以上,则抛异常
if(currentTimes >= MAX_TIMES){
throw new RuntimeException("请在"+ LIMIT_TIME +"分钟后继续尝试");
} // TODO 做其它逻辑,比如登录操作
Integer code = userService.login(phone, password); // 比如密码不正确的状态码是10086
if(code == 10086){
// 失败次数+1
int thisTimes = currentTimes + 1;
String value = String.valueOf(thisTimes); // 如果小于最大限制
if(thisTimes < MAX_TIMES){
redisService.set(key, value);
throw new RuntimeException("原密码错误,还可以重试"+ (MAX_TIMES - thisTimes) +"次");
}else{
redisService.setex(key, LIMIT_TIME*60, value);
throw new RuntimeException("原密码错误,请在"+ LIMIT_TIME +"分钟后继续尝试");
}
} // 登陆成功,清理redis
redisService.del(key);
}
}

以上代码思路:

以上代码有什么问题呢:当失败次数小于最大限制,那里直接set了一个值,没有设置失效时间。如果用户失败了一次就不再尝试了,那么我们设置的key就会永远存在;同时用户在n年后再去登陆,他拥有的重试次数是凌驾于n年前的重试次数之上的,也就是说我今年浪费了1次重试次数,还剩下4次,我明年再重试,我能够重试的次数就不是5而是4了,因为我的重试次数记录一直存在。

import java.text.MessageFormat;

public class Demo {

    /**
* 限制次数
*/
private static final Integer MAX_TIMES = 5;
/**
* 锁定时间(也是key的失效时间)
*/
private static final Integer LIMIT_TIME = 3;
/**
* key
*/
private static final String LIMIT_TIMES_KEY = "LimitTimesKey:%s"; public void deal(String phone, String password){
// 用户id
Long userId = 6815356L;
// 组装key
String key = MessageFormat.format(LIMIT_TIMES_KEY, userId);
// 先获取key对应的value
String s = redisService.get(key);
int currentTimes = s != null ? Integer.parseInt(s) : 0;
// 如果当前次数为[LIMIT_TIMES]次或以上,则抛异常
if(currentTimes >= MAX_TIMES){
throw new RuntimeException("请在"+ LIMIT_TIME +"分钟后继续尝试");
} // TODO 做其它逻辑,比如登录操作
Integer code = userService.login(phone, password); // 比如密码不正确的状态码是10086
if(code == 10086){
// 失败次数+1
int thisTimes = currentTimes + 1;
String value = String.valueOf(thisTimes);
// 设置值,重点是失效时间
redisService.setex(key, LIMIT_TIME*60, value);
// 如果小于最大限制
if(thisTimes < MAX_TIMES){
throw new RuntimeException("原密码错误,还可以重试"+ (MAX_TIMES - thisTimes) +"次");
}else{
throw new RuntimeException("原密码错误,请在"+ LIMIT_TIME +"分钟后继续尝试");
}
} // 登陆成功,清理redis
redisService.del(key);
}
}

  

改进之后的思路如下:

Redis解决“重试次数”场景的实现思路的更多相关文章

  1. redis数据类型及使用场景

    Redis数据类型  String: Strings 数据结构是简单的key-value类型,value其实不仅是String,也可以是数字. 常用命令:  set,get,decr,incr,mge ...

  2. retry重试常见场景及实现

    当我们的代码是有访问网络相关的操作时,比如http请求或者访问远程数据库,经常可能会发生一些错误,有些错误可能重新去发送请求就会成功,本文分析常见可能需要重试的场景,并最后给出python代码实现. ...

  3. OkHttp自定义重试次数

    本文主要应用了OkHttp的Interceptor来实现自定义重试次数 虽然OkHttp自带retryOnConnectionFailure(true)方法可以实现重试,但是不支持自定义重试次数,所以 ...

  4. 170222、使用Spring Session和Redis解决分布式Session跨域共享问题

    使用Spring Session和Redis解决分布式Session跨域共享问题 原创 2017-02-27 徐刘根 Java后端技术 前言 对于分布式使用Nginx+Tomcat实现负载均衡,最常用 ...

  5. SpringCloud Feign 之 超时重试次数探究

    SpringCloud Feign 之 超时重试次数探究 上篇文章,我们对Feign的fallback有一个初步的体验,在这里我们回顾一下,Fallback主要是用来解决依赖的服务不可用或者调用服务失 ...

  6. redis 实现登陆次数限制

    title: redis-login-limitation 利用 redis 实现登陆次数限制, 注解 + aop, 核心代码很简单. 基本思路 比如希望达到的要求是这样: 在 1min 内登陆异常次 ...

  7. Redis 客户端重试指南

    本作品采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可. 在互联网服务中,特别是在云环境下,网络及硬件环境复杂,所有应用程序都可能遇到暂时性故障.暂时性故障包括瞬时的网络抖动,服务暂时不可 ...

  8. Redis数据库的使用场景介绍(避免误用Redis)

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/122.html?1455854235 Redis 是目前 NoSQL 领域 ...

  9. 豌豆夹Redis解决方式Codis源代码剖析:Proxy代理

    豌豆夹Redis解决方式Codis源代码剖析:Proxy代理 1.预备知识 1.1 Codis Codis就不详细说了,摘抄一下GitHub上的一些项目描写叙述: Codis is a proxy b ...

随机推荐

  1. JS实现继承的几种方法

    父类: // 定义一个动物的类 function Animal (name) { // 属性 this.name = name || 'Animal'; // 实例方法 this.sleep = fu ...

  2. Using the Repository and Unit Of Work Pattern in .net core

    A typical software application will invariably need to access some kind of data store in order to ca ...

  3. 读RAM时的时序风险

    读RAM时的时序有两个风险:1.数据已经存储好,读所需的时间.2.数据同时更新,读所需的时间节点.对于前者,只要延时足够节拍就行.对于后者,还必须要考虑数据建立的时间,同样延时的准备可能会因为数据尚未 ...

  4. python字符串转整形异常

    python字符串转整形异常 问题 在使用int("xx")转化字符串为整形时,如果字符串是float形式,这样转化会异常 int('3.0') Traceback (most r ...

  5. UDF——查找单元的相邻单元

    Fluent版本:Fluent 19.2 Visual Studio版本:Visual Studio 2013 测试文件及源码下载链接: https://pan.baidu.com/s/1AZ59hs ...

  6. 如何将 普通代码变成 java lamband表达式

    public static void main(String[] args) { Map<String, String> map = new HashMap<>(); List ...

  7. elasticsearch-py 解决 too_long_frame_exception 问题

    elasticsearch-py 解决 too_long_frame_exception 问题 老大让我搞一搞数据统计,配环境时遇到个奇葩错误,记录一下,希望能帮助到某些人. 我需要安装 Elasti ...

  8. patchUpload.vue?5e29:406 Uncaught (in promise) DOMException: Failed to execute 'readAsArrayBuffer' on 'FileReader': The object is already busy reading Blobs.

    patchUpload.vue?5e29:406 Uncaught (in promise) DOMException: Failed to execute 'readAsArrayBuffer' o ...

  9. git push ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/Operater9/guest' hint: Updates were rejected because the tip of your current bra

    推送本地代码到github报错 git push报错 ! [rejected] master -> master (non-fast-forward) error: failed to push ...

  10. Python3基础 内置函数 dir 查询对象的方法列表

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...