当前位置:网站首页>Redis存储验证码
Redis存储验证码
2022-08-10 15:58:00 【qq_45860901】
import redis.clients.jedis.Jedis;
import java.util.Random;
public class RedisConfig {
static String code = null;
public static void main(String[] args) {
//1,先判断该手机验证次数几次了,有没有超时间
//2,然后生成随机码
//3,再判断填写的随机码是否和生成的随机码相同。
verifyPhone("12345678912");
}
public static void verifyPhone(String phone){
Jedis jedis = new Jedis("192.168.110.110", 6379);
String keyPhone = "verify" + phone + "count";
String codePhone = phone + "code";
String s = jedis.get(keyPhone);
//第一次,没有key的记录,所以需要判断null
if(s == null){
jedis.setex(keyPhone, 24*60*60, "1");
}else if(Integer.valueOf(s) < 3){
//第二,三次,有记录了
jedis.incr(keyPhone);
}else {
System.out.println("今天已经超过三次");
jedis.close();
return;
}
//如果还在三次之内可以,可以执行这个获取值的操作
code = getRandomCode();
jedis.setex(codePhone, 60, code);
jedis.close();
}
public static String getRandomCode(){
Random random = new Random();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 6; ++i){
int tmp = random.nextInt(10);
builder.append(tmp);
}
return builder.toString();
}
}
边栏推荐
猜你喜欢
随机推荐
Detailed understanding of all built-in functions (Part 2)
Zotero 开源文献管理工具
推荐几款最好用的MySQL开源客户端,建议收藏!
C#去水印软件源代码
嵌入式开发:嵌入式基础——使用指针数组映射外设
LeetCode-101. Symmetric Tree
如何将静图变gif动图?教你jpg合成gif的方法
I met a 25k+ from Tencent, he let me see what kind of basic ceiling
MySQL batch update and batch update method of different values of multiple records
并发容器线程安全应对之道
openpyxl绘制堆叠图
力扣+牛客--刷题记录
一文带你了解 HONOR Connect
Mobileye joins hands with Krypton to open a new chapter in advanced driver assistance through OTA upgrade
spark面试常问问题
FP6378AS5CTR SOT - 23-5 effective 1 mhz2a synchronous buck regulator
【每日一题】【leetcode】25. 数组-旋转数组的最小数字
LeetCode-876. Middle of the Linked List
简述 Mock 接口测试
web安全入门-Kill Chain测试流程









