Maven依赖:
-
<!-- 阿里云短信 -->
-
<dependency>
-
<groupId>com.aliyun
</groupId>
-
<artifactId>aliyun-java-sdk-core
</artifactId>
-
<version>4.1.0
</version>
-
</dependency>
Java代码:
-
package tech.yooo.ratel.common.util;
-
-
import com.alibaba.fastjson.JSONObject;
-
import com.aliyuncs.CommonRequest;
-
import com.aliyuncs.CommonResponse;
-
import com.aliyuncs.DefaultAcsClient;
-
import com.aliyuncs.IAcsClient;
-
import com.aliyuncs.http.MethodType;
-
import com.aliyuncs.profile.DefaultProfile;
-
import lombok.extern.slf4j.Slf4j;
-
-
/**
-
* 阿里云短信工具类<br>
-
* 【1】价格说明 <br>
-
* 验证码类:0.045元/条,如:登录验证、支付确认、登录异常等<br>
-
* 短信通知:0.045元/条,如:物流通知、付款回执、状态通知等<br>
-
* 推广短信:0.055元/条,如:营销推广类短信,如会员关怀、新品上线、活动通知等<br>
-
* 国际短信:0.1元/条 - 1.2 元/条不等(有需要再添加,当前不支持该类短信)<br>
-
*
-
* @author ZhangYuanqiang
-
* @since 2019/12/12
-
*/
-
@Slf4j
-
public
class SmsUntil {
-
-
private
static
final String ACCESS_KEY_ID =
"xxxxxxxxx";
-
private
static
final String ACCESS_SECRET =
"yyyyyyyyy";
-
private
static
final String SIGN_NAME =
"哇哈哈";
-
private
static IAcsClient client;
-
-
private static IAcsClient getClient() {
-
if (client !=
null) {
-
return client;
-
}
else {
-
DefaultProfile profile =
-
DefaultProfile.getProfile(
"cn-hangzhou", ACCESS_KEY_ID, ACCESS_SECRET);
-
client =
new DefaultAcsClient(profile);
-
}
-
return client;
-
}
-
-
/**
-
* 发送短信 <br>
-
* 参考文档:https://api.aliyun.com/?spm=a2c4g.11186623.2.15.450860e2NsGyiW#/?product=Dysmsapi&api=SendSms¶ms={}&tab=DEMO&lang=JAVA
-
* <br>
-
*
-
* @param tel 接收手机号 <br>
-
* @param code 发送内容:只发送阿里云模板配置的变量 <br>
-
* @param template 短信类型 {@link SmsTemplate}}<br>
-
* @return true : 发送成功,false : 发送失败
-
*/
-
public static boolean sendVerifyCode(String tel, String code, String template) {
-
log.info(
"sendVerifyCode: 发送验证码 [tel:{}, code:{}, template:{}]", tel, code, template);
-
CommonRequest request = getCommonRequest(
"SendSms");
-
request.putQueryParameter(
"TemplateCode", template);
-
request.putQueryParameter(
"PhoneNumbers", tel);
-
request.putQueryParameter(
"TemplateParam", String.format(
"{\"code\":\"%s\"}", code));
-
try {
-
CommonResponse response = getClient().getCommonResponse(request);
-
String resp = response.getData();
-
JSONObject obj = JSONObject.parseObject(resp);
-
String message = obj.getString(
"Message");
-
if (message.equals(
"OK")) {
-
log.info(
"sendVerifyCode: 发送成功!");
-
return
true;
-
}
else {
-
log.info(
"sendVerifyCode: 发送失败![{}]", resp);
-
return
false;
-
}
-
}
catch (Exception e) {
-
log.info(
"sendVerifyCode: 发送验证码异常");
-
e.printStackTrace();
-
return
false;
-
}
-
}
-
-
private static CommonRequest getCommonRequest(String action) {
-
CommonRequest request =
new CommonRequest();
-
request.setMethod(MethodType.POST);
-
request.setDomain(
"dysmsapi.aliyuncs.com");
-
request.setVersion(
"2017-05-25");
-
request.setAction(action);
-
request.putQueryParameter(
"RegionId",
"cn-hangzhou");
-
request.putQueryParameter(
"SignName", SIGN_NAME);
-
return request;
-
}
-
-
public
static
class SmsTemplate {
-
/** 身份验证验证码 */
-
public
static String AUTHENTICATION =
"SMS_178535388";
-
/** 登录确认验证码 */
-
public
static String LOGIN_CONFIRM =
"SMS_178535387";
-
/** 登录异常验证码 */
-
public
static String LOGIN_UNEXPECTED =
"SMS_178535386";
-
/** 用户注册验证码 */
-
public
static String REGISTER =
"SMS_178535385";
-
/** 修改密码验证码 */
-
public
static String UPDATE_PASSWORD =
"SMS_178535384";
-
/** 信息变更验证码 */
-
public
static String INFO_CHANGE =
"SMS_178535383";
-
}
-
}
转载:https://blog.csdn.net/sunnyzyq/article/details/103517144
查看评论