文章目录
前言
现在阿里云短信功能和腾讯云短信功能均需要验证已经备案的网站或者已经上线的App才能使用,对于很多开发的新手来说,想要为自己的项目添加短信功能就成了难题。下面分享一下网易云信的短信功能
提示:以下是本篇文章正文内容,下面案例可供参考
一、如何申请网易云信短信功能?
需要实名认证,现在手持身份证拍照就能够通过。均在一天内可以申请完成。
需要先添加签名再添加短信模板
短信可以自己添加模板,在代码中修改短信id即可
二、官网提供实例
参数说明:
返回数据示例:
"Content-Type": "application/json; charset=utf-8"
{
"code": 200,
"msg": "88",
"obj": "1908"
}
package com.netease.code;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.netease.checksum.CheckSumBuilder;
/**
* 发送验证码
* @author liuxuanlin
*
*/
public class SendCode {
//发送验证码的请求路径URL
private static final String
SERVER_URL="https://api.netease.im/sms/sendcode.action";
//网易云信分配的账号,请替换你在管理后台应用下申请的Appkey
private static final String
APP_KEY="fd460d34e786e7754e505bc4fab0f027";
//网易云信分配的密钥,请替换你在管理后台应用下申请的appSecret
private static final String APP_SECRET="xxxxxxxx";
//随机数
private static final String NONCE="123456";
//短信模板ID
private static final String TEMPLATEID="3057527";
//手机号
private static final String MOBILE="13888888888";
//验证码长度,范围4~10,默认为4
private static final String CODELEN="6";
public static void main(String[] args) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(SERVER_URL);
String curTime = String.valueOf((new Date()).getTime() / 1000L);
/*
* 参考计算CheckSum的java代码,在上述文档的参数列表中,有CheckSum的计算文档示例
*/
String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime);
// 设置请求的header
httpPost.addHeader("AppKey", APP_KEY);
httpPost.addHeader("Nonce", NONCE);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("CheckSum", checkSum);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 设置请求的的参数,requestBody参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
/*
* 1.如果是模板短信,请注意参数mobile是有s的,详细参数配置请参考“发送模板短信文档”
* 2.参数格式是jsonArray的格式,例如 "['13888888888','13666666666']"
* 3.params是根据你模板里面有几个参数,那里面的参数也是jsonArray格式
*/
nvps.add(new BasicNameValuePair("templateid", TEMPLATEID));
nvps.add(new BasicNameValuePair("mobile", MOBILE));
nvps.add(new BasicNameValuePair("codeLen", CODELEN));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
// 执行请求
HttpResponse response = httpClient.execute(httpPost);
/*
* 1.打印执行结果,打印结果一般会200、315、403、404、413、414、500
* 2.具体的code有问题的可以参考官网的Code状态表
*/
System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
}
}
三、StringBoot开发实例
1.额外添加的POM依赖
代码如下(示例):
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
<!-- classifier可以是任意的字符串,用于拼接在GAV之后来确定指定的文件。
可用于区分不同jdk版本所生成的jar包,一般不用修改,不然可能会引入失败-->
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
<!--lombok用来简化实体类:需要安装lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
2.entity实体类
Message 类 用于封装返回的短信json数据
返回数据示例:
"Content-Type": "application/json; charset=utf-8"
{
"code": 200,
"msg": "88",
"obj": "1908"
}
@Data
@ToString
public class Message {
Integer code;
String msg;
String obj;
}
3.utils工具类
- MessagePropertiesUtils 用于从配置文件读取所需数据,解耦。方便修改
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MessagePropertiesUtils implements InitializingBean {
@ApiModelProperty("发送验证码的请求路径URL")
@Value("${yunxin.messmage.serverUrl}")
private String serverUrl;
@ApiModelProperty("网易云信分配的账号,请替换你在管理后台应用下申请的Appkey")
@Value("${yunxin.messmage.appKey}")
private String appKey;
@ApiModelProperty("网易云信分配的密钥,请替换你在管理后台应用下申请的appSecret")
@Value("${yunxin.messmage.appSecret}")
private String appSecret;
@ApiModelProperty("短信模板ID")
@Value("${yunxin.messmage.templateid}")
private String templateid;
@ApiModelProperty("验证码长度,范围4~10,默认为4")
@Value("${yunxin.messmage.codelen}")
private String codelen;
@ApiModelProperty("随机数")
@Value("${yunxin.messmage.nonce}")
private String nonce;
public static String SERVER_URL;
public static String APP_KEY;
public static String APP_SECRET;
public static String TEMPLATEID;
public static String CODELEN;
public static String NONCE;
@Override
public void afterPropertiesSet() throws Exception {
SERVER_URL = serverUrl;
APP_KEY = appKey;
APP_SECRET = appSecret;
TEMPLATEID = templateid;
CODELEN = codelen;
NONCE = nonce;
}
}
- CheckSumBuilder 用于进行SHA1哈希计算,转化成16进制字符(String,小写) SHA1(AppSecret + Nonce + CurTime),三个参数拼接的字符串
import java.security.MessageDigest;
public class CheckSumBuilder {
// 计算并获取CheckSum
public static String getCheckSum(String appSecret, String nonce, String curTime) {
return encode("sha1", appSecret + nonce + curTime);
}
// 计算并获取md5值
public static String getMD5(String requestBody) {
return encode("md5", requestBody);
}
private static String encode(String algorithm, String value) {
if (value == null) {
return null;
}
try {
MessageDigest messageDigest
= MessageDigest.getInstance(algorithm);
messageDigest.update(value.getBytes());
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String getFormattedText(byte[] bytes) {
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
for (int j = 0; j < len; j++) {
buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
}
return buf.toString();
}
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}
4.配置文件
- application.properties
#redis
spring.redis.host=xxx.xxx.xxx.xxx
spring.redis.port=6379
spring.redis.database=0
spring.redis.timeout=1800000
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
#短信功能使用
//发送验证码的请求路径URL
yunxin.messmage.serverUrl=https://api.netease.im/sms/sendcode.action
//随机数
yunxin.messmage.nonce=123456
//以下参数请务必自行更改
//网易云信分配的账号,请替换你在管理后台应用下申请的Appkey
yunxin.messmage.appKey=fd460d34e786e7754e505bc4fab0f027
//网易云信分配的密钥,请替换你在管理后台应用下申请的appSecret
yunxin.messmage.appSecret=xxxxxxxx
//短信模板ID
yunxin.messmage.templateid=3057527
//验证码长度,范围4~10,默认为4
yunxin.messmage.codelen=4
5.MsmService
public interface MsmService {
String send(String phone);
}
6.MsmServiceImpl
import com.gyh.msmservice.service.MsmService;
import com.gyh.msmservice.utils.CheckSumBuilder;
import com.gyh.msmservice.utils.MessagePropertiesUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class MsmServiceImpl implements MsmService {
@Override
public String send(String phone) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(MessagePropertiesUtils.SERVER_URL);
String curTime = String.valueOf((new Date()).getTime() / 1000L);
/*
* 参考计算CheckSum的java代码,在上述文档的参数列表中,有CheckSum的计算文档示例
*/
String checkSum = CheckSumBuilder.getCheckSum(MessagePropertiesUtils.APP_SECRET, MessagePropertiesUtils.NONCE, curTime);
// 设置请求的header
httpPost.addHeader("AppKey", MessagePropertiesUtils.APP_KEY);
httpPost.addHeader("Nonce", MessagePropertiesUtils.NONCE);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("CheckSum", checkSum);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 设置请求的的参数,requestBody参数
List<NameValuePair> nvps = new ArrayList<>();
/*
* 1.如果是模板短信,请注意参数mobile是有s的,详细参数配置请参考“发送模板短信文档”
* 2.参数格式是jsonArray的格式,例如 "['13888888888','13666666666']"
* 3.params是根据你模板里面有几个参数,那里面的参数也是jsonArray格式
*/
nvps.add(new BasicNameValuePair("templateid", MessagePropertiesUtils.TEMPLATEID));
nvps.add(new BasicNameValuePair("mobile", phone));
nvps.add(new BasicNameValuePair("codeLen", MessagePropertiesUtils.CODELEN));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 执行请求
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
/*
* 1.打印执行结果,打印结果一般会200、315、403、404、413、414、500
* 2.具体的code有问题的可以参考官网的Code状态表
*/
String entity = null;
try {
if (response != null) {
response.getEntity();
entity = EntityUtils.toString(response.getEntity(), "utf-8");
}
} catch (IOException e) {
e.printStackTrace();
}
return entity;
}
}
7.controller
这里使用redis实现短信验证码的时效
import com.gyh.msmservice.entity.Message;
import com.gyh.msmservice.service.MsmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.TimeUnit;
/**
* @author bean
* @create 2021-08-10 18:29
*/
@RestController
@RequestMapping("/api/msm")
//@CrossOrigin //跨域
@Api(tags = {"发送短信控制器"})
public class MsmApiController {
@Autowired
private MsmService msmService;
@Autowired
private RedisTemplate<String, String> redisTemplate;
@ApiOperation(value = "发送短信")
@GetMapping(value = "/send/{phone}")
public String code(
@ApiParam(name = "phone", value = "手机号码", required = true)
@PathVariable String phone) {
// 以通过phone手机号为key从Redis获取验证码:有空和不为空两种情况
String code = redisTemplate.opsForValue().get(phone);
// 根据code在Redis中是否有值判断是否已经发送过短信
if (!StringUtils.isEmpty(code)) {
// 已经发送过短信
return "发送短信成功";
} else {
// 未发送短信
// 调用send执行网易短信发送
String send = msmService.send(phone);
// 将String转换为JSONObject
JSONObject jsonobject = JSONObject.fromObject(send);
// 将json转换为message对象
Message message = (Message) JSONObject.toBean(jsonobject, Message.class);
// 200状态码代表成功
if (message.getCode() == 200) {
// message.getObj()为验证码的值
// 将验证码放入Redis, key=phone:value=message.getObj()
// 通过Redis设置过期时间为10分钟,来实现验证码10分钟有效
redisTemplate.opsForValue().set(phone, message.getObj(), 10, TimeUnit.MINUTES);
return "发送短信成功";
} else {
return "发送短信失败";
}
}
}
}
补充:此处本人使用swagger进行接口测试,故添加ApiXXX注释,@CrossOrigin 为跨域注解,可以去掉
最终测试
此处我使用的是自定义的返回类,按照如上代码,应该返回"发送短信成功"