服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|

服务器之家 - 编程语言 - JAVA教程 - Springboot实现阿里云通信短信服务有关短信验证码的发送功能

Springboot实现阿里云通信短信服务有关短信验证码的发送功能

2020-12-17 14:29喝酒不骑马 JAVA教程

短信验证码是通过发送验证码到手机的一种有效的验证码系统。主要用于验证用户手机的合法性及敏感操作的身份验证。下面通过本文大家分享Springboot实现阿里云通信短信服务有关短信验证码的发送功能,一起看看吧

前言

短信验证码是通过发送验证码到手机的一种有效的验证码系统。主要用于验证用户手机的合法性及敏感操作的身份验证。

现在市面上的短信服务平台有很多。大家在选择的时候未免会有些不好抉择。本人建议选择短信服务商应遵循以下几点:

  1. 服务商知名度高,业务流量大。(这样的平台可信度高)
  2. 服务稳定,不能经常宕机。(保证自身业务的流畅运行)
  3. 文档全面详细。(没文档怎么玩?)

最近的一个项目中,注册和修改密码时需要用到短信验证码校验手机号的功能。本人也是对比几家后,直接选择阿里云通信的短信服务。(本身项目服务器也是部署在阿里云上,但之前并不知道阿里云有短信服务,早知道阿里有的话就不会浪费时间找其他平台了)。废话不多说,下面直接开始短信验证服务教程。

准备

1.登录阿里云,开通阿里云通信短信服务。

2.申请accesskey

选择右上角accesskeys,创建一个access key

Springboot实现阿里云通信短信服务有关短信验证码的发送功能

3.进入控制台,申请短信签名。这个作用就是用于短信前面“【】”里面的名称。输入时不需要带“【】”

Springboot实现阿里云通信短信服务有关短信验证码的发送功能 

注意申请规范,要不然审核不会过。符合规范的话一般一个小时左右就能通过了。

我这里的签名是“喝酒不骑马”

Springboot实现阿里云通信短信服务有关短信验证码的发送功能 

4.申请短信模版

模板类型选择验证码。注意,选择验证码后,模板替代变量只支持验证码作为变量,且变量替换值<=6位数字或字母。

我的模板如下图

Springboot实现阿里云通信短信服务有关短信验证码的发送功能 

1.下载sdk包

 

下载后,我们需要使用

aliyun-java-sdk-core-3.2.2.jar

aliyun-java-sdk-dysmsapi-1.0.0-sanpshot.jar

aliyun-sdk-mns-1.1.8.jar

这三个包

springboot + maven下的jar包配置

1.在工程的resources目录下新建一个lib文件夹,将jar包复制进去。

Springboot实现阿里云通信短信服务有关短信验证码的发送功能

2.由于maven库中没有上述几个jar包的依赖,所以需要将本地lib目录下的jar包导入到maven中。并在pom.xml文件中填下如下依赖。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<dependency>
 <groupid>aliyun-message-sdk-core</groupid>
 <artifactid>aliyun-message-sdk-core</artifactid>
 <version>3.2.3</version>
 <scope>system</scope>
 <systempath>${project.basedir}/src/main/resources/lib/aliyun-java-sdk-core-3.2.2.jar</systempath>
</dependency>
<dependency>
 <groupid>aliyun-message-sdk-mns</groupid>
 <artifactid>aliyun-message-sdk-mns</artifactid>
 <version>1.1.8</version>
 <scope>system</scope>
 <systempath>${project.basedir}/src/main/resources/lib/aliyun-sdk-mns-1.1.8.jar</systempath>
</dependency>
<dependency>
 <groupid>aliyun-java-sdk-dysmsapi</groupid>
 <artifactid>aliyun-java-sdk-dysmsapi</artifactid>
 <version>1.0.0</version>
 <scope>system</scope>
 <systempath>${project.basedir}/src/main/resources/lib/aliyun-java-sdk-dysmsapi-1.0.0-sanpshot.jar</systempath>
</dependency>

