java web 手机验证_javaWeb发送手机短信验证码(云通讯)
/*** 2>向手機發送驗證碼
*@paramrequest( telephone 手機號碼)
*@return向前臺返回的結果*/
public voidsendSmsIdentifyingCode() {
String phone_no=getRequest().getParameter("phone_no");
String templateId=getRequest().getParameter("templateId");//request.getParameter("phone_no");//設置多少分鐘內短信有效(單位:分鐘)
int timeOutMin = 5;
String randomInt= creatNum()+"";//生成驗證隨機數
ObjectNode smsResult =sendInfo(phone_no, timeOutMin, randomInt , templateId );//電話號碼--有效時間--驗證碼隨機數--模板ID
System.out.println(randomInt);
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html");
PrintWriter out= null;try{
out=response.getWriter();
}catch(IOException e) {
e.printStackTrace();
}if (smsResult == null) {
out.println(ResultMessageUtils.creatJsonResultMessage(CoreStatus.RESULT_900009));
}else if (smsResult.get("statusCode").textValue().equals("000000")) {
Map telephoneCode = new HashMap<>();
String code= smsResult.get("randomNum").asText().replace("\"", "");
telephoneCode.put("code", code);
telephoneCode.put("time", String.valueOf(DateUtils.getSystemDate().getTime()));
telephoneCode.put("message", "Success");//存入Redis中
cacheClient.setObject("tel" +phone_no, telephoneCode);
out.print( ResultMessageUtils.creatJsonResultMessage(CoreStatus.RESULT_000000));
}else{
ObjectNode result;
result=JsonUtils.createObjectNode();
result.put("code", smsResult.get("statusCode").textValue());
result.put("message", smsResult.get("statusMsg").textValue());
result.put("datatime", DateUtils.getDefaultDateTimeToString());//return result.toString();
out.println(result.toString());
}
}/*** 向手機發送信息
*
*@paramphone_no
*@paramtimeOutMin(可以為空)
*@paramrandomNum
*@parammsm_type
*@paramtemplateId 短信模板Id
*@returnsmsResult*/
publicObjectNode sendInfo(String phone_no, Integer timeOutMin, String randomNum,String templateId) {//Map telephoneMap = (Map) cacheClient.getObject("tel" + phone_no);
List messageSubstitutes = newArrayList();if (timeOutMin != null) {
messageSubstitutes.add(String.valueOf(randomNum));
messageSubstitutes.add(String.valueOf(timeOutMin));
}
List telephoneList = new ArrayList<>();
telephoneList.add(phone_no);
String smsResultString=smsService.sendSms(telephoneList, templateId, messageSubstitutes);
ObjectNode smsResult=(ObjectNode) JsonUtils.stringToJsonObject(smsResultString);
smsResult.put("randomNum", randomNum.replace("\"", ""));returnsmsResult;
}/*** 生成隨機驗證碼(密碼)
*
*@return
*/
publicInteger creatNum() {int randomInt = new Random().nextInt(900000) + 100000;//設置短信發送內容
returnrandomInt;
}
總結
以上是生活随笔為你收集整理的java web 手机验证_javaWeb发送手机短信验证码(云通讯)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中的reader_java中Bu
- 下一篇: word转换成pdf java代码_ja