短信验证

public Object huaweiSendCode(String phone){
Random random = new Random();
String value = String.format("%s%s%s%s%s%s", random.nextInt(10), random.nextInt(10), random.nextInt(10),
random.nextInt(10), random.nextInt(10), random.nextInt(10));
// 多子project场景下使用。regionName(YourMultiSubProjectName)应该填自定义的子project名,如cn-north-1_smn为自己定义的子project名
SmnClient smnClient = new DefaultSmnClient(
"welife",
"welife",
"bc182119",
"cn-south-1");

// 构造请求对象
SmsPublishRequest smnRequest = new SmsPublishRequest();

// 设置参数
smnRequest.setEndpoint("+86"+phone)
.setMessage("您的验证码是:"+value+",请查收")
.setSignId("c4df5378d52b44ebb8c66e5702d2ec48");

// 发送短信
try {
SmsPublishResponse res = smnClient.sendRequest(smnRequest);
String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+phone;
String json = loadJSON(url);
System.out.println(json);
ZoneResult zoneResult=new ZoneResult();
zoneResult.setPhone(phone);
zoneResult.setZoneResult(json);
zoneResultService.save(zoneResult);
if(res.getHttpCode()==200){
//发送验证码成功以后 要把验证码保存起来,登录的时候 校验验证码是否正确
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String firstTime=format.format(new Date());
logger.info("发送验证码:"+phone+"手机号发送时间是 "+firstTime);
String stampValue[]=new String[2];
stampValue[0]=value;
stampValue[1]=firstTime;
map.put(phone,stampValue);
return JsonUtil.successMessage("验证码发送成功");
}
System.out.println("httpCode:" + res.getHttpCode()
+ ",message_id:" + res.getMessageId()
+ ", request_id:" + res.getRequestId()
+ ", errormessage:" + res.getMessage());
} catch (Exception e) {
// 处理异常
e.printStackTrace();
}
return JsonUtil.failMessage("验证码发送失败");
}

/**
* 是否超时
* @param firstTime
* @param lastTime
* @return
*/
private boolean overtimeCode(String firstTime,String lastTime,long overtime){
boolean flag=false;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date one;
Date two;
try {
one = df.parse(firstTime);
two = df.parse(lastTime);
long time1 = one.getTime();
long time2 = two.getTime();
long diff = time2 - time1;
if(diff/1000>overtime){
flag=true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return flag;
}



SimpleDateFormat dft2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str2=dft2.format(new Date());
String time= map.get(phone)[1];
boolean flag=overtimeCode(time,str2,6000);
if(flag){
return JsonUtil.failMessage("验证码失效"+time+"--"+str2);
}
原文地址:https://www.cnblogs.com/duanqiao123/p/8875696.html