注意, <groupid>、<artifactid>、<version>随意填写。<scope>system</scope>必须要加,这表示导入本地的jar包。

${project.basedir}为项目的根路径。

建立aliyunmessageutil工具类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class aliyunmessageutil {
 private static final string product = "dysmsapi";
 //产品域名,开发者无需替换
 private static final string domain = "dysmsapi.aliyuncs.com";
 // 此处需要替换成开发者自己的ak(在阿里云访问控制台寻找)
 private static final string accesskeyid = "这里替换你的accesskeyid";
 private static final string accesskeysecret = "这里替换你的accesskeysecret";
 public static sendsmsresponse sendsms(map<string, string> parammap) throws com.aliyuncs.exceptions.clientexception {
  //可自助调整超时时间
  system.setproperty("sun.net.client.defaultconnecttimeout", "10000");
  system.setproperty("sun.net.client.defaultreadtimeout", "10000");
  //初始化acsclient,暂不支持region化
  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(parammap.get("phonenumber"));
  //必填:短信签名-可在短信控制台中找到
  request.setsignname(parammap.get("msgsign"));
  //必填:短信模板-可在短信控制台中找到
  request.settemplatecode(parammap.get("templatecode"));
  //可选:模板中的变量替换json串,如模板内容为"亲爱的${name},您的验证码为$[code]"时,此处的值为
  request.settemplateparam(parammap.get("jsoncontent"));
  //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
//  request.setsmsupextendcode(parammap.get("extendcode"));
  //可选:outid为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
//  request.setoutid(parammap.get("outid"));
  //hint 此处可能会抛出异常,注意catch
  sendsmsresponse sendsmsresponse = acsclient.getacsresponse(request);
  return sendsmsresponse;
 }
}

sendsms(map<string, string> parammap)方法是我封装的方法。传入的参数是parammap,其中包含以下属性:

phonenumber:接受者手机号

msgsign:短信签名名称。在控制台的短信签名里能找到。

templatecode:短信模版的code。见控制台中的模版code。

jsoncontent:需要替换的变量的json字符串。对于验证码来说,string jsoncontent = "{\"number\":\"" + randomnum + "\"}";即可。其中randomnum是随机生成的6位验证码。

extendcode:上行短信模板的验证码,不需要的话可以忽略

outid:扩展字段,不需要则可以忽略

调用demo

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public string sendmsg() throws lgdserviceexception, clientexception {
 string phonenumber = "18888888888";
 string randomnum = createrandomnum(6);
 string jsoncontent = "{\"number\":\"" + randomnum + "\"}";
 map<string, string> parammap = new hashmap<>();
 parammap.put("phonenumber", phonenumber);
 parammap.put("msgsign", "喝酒不骑马");
 parammap.put("templatecode", "xxxxxxxx");
 parammap.put("jsoncontent", jsoncontent);
 sendsmsresponse sendsmsresponse = aliyunmessageutil.sendsms(parammap);
 if(!(sendsmsresponse.getcode() != null && sendsmsresponse.getcode().equals("ok"))) {
  if(sendsmsresponse.getcode() == null) {
   //这里可以抛出自定义异常
  }
  if(!sendsmsresponse.getcode().equals("ok")) {
    //这里可以抛出自定义异常
  }
 }
}
/**
 * 生成随机数
 * @param num 位数
 * @return
 */
public static string createrandomnum(int num){
 string randomnumstr = "";
 for(int i = 0; i < num;i ++){
  int randomnum = (int)(math.random() * 10);
  randomnumstr += randomnum;
 }
 return randomnumstr;
}

总结

短信效果如图所示

Springboot实现阿里云通信短信服务有关短信验证码的发送功能 

对于验证码的校验,可参见我的另一篇博文springboot实现短信验证码校验.

以上所述是小编给大家介绍的springboot实现阿里云通信短信服务有关短信验证码的发送功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://blog.csdn.net/Colton_Null/article/details/77283193

延伸 · 阅读

精彩推荐