redis?手機驗證碼實現(xiàn)示例
更新時間:2022年03月11日 11:11:41 作者:gh_xiaohe
本文主要介紹了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中,設置過期時間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ā)送 //設置發(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 以內的值 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" 校驗
到此這篇關于redis 手機驗證碼實現(xiàn)示例的文章就介紹到這了,更多相關redis 手機驗證碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
redis數(shù)據(jù)一致性的實現(xiàn)示例
所謂的redis數(shù)據(jù)一致性即當進行修改或者保存、刪除之后,redis中的數(shù)據(jù)也應該進行相應變化,本文主要介紹了redis數(shù)據(jù)一致性,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03RedisDesktopManager無法遠程連接Redis的完美解決方法
下載RedisDesktopManager客戶端,輸入服務器IP地址,端口(缺省值:6379);點擊Test Connection按鈕測試連接,連接失敗,怎么回事呢?下面小編給大家?guī)砹薘edisDesktopManager無法遠程連接Redis的完美解決方法,一起看看吧2018-03-03Redis實現(xiàn)短信登錄的企業(yè)實戰(zhàn)
本文主要介紹了Redis實現(xiàn)短信登錄的企業(yè)實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07Redis分布式鎖python-redis-lock使用方法
這篇文章主要介紹了Redis分布式鎖python-redis-lock使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11