redis?手機驗證碼實現(xiàn)示例
本文主要介紹了redis 手機驗證碼實現(xiàn)示例,分享給大家,具體如下:

/**
* @author gh Email:@2495140780qq.com
* @Description
* @date 2021-11-10-21:12
*/
public class PhoneCode {
public static void main(String[] args) {
//模擬驗證碼發(fā)送
// verifyCode("13796734562");
//效驗
getRedisCode("13796734562", "740032");
}
//3.驗證碼的校驗
public static void getRedisCode(String phone,String code) {
//從redis中獲取驗證碼
Jedis jedis = new Jedis("127.0.0.1",6379);
//驗證碼key
String codeKey = "VerifyCode"+phone+":code";
String redisCode = jedis.get(codeKey);
//判斷
if(redisCode.equals(code)) {
System.out.println("成功");
}else {
System.out.println("失敗");
}
jedis.close();
}
//2.每個手機每天只能發(fā)送三次,驗證碼放到redis中,設(shè)置過期時間60
public static void verifyCode(String phone) {//手機號
//鏈接redis
Jedis jedis = new Jedis("127.0.0.1",6379);
//拼接key
//手機發(fā)送次數(shù)
String countKey = "VerifyCode" + phone + ":count";//規(guī)則保證唯一,規(guī)則自己訂
//驗證碼key
String codeKey = "VerifyCode" + phone + ":code";
//每個手機每天只能發(fā)送三次
String count = jedis.get(countKey);//手機發(fā)送次數(shù)
if (count == null) {
//沒有發(fā)送次數(shù),第一次發(fā)送
//設(shè)置發(fā)送次數(shù)是1
jedis.setex(countKey, 24*60*60, "1");
}else if (Integer.parseInt(count) <= 2) {
//發(fā)送次數(shù) +1
jedis.incr(countKey);
}else if (Integer.parseInt(count) >2) {
//發(fā)送三次,不能大發(fā)送
System.out.println("今天發(fā)送次數(shù)已經(jīng)超過三次");
jedis.close();
return;
}
//發(fā)送的驗證放到redis中去
String vcode = getCode();
jedis.setex(codeKey,120,vcode);
jedis.close();
}
//1.生成6位的驗證碼
public static String getCode() {
Random random = new Random();
String code = "";
for (int i = 0; i < 6; i++) {
int rand = random.nextInt(10); //10 以內(nèi)的值
code += rand;
}
return code;
}
}發(fā)送驗證碼
127.0.0.1:6379> flushdb OK 127.0.0.1:6379> keys * 1) "VerifyCode13796734562:count" 2) "VerifyCode13796734562:code" 127.0.0.1:6379> get VerifyCode13796734562:count # 第一次獲取驗證碼 "1" 127.0.0.1:6379> get VerifyCode13796734562:code # 獲取的驗證碼為 "478121" 127.0.0.1:6379> get VerifyCode13796734562:count "2" 127.0.0.1:6379> get VerifyCode13796734562:code "250610" 校驗

到此這篇關(guān)于redis 手機驗證碼實現(xiàn)示例的文章就介紹到這了,更多相關(guān)redis 手機驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis?緩存淘汰策略和事務(wù)實現(xiàn)樂觀鎖詳情
這篇文章主要介紹了Redis緩存淘汰策略和事務(wù)實現(xiàn)樂觀鎖詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07
使用Redis獲取數(shù)據(jù)轉(zhuǎn)json,解決動態(tài)泛型傳參的問題
這篇文章主要介紹了使用Redis獲取數(shù)據(jù)轉(zhuǎn)json,解決動態(tài)泛型傳參的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Spring Boot整合Redis實現(xiàn)訂單超時處理問題
這篇文章主要介紹了Spring Boot整合Redis實現(xiàn)訂單超時處理,通過這個基本的示例,你可以了解如何使用Spring Boot和Redis來處理訂單超時問題,并根據(jù)需要進行擴展和定制,需要的朋友可以參考下2023-11-11
Windows中Redis安裝配置流程并實現(xiàn)遠(yuǎn)程訪問功能
很多在windows環(huán)境中安裝Redis總是出錯,今天小編抽空給大家分享在Windows中Redis安裝配置流程并實現(xiàn)遠(yuǎn)程訪問功能,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-06-06
Redis migrate數(shù)據(jù)遷移工具的使用教程
這篇文章主要給大家介紹了關(guān)于Redis migrate數(shù)據(jù)遷移工具的使用教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
SpringBoot讀寫Redis客戶端并實現(xiàn)Jedis技術(shù)切換功能
這篇文章主要介紹了SpringBoot讀寫Redis客戶端并實現(xiàn)技術(shù)切換功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01

