JAVA短信验证登录

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;
}

标签: 缓存, 验证码, 短信
0
0
« 上一篇: git 配置SSH免密
» 下一篇: SQL 日期筛选的两种方式
posted @ 2016-07-21 17:26 亢东升 阅读( 5397) 评论( 4) 编辑 收藏

转载于:https://www.cnblogs.com/jpfss/p/7122466.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值