- 前台页面:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" id="viewport" name="viewport">
<title></title>
<script src="../js/jquery.min.js"></script>
<style type="text/css">
body,div{
padding: 0;
margin: 0;
}
.wraper{
padding: 100px;
}
.phone{
width: 220px;
height: 40px;
box-sizing: border-box;
outline: none;
padding: 0 10px;
}
.getverify-code-btn{
display: inline-block;
width: 140px;
height: 40px;
line-height: 40px;
text-align: center;
background-color: blue;
border: 1px solid #ccc;
box-sizing: border-box;
vertical-align: middle;
cursor: pointer;
color: #fff;
}
.getverify-code-btn.unabled{
background-color: lightblue;
color: #eee;
cursor: default;
}
</style>
<script type="text/javascript">
var countdown=60;
function settime() {
var obj=document.getElementById("j_getVerifyCode");
if (countdown == 0) {
obj.removeAttribute("disabled");
obj.value="免费获取验证码";
countdown = 60;
return;
} else {
obj.setAttribute("disabled", true);
obj.value="重新发送(" + countdown + ")";
countdown--;
}
setTimeout(function() {
settime();
}
,1000);
}
function sendInfo(){
var phoneReg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/,
flag = phoneReg.test($("#j_phone").val());
if(!flag){
alert("电话号码填写不正确!");
return false;
}
settime();
yzm();
}
function yzm(){
var tel = $("#j_phone").val();
$.post("/Tel0/yzm",{tel:tel},function(result){
alert(tel);
if(result==0){
alert('发送成功,请注意接收');
}else{
alert(result)
}
});
}
</script>
<body>
<form action="/Tel0/checkValidation" method="post">
<div class="wraper">
<h1>获取手机验证码</h1>
手机号:<input type="text" id="j_phone" class="phone" name="telphone">
<input id="j_getVerifyCode" class="getverify-code-btn" type="button" value="免费获取验证码" onclick="sendInfo()" /> <br>
验证码:<input name="yzm" class="phone" type="text" > <br>
<input name="sub" type="submit" value="提交">
</form>
</body>
</html>
- controller层
package com.sdbairui.demo.jwt.Controller;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/Tel0")
public class TelAlibba {
@RequestMapping("/toreg")
public String toreg(){
return "reg";
}
@RequestMapping("/yzm")
@ResponseBody
public void yzm(String tel) throws ClientException {
setNewcode(tel);
String code = Integer.toString(getNewcode());
SendSmsResponse sendSms =sendSms(tel,code);
System.out.println("短信接口返回的数据----------------");
System.out.println("Code=" + sendSms.getCode());
System.out.println("Message=" + sendSms.getMessage());
System.out.println("RequestId=" + sendSms.getRequestId());
System.out.println("BizId=" + sendSms.getBizId());
}
static final String product = "Dysmsapi";
static final String domain = "dysmsapi.aliyuncs.com";
static final String accessKeyId = "LTAI4GEYRNMuk3aDjdpt3SNT";
static final String accessKeySecret = "Wkdf89KTbCHUp1363d6SqseOYumJlH";
public static SendSmsResponse sendSms(String telephone1, String code) throws ClientException {
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setPhoneNumbers(telephone1);
request.setSignName("ABC商城");
request.setTemplateCode("SMS_206895158");
request.setTemplateParam("{\"code\":\"" + code + "\"}");
request.setOutId("yourOutId");
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){
System.out.println("短信发送成功!");
}else {
System.out.println("短信发送失败!");
}
return sendSmsResponse;
}
private static int newcode;
public static int getNewcode() {
return newcode;
}
public static void setNewcode(String telphone){
newcode = (int)(Math.random()*9999)+100;
Jedis jedis = new Jedis("localhost", 6379);
jedis.setex(telphone,60*2, String.valueOf(newcode));
}
@RequestMapping("/checkValidation")
public String checkValidation(HttpServletRequest request){
String yzm = request.getParameter("yzm");
String tel = request.getParameter("telphone");
Jedis jedis = new Jedis("localhost", 6379);
String validation = jedis.get(tel);
System.out.println(validation);
if (validation == null) {
System.out.println("验证码未发送或者失效");
}
if (validation.equals(yzm)) {
System.out.println("验证成功");
return "success";
} else {
System.out.println("验证失败");
return "failed";
}
}
}