使用云MAS短信接口,发送手机短信验证码
1、登录云MAS业务平台
地址: http://mas.10086.cn/login.
下载文档后,根据文档说明,配置获得相关参数。
2、获得相关参数
参数写到配置文件 application.properties (实际值已省略)
# 集团客户名称
sms.ecName=
# 用户名
sms.apId=
# 密码
sms.secretKey=
# 签名编码。在云MAS平台『管理』→『接口管理』→『短信接入用户管理』获取。
sms.sign=
# 扩展码。依据申请开户的服务代码匹配类型而定,如为精确匹配,此项填写空字符串("");如为模糊匹配,此项可填写空字符串或自定义的扩展码,注:服务代码加扩展码总长度不能超过20位。
sms.addSerial=
# 模板短信接口地址
sms.tmpUrl=
# 模板ID
sms.templateId=
# 有效期时间,单位:分钟
sms.codeTime=1
3、工具类
package com.cxbdapp.msp.admin.utils;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 云MAS平台短信工具类
*
* @author zxy
* @date 2020-10-22 16:32:24
*/
@Slf4j
@Component
public class SmsSendUtil {
private static String ecName;
@Value("${sms.ecName}")
public void setEcName(String ecName) {
this.ecName = ecName;
log.info("this.ecName = " + this.ecName);
}
private static String apId;
@Value("${sms.apId}")
public void setApId(String apId) {
this.apId = apId;
log.info("this.apId = " + this.apId);
}
private static String secretKey;
@Value("${sms.secretKey}")
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
log.info("this.secretKey = " + this.secretKey);
}
private static String sign;
@Value("${sms.sign}")
public void setSign(String sign) {
this.sign = sign;
log.info("this.sign = " + this.sign);
}
private static String addSerial;
@Value("${sms.addSerial}")
public void setAddSerial(String addSerial) {
this.addSerial = addSerial;
log.info("this.addSerial = " + this.addSerial);
}
private static String tmpUrl;
@Value("${sms.tmpUrl}")
public void setTmpUrl(String tmpUrl) {
this.tmpUrl = tmpUrl;
}
private static String templateId;
@Value("${sms.templateId}")
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
public static String codeTime;
@Value("${sms.codeTime}")
public void setCodeTime(String codeTime) {
this.codeTime = codeTime;
}
/**
* 发送模板短信
*
* @param mobile 手机号
* @param code 验证码
* @param codeTime 有效期(分钟)
* @return 是否发送成功
*/
public static boolean sendTSMS(String mobile, String code, String codeTime) {
boolean success = false;
try {
String encode = getTSMSParam(mobile, code, codeTime);
// 发送请求
JSONObject jsonObject = JSON.parseObject(HttpUtil.post(tmpUrl, encode));
success = (Boolean) jsonObject.get("success");
log.info("发送模板短信响应报文:{}", jsonObject.toJSONString());
} catch (Exception e) {
log.error("网络异常,发送短信失败!", e);
}
return success;
}
/**
* 组装传参
*
* @param mobile 手机号
* @param code 验证码
* @param codeTime 有效期(分钟)
* @return Base64加密数据
*/
private static String getTSMSParam(String mobile, String code, String codeTime) {
SmsSubmitReq smsSubmitReq = new SmsSubmitReq();
String[] paramss = {code, codeTime};
smsSubmitReq.setEcName(ecName);
smsSubmitReq.setApId(apId);
smsSubmitReq.setSecretKey(secretKey);
smsSubmitReq.setTemplateId(templateId);
smsSubmitReq.setMobiles(mobile);
smsSubmitReq.setParams(JSON.toJSONString(paramss));
smsSubmitReq.setSign(sign);
smsSubmitReq.setAddSerial(addSerial);
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(smsSubmitReq.getEcName());
stringBuffer.append(smsSubmitReq.getApId());
stringBuffer.append(smsSubmitReq.getSecretKey());
stringBuffer.append(smsSubmitReq.getTemplateId());
stringBuffer.append(smsSubmitReq.getMobiles());
stringBuffer.append(smsSubmitReq.getParams());
stringBuffer.append(smsSubmitReq.getSign());
stringBuffer.append(smsSubmitReq.getAddSerial());
smsSubmitReq.setMac(MyMD5Util.md5Encrypt32Lower(stringBuffer.toString()));
String reqText = JSON.toJSONString(smsSubmitReq);
// Base64加密
String encode = Base64.encodeBase64String(reqText.getBytes());
return encode;
}
}
4、手机验证码业务实现类
package com.cxbdapp.msp.admin.service.impl;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cxbdapp.msp.admin.service.MobileService;
import com.cxbdapp.msp.admin.utils.SmsSendUtil;
import com.cxbdapp.msp.admin.utils.VoCodeEnum;
import com.cxbdapp.msp.common.core.constant.CacheConstants;
import com.cxbdapp.msp.common.core.constant.SecurityConstants;
import com.cxbdapp.msp.common.core.constant.enums.LoginTypeEnum;
import com.cxbdapp.msp.common.core.util.R;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* @author msp
* @date 2018/11/14
* <p>
* 手机登录相关业务实现
* </p>
*/
@Slf4j
@Service
@AllArgsConstructor
public class MobileServiceImpl implements MobileService {
private final RedisTemplate redisTemplate;
/**
* 发送手机验证码
*
* @param mobile 手机号码
* @return R
*/
@Override
public R sendSmsCode(String mobile) {
String code = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE));
log.info("手机号生成验证码成功:{},{}", mobile, code);
// 有效期时间,单位:分钟
String codeTime = SmsSendUtil.codeTime;
// 发送短信
boolean rt = SmsSendUtil.sendTSMS(mobile, code, codeTime);
if (rt) {
String key = CacheConstants.DEFAULT_CODE_KEY + LoginTypeEnum.SMS.getType() + StringPool.AT + mobile;
redisTemplate.opsForValue().set(key, code, SecurityConstants.CODE_TIME * Integer.valueOf(codeTime), TimeUnit.SECONDS);
return R.buildResult(null, VoCodeEnum.SUCCESS.getCode(), "手机号生成验证码成功");
}
return R.buildResult(null, VoCodeEnum.FAIL.getCode(), "手机号生成验证码失败");
}
}