短信验证码 #1
15
pom.xml
15
pom.xml
|
@ -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>
|
||||
|
|
|
@ -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) );
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue