maven
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>2.1.0</version>
</dependency>
配置
## 阿里云短信验证
aliyun.sms.accessKeyId=去阿里云控制台找
aliyun.sms.accessKeySecret=去阿里云控制台找
aliyun.sms.template_code=去阿里云创建
aliyun.sms.sign_name=去阿里云创建
代码
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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* 短信服务
*
* @author Zhenfeng Li
* @date 2020-02-26 13:17:04
*/
@Service
@Slf4j
public class MessageService{
@Value("${aliyun.sms.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.sms.accessKeySecret}")
private String accessKeySecret;
@Value("${aliyun.sms.sign_name}")
private String signName;
@Value("${aliyun.sms.template_code}")
private String templateCode;
/**
* 发送短信验证码
*
* @param phone 手机号
* @param code 验证码
* @return
*/
@Override
public Boolean sendMessage(String phone, String code) {
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("dysmsapi.aliyuncs.com");
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", phone);
request.putQueryParameter("SignName", signName);
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
try {
CommonResponse response = client.getCommonResponse(request);
JSONObject jsonObject = JSON.parseObject(response.getData());
if ("OK".equals(jsonObject.get("Code"))) {
log.info("手机号[{}],发送验证码[{}],成功", phone, code);
return true;
}
log.error("手机号[{}]发送注册验证码失败,Error Code:{},Error Message:", jsonObject.get("Code"), jsonObject.get("Message"));
} catch (ClientException e) {
log.error("手机号[{}]发送注册验证码失败\r\n{}", phone, e.getErrMsg());
}
return false;
}
}