如何使用Java redis實(shí)現(xiàn)發(fā)送手機(jī)驗(yàn)證碼功能
要求:
1、輸入手機(jī)號,點(diǎn)擊發(fā)送后隨機(jī)生成6位數(shù)字碼,2分鐘有效
2、輸入驗(yàn)證碼,點(diǎn)擊驗(yàn)證,返回成功或失敗
3、每個手機(jī)號每天只能輸入3次
代碼如下
import redis.clients.jedis.Jedis;
import java.util.Random;
public class ValidationTest {
public static void main(String[] args) {
//getValidation("15005076571");
//checkValidation("769897","15005076571");
}
static void getValidation(String tel) {
//主機(jī)、端口
Jedis jedis = new Jedis("myhost", 6379);
//密碼
jedis.auth("mypassword");
try {
//獲取電話號碼
String phoneNo = tel;
//本人用1庫進(jìn)行測試
jedis.select(1);
String countKey = phoneNo + ":count";
String codeKey = phoneNo + ":code";
//獲取指定的電話號碼發(fā)送的驗(yàn)證碼次數(shù)
String cnt = jedis.get(countKey);
//對次數(shù)進(jìn)行判斷
if (cnt == null) {
//沒有發(fā)送過驗(yàn)證碼
jedis.setex(countKey, 60 * 60 * 24, "1");
//發(fā)送驗(yàn)證碼,假設(shè)生成的驗(yàn)證碼
StringBuffer code = new StringBuffer();
for (int i = 0; i < 6; i++) {
code.append(new Random().nextInt(10));
}
System.out.println("code:" + code);
//緩存中添加驗(yàn)證碼
jedis.setex(codeKey, 60 * 2, code.toString());
} else {
if (Integer.parseInt(cnt) < 3) {
//發(fā)送驗(yàn)證碼,假設(shè)生成的驗(yàn)證碼
StringBuffer code = new StringBuffer();
for (int i = 0; i < 6; i++) {
code.append(new Random().nextInt(10));
}
System.out.println("code:" + code);
//緩存中添加驗(yàn)證碼
jedis.setex(codeKey, 60 * 2, code.toString());
//遞增手機(jī)發(fā)送數(shù)量
jedis.incr(countKey);
} else {
//返回超出3次,禁止發(fā)送
System.out.println("超出3次,禁止發(fā)送");
}
}
} catch (Exception e) {
//這邊其實(shí)是需要回滾下redis
e.printStackTrace();
} finally {
//關(guān)閉redis
if (jedis != null) {
jedis.close();
}
}
}
static void checkValidation(String code, String tel) {
Jedis jedis = null;
try {
jedis = new Jedis("myhost", 6379);
//密碼
jedis.auth("mypassword");
jedis.select(1);
String codeKey = tel + ":code";
String validation = jedis.get(codeKey);
if (validation == null) {
System.out.println("驗(yàn)證碼未發(fā)送或者失效");
} else {
if (validation.equals(code)) {
System.out.println("驗(yàn)證成功");
} else {
System.out.println("驗(yàn)證失敗");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
}
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中BigDecimal的equals方法和compareTo方法的區(qū)別詳析
這篇文章主要給大家介紹了關(guān)于Java中BigDecimal的equals方法和compareTo方法區(qū)別的相關(guān)資料,對于BigDecimal的大小比較,用equals方法的話會不僅會比較值的大小,還會比較兩個對象的精確度,而compareTo方法則不會比較精確度,只比較數(shù)值的大小,需要的朋友可以參考下2023-11-11
Java線程編程中Thread類的基礎(chǔ)學(xué)習(xí)教程
這篇文章主要介紹了Java線程編程中Thread類的基礎(chǔ)學(xué)習(xí)教程,Thread類包含諸多操作線程的方法,非常重要,需要的朋友可以參考下2015-12-12
Maven 多模塊父子工程的實(shí)現(xiàn)(含Spring Boot示例)
這篇文章主要介紹了Maven 多模塊父子工程的實(shí)現(xiàn)(含Spring Boot示例),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Spring?Security實(shí)現(xiàn)添加圖片驗(yàn)證功能
這篇文章主要為大家介紹了Spring?Security實(shí)現(xiàn)添加圖片驗(yàn)證功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
SpringBoot 整合Lock4j 分布式鎖深度使用案例講解
本文介紹SpringBoot分布式鎖框架Lock4j,支持Redis、Zookeeper等多種存儲實(shí)現(xiàn),通過注解和AOP簡化鎖管理,提供并發(fā)控制、防止重復(fù)提交等場景應(yīng)用,并支持自定義執(zhí)行器和key生成策略,適用于高并發(fā)和異構(gòu)系統(tǒng)需求,感興趣的朋友一起看看吧2025-07-07
Spring Boot從main方法到內(nèi)嵌Tomcat的全過程(自動化流程)
SpringBoot啟動始于main方法,創(chuàng)建SpringApplication實(shí)例,初始化上下文,準(zhǔn)備環(huán)境,刷新容器并啟動內(nèi)嵌Tomcat,通過自動配置加載必要類完成應(yīng)用部署,本文給大家介紹Spring Boot從main方法到內(nèi)嵌Tomcat的全過程,感興趣的朋友一起看看吧2025-07-07

