pigxCloud微服务项目02——服务端——使用云MAS短信接口,发送手机短信验证码

本文档介绍了如何使用云MAS业务平台发送手机短信验证码。首先登录平台获取相关参数,然后在配置文件中设置这些参数。接着,通过工具类实现发送模板短信的方法,最后在验证码业务实现类中调用工具类发送验证码并存储到缓存中。整个流程涉及了HTTP请求、JSON解析和Base64编码等技术。
摘要由CSDN通过智能技术生成

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(), "手机号生成验证码失败");
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小言W

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值