JAVA短信驗證登錄


短信驗證登陸

1、點擊觸發,以電話號碼為參數調用發送驗證登錄短信方法

2、默認模板為驗證模板
生成6位驗證碼

3、將生成的驗證碼和手機號碼放入緩存,(已經設置好緩存存放時間)

4、調用發送模板短信方法發送短信(設置好短信中驗證碼有效的時間)

5、點擊觸發登陸,調用對應驗證登錄函數 ,以電話號碼和驗證碼為參數

6、校驗緩存中對應保留的信息
如果一致,登陸成功;
登陸不成功是返回原因(1、超時 2、驗證碼輸入錯誤)

/**
*發送驗證碼短信
*參數:手機號碼
*/
public void sendVerifyLoginSMS(String to) { 

Jedis cache = sendSMSCache.getResource();

//生成六位驗證碼
String charValue = "";
for (int i = 0; i < 6; i++) {
char c = (char) (randomInt(0, 9) + '0');
charValue += String.valueOf(c);
}

//將生成的六位驗證碼和傳進來的手機號碼存入緩存,時間90S
try{
Pipeline pipeline = cache.pipelined();
pipeline.set("CACHE" + to, charValue);
pipeline.expire("CACHE", 90);
pipeline.sync();
}
finally
{
if (cache != null)
{
cache.close();
}
}
//驗證碼和顯示時間
String[] datas = {charValue,"1.5"};

//短信模板

String templateId = "1";
sMSClientBiz.sendSMS(to, templateId, datas);
}

 

/**
* 生成隨機數
* 
* */
public int randomInt(int from, int to) {
Random r = new Random();
return from + r.nextInt(to - from);
}

/**
* 驗證短信驗證碼登陸
* 
* */

public boolean verifySMS(String to, String verifyCode) {

Jedis cache = sendSMSCache.getResource();
// 緩存中驗證碼
String cacheVerifyCode;

try{
cacheVerifyCode = cache.get("CACHE" + to); 
}
finally
{
if (cache != null)
{
cache.close();
}
}

//如果輸入的驗證碼和緩存中的驗證碼一致,則驗證成功
if(verifyCode ==cacheVerifyCode ){
return true;
}else
return false;
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM