改一下这里面的就OKjava调用阿里云实现短信验证码
controller层
package com.jwt.demo.controller;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;
import static com.jwt.demo.util.DXutil.*;
@Controller
public class Verification {
public static int Aa;
public static String Bb;
@RequestMapping("/dxyanz")
public String verification(){
return "yanz";
}
@PostMapping("/dxyanz1")
@ResponseBody
// public static void main(String[] args) throws Exception {
public void DXyz(HttpServletRequest request) throws Exception {
String phone = request.getParameter("phone");
setNewcode();
//给全局变量赋值
// Aa=getNewcode();
// Bb=phone;
// System.out.println(Aa);
// System.out.println(Bb);
String code = Integer.toString(getNewcode());//15762971759
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
// 如果 Redis 服务设置来密码,需要下面这行,没有就不需要
// jedis.auth("123456");
System.out.println("连接成功");
jedis.set("phone", phone);
jedis.set("code", code);
System.out.println("redis 存储的字符串为: "+ jedis.get("phone"));
System.out.println("redis 存储的字符串为: "+ jedis.get("code"));
//查看服务是否运行
System.out.println("服务正在运行: "+jedis.ping());
SendSmsResponse sendSms =sendSms(phone,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());
// return getNewcode();
}
@PostMapping("/dxyanz2")
@ResponseBody
public String DXyz2(HttpServletRequest request) throws Exception{
String phone = request.getParameter("phone");
String yzm = request.getParameter("yzm");
if (phone==""){
System.out.println("手机号不能为空");
return "手机号不能为空";
}
if (yzm==""){
System.out.println("验证码不能为空");
return "验证码不能为空";
}
// System.out.println(phone);
// System.out.println(Bb);
// System.out.println(yzm);
// System.out.println(Aa);
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
System.out.println("连接成功");
System.out.println("redis 存储的字符串为: "+ jedis.get("phone"));
System.out.println("redis 存储的字符串为: "+ jedis.get("code"));
String phone1 = jedis.get("phone");
String code1 = jedis.get("code");
//查看服务是否运行
System.out.println("服务正在运行: "+jedis.ping());
if(phone.equals(phone1) && yzm.equals(code1)){
return "验证码对了,成功";
}else {
return "验证码不对,失败";
}
//equals用的都是String类型的,所以需要先转换类型
// String AA = ""+Aa;
// if(phone.equals(Bb) && yzm.equals(AA)){
// return "验证码对了,成功";
// }else {
// return "验证码不对,失败";
// }
}
// @RequestMapping("/yanz1")
// @ResponseBody
// public String testRandom(){
// String msg="您的注册码为%s,谢谢注册!";
// System.out.println("Math.random得到小数");
// System.out.println(Math.round((Math.random()+1) * 1000));
// System.out.println("Random");
// System.out.println(new Random().nextInt(9999));
// System.out.println("字符串前面补0的话就这样String.format");
// System.out.println(String.format("%04d",new Random().nextInt(9999)));
// return msg;
// }
}