1.引入jar
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.0</version>
</dependency>
2.
package org.a.base.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.a.base.entity.BaseProperties;
import org.a.base.entity.vo.Message;
import org.a.base.service.CacheBaseService;
import org.a.learning.entity.vo.ApiMessage;
import java.util.Random;
import redis.RedisUtil;
/**
* 短信发送
*/
@Component
public class SMSUtil {
@Autowired
private RedisUtil redisUtil;
@Autowired
private CacheBaseService cacheBaseService;
private static final String SMS_CACHE_CODE="SMS.CODE.";
public ApiMessage sendSMS(String mobile) {
ApiMessage message = new ApiMessage();
String accessKey = cacheBaseService.getPropertiesOneRedis("SMS.verification.accessKey").getPropertyValue();//短信服务appKey
String secret = cacheBaseService.getPropertiesOneRedis("SMS.verification.secret").getPropertyValue();//短信服务appkey对应的secret
String smsFreeSignName = cacheBaseService.getPropertiesOneRedis("SMS.verification.smsFreeSignName").getPropertyValue();//短信服务签名
String smsTemplateCode = cacheBaseService.getPropertiesOneRedis("SMS.verification.smsTemplateCode").getPropertyValue();短信服务模板的 TemplateCode
// 短信的验证码
String code = "" + ((new Random()).nextInt(9999));
while (code.length() < 4) {
code = "0" + code;
}
//发送短信
DefaultProfile profile = DefaultProfile.getProfile("default", accessKey, secret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
//request.setProtocol(ProtocolType.HTTPS);
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("PhoneNumbers", mobile); //接收短信的手机号码
request.putQueryParameter("SignName", smsFreeSignName); // 短信签名
request.putQueryParameter("TemplateCode",smsTemplateCode ); // 短信模板ID
JSONObject templateParam = new JSONObject();
templateParam.put("code",code);
request.putQueryParameter("TemplateParam", templateParam.toJSONString()); // 短信模板变量对应的实际值,JSON格式
try {
CommonResponse response = client.getCommonResponse(request);
JSONObject res = (JSONObject) JSON.parse(response.getData());
if("OK".equals(res.get("Code"))){
// 发送成功,存储code到redis里
this.setCode(mobile,code);
message.setStatus(1);
}
message.setData(res);
} catch (Exception e) {
e.printStackTrace();
message.setMessage("网络异常");
message.setStatus(0);
}
return message;
}
// 存储code
private void setCode(String mobile,String code){
redisUtil.set(SMS_CACHE_CODE + mobile, code);
redisUtil.expire(SMS_CACHE_CODE + mobile, 300);
// redisUtil.cacheSet(SMS_CACHE_CODE + mobile,code,300);
}
// 获取短信验证码code
public String getSMSCode(String mobile) {
ApiMessage message = new ApiMessage();
String code = (String) redisUtil.get(SMS_CACHE_CODE + mobile);
// 清除这个验证码的缓存
this.removeSMSCode(mobile);
return code;
}
// 清除这个验证码的缓存
public void removeSMSCode(String mobile) {
ApiMessage message = new ApiMessage();
redisUtil.removeHashValue(SMS_CACHE_CODE + mobile);
}
}
3.配置相应的参数