短信验证码 #1

Merged
zcx merged 1 commits from dev-zcx into master 2023-08-12 18:22:23 +08:00
2 changed files with 139 additions and 0 deletions

15
pom.xml
View File

@ -130,6 +130,21 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 短信服务 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
<!-- 短信服务 -->
</dependencies>
<build>

View File

@ -0,0 +1,124 @@
package com.jwl.driver.server.util;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author zcx
*
*/
@Data
public class SmsUtil {
private static Logger logger = LoggerFactory.getLogger(SmsUtil.class);
/**
*
*/
private static String product = "Dysmsapi";
private static String url = "dysmsapi.aliyuncs.com";
private static String appkey = "LTAI4GH1CqJtaUVpmD1oYEUZ";
private static String secret = "DFG848ea8U0QaQcdiHAYznUrCWfJzn";
private static String smsFreeSignName = "对嘛科技";
//用于存储短信验证码
private static Map<String, String> codeMap = new HashMap<>();
/**
*
*/
public static String getMessageCode(String phone){
String code = (int)(Math.random()*999999)+ "";
if (code.length() == 5){
code = "0" + code;
}
codeMap.put(phone, code);
return code;
}
/**
* 6
*/
public static String getCodeByPhone(String phone){
return codeMap.get(phone);
}
/**
*
*
* @param sendPhone
*
* @param templet
* 使
* @param jsonText
* json
*/
public static String currencySendMessage(String sendPhone, String templet,
String jsonText) {
IAcsClient aliClient = createClient();
//组装请求对象
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号。支持以逗号分隔的形式进行批量调用批量上限为20个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
request.setPhoneNumbers(sendPhone);
//必填:短信签名-可在短信控制台中找到
request.setSignName(smsFreeSignName);
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(templet);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam(jsonText);
SendSmsResponse sendSmsResponse = null;
logger.info("发送短信参数内容:"+jsonText+"\n手机号码是"+sendPhone+"\n模板编号"+templet);
try {
sendSmsResponse = aliClient.getAcsResponse(request);
} catch (ClientException e) {
logger.error("发送短信失败:"+e.getErrMsg()+"\n"+e.getMessage());
e.printStackTrace();
}
if (sendSmsResponse!=null){
logger.info("message:"+sendSmsResponse.getMessage()+"\n"+
"code:"+sendSmsResponse.getCode()+"\n");
}else{
logger.info("发送短信响应为null");
}
return JSONObject.toJSONString(sendSmsResponse);
}
/**
*
* @return
*/
private static IAcsClient createClient() {
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", appkey,
secret);
try {
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, url);
} catch (ClientException e) {
e.printStackTrace();
}
IAcsClient acsClient = new DefaultAcsClient(profile);
return acsClient;
}
public static void main(String[] args) throws IOException {
Map<String, Object> param = new HashMap<>();
param.put("code", 666666);
SmsUtil.currencySendMessage("18855146869", "SMS_198880447", JSONObject.toJSONString(param) );
}
}