阿里云短信服务文档使用指引:
https://help.aliyun.com/document_detail/59210.html
B站视频教程链接:
https://www.bilibili.com/video/BV16i4y1E7XP
阿里云短信服务文档使用指引:
对接阿里云短信流程如下图:
1、购买短信服务
2、创建AccessKey获取它的ID和Secret
3、创建签名和模板
我们可以进入该页面点击黄色字体的链接,进入他的详细页面去创建和设置我们需要的参数。
设置模板签名页面
代码模块:
maven
<!--阿里短信-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
controller层代码
/**
* 发送短信验证码
* @param phone
* @return
*/
@RequestMapping("sendMessage")
@ResponseBody
public JsonResult sendMessage(String phone){
JsonResult result = new JsonResult();
System.out.println("发送短信手机号码= "+phone);
Jedis jedis = redisUtil.getJedis();
try {
Boolean boo = isMobile(phone);
if (boo==false){
result.setMessage("电话号码格式不对");
return result;
}
//生成一位四位数字的验证码
String randomCode = WebUtil.randomCode(4);
System.err.println(randomCode);
String phoneNum = phone;
//调用发送短信的接口,传入手机号和验证码
WebUtil.sendShortMessage(randomCode, phoneNum);
//把手机号码和验证码存入redis中,设置过期时间五分钟
String res = jedis.setex("phone" + phoneNum, 5 * 60, randomCode);
System.err.println(res);
result.setState(SUCCESS);
}catch (Exception e){
e.printStackTrace();
}finally {
jedis.close();
}
return result;
}
/**
* 手机号验证
* @param str
* @return 验证通过返回true
*/
public static boolean isMobile(final String str) {
Pattern p = null;
Matcher m = null;
boolean b = false;
p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号
m = p.matcher(str);
b = m.matches();
return b;
}
工具类:
/**
* 生成随机验证码
*
// * @param length 验证码长度
* @return 生成的验证码
* @throws RuntimeException 验证码长度必须大于0
* @author
*/
public static String randomCode(int length) {
if (length <= 0) {
throw new RuntimeException("验证码长度不合法");
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < length; i++) {
// 1.生成随机数
double doubleRandom = Math.random();
// 2.调整
int integerRandom = (int) (doubleRandom * 10);
// 3.拼接
builder.append(integerRandom);
}
return builder.toString();
}
/**
* 发送验证码短信
*
// * @param appcode 阿里云市场中调用API时识别身份的appCode
* @param randomCode 验证码值
* @param phoneNum 接收验证码短信的手机号
*/
// private static final String accessKeyId = "LTAI4GCFZygjHiUxLrKZGQZ8";
// private static final String secret = "H4qOYT6PLOQqHA5ViKoiKjsOWP8en0";
// private static final String TemplateCode = "SMS_203190588";
// private static final String SignName = "微光科技";
public static void sendShortMessage(String randomCode, String phoneNum) {
//我们需要的四个参数 AccessKeyId、secret、短信签名和模板ID
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, secret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("dysmsapi.aliyuncs.com");
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", phoneNum);
request.putQueryParameter("SignName", SignName); //短信签名
request.putQueryParameter("TemplateCode", TemplateCode); //短信签名模板id
// request.putQueryParameter("TemplateParam", "{code:"+0000+"}");
request.putQueryParameter("TemplateParam", "{code:"+ randomCode +"}");
try {
CommonResponse response = client.getCommonResponse(request);
System.err.println("得到的数据= "+response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
发送成功返回的数据:
关注微信公众号:程序猿木大大 可以获取更多资料详情(面试试题,开源项目等)