注册榛子云
注册取得appId和AppSecret
下载榛子云jar包
代码
@RequestMapping(value="ZhenziDx",method=RequestMethod.POST)
@ResponseBody
public Object ZhenziDx(HttpSession session,@RequestParam("number")String number ) {//number发送的手机号
try {
JSONObject json = null;
//生成6位验证码
int num1=(int)(Math.random()*10000);
String SjNum = String.valueOf(num1);
//发送短信
ZhenziSmsClient client = new ZhenziSmsClient("https://sms_developer.zhenzikj.com","你的appId", "你的AppSecret");
String result = client.send(number, "Af科技,您的验证码为:" + SjNum + ",该码有效期为1分,牛不牛逼");
json = JSONObject.parseObject(result);
if(json.getIntValue("code") != 0) {//发送短信失败
return JSON.toJSONString("fail");
}else {
// 将认证码存入SESSION
session.setAttribute("SjNum ", json);
session.setMaxInactiveInterval(1*60);
return JSON.toJSONString("success");
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}