转载请表明出处 https://blog.csdn.net/Amor_Leo/article/details/106843874 谢谢
pom
<!--阿里云短信-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>2.1.0</version>
</dependency>
YML
# 阿里短信
aliyun:
sms:
accessKeyId: xxxx
accessKeySecret: xxxx
templateCode: SMS_xxxx
signName: xxx
业务层
@Override
public boolean sendValidateCode(String mobile) {
String code = createRandom(true, 6);
String mobileKey = GlobalsConstants.MOBILE_KEY + mobile;
long seconds = 3 * 60;
//发送验证码到手机上
boolean validateCode = sendMobleCode(code, mobile, mobileKey, seconds);
if (!validateCode) {
return false;
}
return true;
}
//发送验证码到手机
private boolean sendMobleCode(String code, String mobile, String mobileKey, long seconds) {
boolean sms = aliyunSmsUtils.sendSms(mobile, code,mobileKey,seconds);
return sms;
}
/**
* 创建指定数量的随机字符串
*
* @param numberFlag 是否是数字
* @param length 长度
* @return String
*/
private String createRandom(boolean numberFlag, int length) {
String retStr = "";
String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
int len = strTable.length();
boolean bDone = true;
do {
retStr = "";
int count = 0;
for (int i = 0; i < length; i++) {
double dblR = Math.random() * len;
int intR = (int) Math.floor(dblR);
char c = strTable.charAt(intR);
if (('0' <= c) && (c <= '9')) {
count++;
}
retStr += strTable.charAt(intR);
}
if (count >= 2) {
bDone = false;
}
} while (bDone);
return retStr;
}
工具类
/**
* @Auther: LHL
*/
@Slf4j
@Component
public class AliyunSmsUtils {
/**
* 阿里云 accessKeyId(安全信息管理下面)
*/
@Value("${aliyun.sms.accessKeyId}")
private String accessKeyId;
/**
* 阿里云 accessKeySecret(安全信息管理下面)
*/
@Value("${aliyun.sms.accessKeySecret}")
private String accessKeySecret;
/**
* 阿里云 templateCode
*/
@Value("${aliyun.sms.templateCode}")
private String templateCode;
/**
* 阿里云 signName
*/
@Value("${aliyun.sms.signName}")
private String signName;
/**
* 短信API产品名称(短信产品名固定,无需修改)
*/
private static final String product = "Dysmsapi";
/**
* 短信API产品域名,接口地址固定,无需修改
*/
private static final String domain = "dysmsapi.aliyuncs.com";
@Autowired
private RedisTemplate<String, String> redisTemplate;
public boolean sendSms(String phone, String code, String mobileKey, long seconds) {
/* 超时时间,可自主调整 */
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
/* 初始化acsClient,暂不支持region化 */
IClientProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
IAcsClient acsClient = 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("PhoneNumbers", phone);
/* 必填:短信签名-可在短信控制台中找到 */
request.putQueryParameter("SignName", signName);
/* 必填:短信模板code-可在短信控制台中找到 */
request.putQueryParameter("TemplateCode", templateCode);
/* 可选:模板中的变量替换JSON串,如模板内容为"亲爱的用户,您的验证码为${code}"时,此处的值为 */
String templateParam = "{\"code\":\"" + code + "\"}";
request.putQueryParameter("TemplateParam", templateParam);
CommonResponse response = null;
try {
response = acsClient.getCommonResponse(request);
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
if (null != response) {
Map map = JSON.parseObject(response.getData(), Map.class);
if (map.get("Code") != null && map.get("Code").equals("OK")) {
//短信发送成功,验证码存入redis,有效期180s(3分钟)
redisTemplate.opsForValue().set(mobileKey, code, seconds, TimeUnit.SECONDS);
log.info("手机号: {}, 短信发送成功!验证码:{}", phone, code);
return true;
} else {
log.info("短信发送失败!");
return false;
}
}
return false;
}
}