七牛云短信

This commit is contained in:
YYM 2024-01-31 10:02:12 +08:00
parent 9e32e3403a
commit 83d86bfe48
5 changed files with 136 additions and 66 deletions

View File

@ -16,10 +16,12 @@ public class QiNiuConfig extends BaseConfig {
private String baseUrl = "https://sms.qiniuapi.com";
private String templateName;
private String singleMsgUrl = "/v1/message/single";
private String massMsgUrl = "/v1/message";
private String signatureId;

View File

@ -1,7 +1,9 @@
package org.dromara.sms4j.qiniu.service;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.api.entity.SmsResponse;
@ -39,64 +41,80 @@ public class QiNiuSmsImpl extends AbstractSmsBlend<QiNiuConfig> {
@Override
public SmsResponse sendMessage(String phone, String message) {
//获取url
String url = getConfig().getBaseUrl() + getConfig().getSingleMsgUrl();
Map<String, String> msg = new LinkedHashMap<>();
msg.put(getConfig().getTemplateName(), message);
//签名id
String signatureId = getConfig().getSignatureId();
//模型id
String templateId = getConfig().getTemplateId();
//手机号
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("mobile", phone);
hashMap.put("template_id", templateId);
hashMap.put("signature_id", signatureId);
hashMap.put("parameters", JSONUtil.toJsonStr(msg));
String jsonBody = JSONUtil.toJsonStr(hashMap);
String signature = null;
try {
signature = QiNiuUtils.getSignature("POST",url, getConfig(), jsonBody);
} catch (Exception e) {
log.error("签名失败", e);
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String signDate = dateFormat.format(new Date());
//请求头
Map<String, String> header = new HashMap<>();
header.put("Authorization", signature);
header.put("X-Qiniu-Date", signDate);
header.put("Content-Type", "application/json");
//请求体
JSONObject jsonObject = http.postJson(url, header, jsonBody);
System.out.println(jsonObject.toString());
System.out.println(jsonObject.toString());
System.out.println(jsonObject.toString());
System.out.println(jsonObject.toString());
System.out.println(jsonObject.toString());
return null;
return sendSingleMsg(phone, getConfig().getTemplateId(), new LinkedHashMap<String, String>() {{
put(getConfig().getTemplateName(), message);
}});
}
@Override
public SmsResponse sendMessage(String phone, LinkedHashMap<String, String> messages) {
return null;
return sendSingleMsg(phone, getConfig().getTemplateId(), messages);
}
@Override
public SmsResponse sendMessage(String phone, String templateId, LinkedHashMap<String, String> messages) {
return null;
return sendSingleMsg(phone, templateId, messages);
}
@Override
public SmsResponse massTexting(List<String> phones, String message) {
return null;
LinkedHashMap<String, String> params = new LinkedHashMap<>();
params.put(getConfig().getTemplateName(), message);
return senMassMsg(phones, getConfig().getTemplateId(), params);
}
@Override
public SmsResponse massTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
return null;
return senMassMsg(phones, templateId, messages);
}
/**
* @return SmsResponse
* @author 初拥
* @date 2024/1/31 8:54
* @Description: 统一处理返回结果
*/
public SmsResponse handleRes(JSONObject jsonObject) {
SmsResponse smsResponse = new SmsResponse();
smsResponse.setSuccess(ObjectUtil.isEmpty(jsonObject.getStr("error")));
smsResponse.setData(jsonObject);
smsResponse.setConfigId(getConfigId());
return smsResponse;
}
/**
* @param phones
* @param templateId
* @param messages
* @return SmsResponse
* @author 初拥
* @date 2024/1/31 9:20
* @Description: 发送群发短信
*/
private SmsResponse senMassMsg(List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
String url = getConfig().getBaseUrl() + getConfig().getMassMsgUrl();
HashMap<String, Object> params = new HashMap<>();
params.put("template_id", templateId);
params.put("mobiles", phones.toArray());
params.put("parameters", messages);
return handleRes(http.postJson(url, QiNiuUtils.getHeaderAndSign(url, params, getConfig()), params));
}
private SmsResponse sendSingleMsg(String phone, String templateId, LinkedHashMap<String, String> messages) {
String url = getConfig().getBaseUrl() + getConfig().getSingleMsgUrl();
//手机号
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("mobile", phone);
hashMap.put("template_id", templateId);
hashMap.put("parameters", messages);
log.info("hashMap:{}", hashMap);
return handleRes(http.postJson(url, QiNiuUtils.getHeaderAndSign(url, hashMap, getConfig()), hashMap));
}
}

View File

@ -1,11 +1,11 @@
package org.dromara.sms4j.qiniu.util;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.comm.constant.Constant;
import org.dromara.sms4j.comm.exception.SmsBlendException;
import org.dromara.sms4j.qiniu.config.QiNiuConfig;
import javax.crypto.Mac;
@ -13,17 +13,11 @@ import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.Date;
import java.util.*;
import static org.apache.commons.codec.digest.HmacUtils.hmacSha1;
@ -33,29 +27,50 @@ import static org.apache.commons.codec.digest.HmacUtils.hmacSha1;
* @描述: QiNiuUtils
**/
@Data
@Slf4j
public class QiNiuUtils {
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
public static String getSignature(String method, String url, QiNiuConfig qiNiuConfig, String body) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, MalformedURLException {
URL reqUrl = new URL(url);
public static String getSignature(String method, String url, QiNiuConfig qiNiuConfig, String body, String signDate) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, MalformedURLException {
URI reqUrl = URI.create(url);
StringBuilder dataToSign = new StringBuilder();
dataToSign.append(method.toUpperCase()).append(" ").append(reqUrl.getPath()).append('\n');
dataToSign.append("Host: ").append(reqUrl.getHost()).append('\n');
dataToSign.append("Content-Type: ").append(Constant.ACCEPT).append("\n").append("\n");
if (body != null && !body.isEmpty()) {
dataToSign.append(method.toUpperCase()).append(" ").append(reqUrl.getPath());
dataToSign.append("\nHost: ").append(reqUrl.getHost());
dataToSign.append("\n").append("Content-Type").append(": ").append(Constant.ACCEPT);
dataToSign.append("\n").append("X-Qiniu-Date").append(": ").append(signDate);
dataToSign.append("\n\n");
if (ObjectUtil.isNotEmpty(body)) {
dataToSign.append(body);
}
System.out.println(dataToSign.toString());
Mac sha1Mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
SecretKeySpec secretKeySpec = new SecretKeySpec(qiNiuConfig.getAccessKeySecret().getBytes(StandardCharsets.UTF_8), HMAC_SHA1_ALGORITHM);
sha1Mac.init(secretKeySpec);
byte[] signData = sha1Mac.doFinal(dataToSign.toString().getBytes(StandardCharsets.UTF_8));
String encodedSignature = Base64.getEncoder().encodeToString(signData);
System.out.println("Qiniu " + qiNiuConfig.getAccessKeyId() + ":" + encodedSignature);
System.out.println("encodedSignature = " + encodedSignature);
return "Qiniu " + qiNiuConfig.getAccessKeyId() + ":" + encodedSignature;
}
public static Map<String, String> getHeaderAndSign(String url, HashMap<String, Object> hashMap, QiNiuConfig qiNiuConfig) {
String signature = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String signDate = dateFormat.format(new Date());
try {
signature = getSignature("POST", url, qiNiuConfig, JSONUtil.toJsonStr(hashMap), signDate);
} catch (Exception e) {
log.error("签名失败", e);
throw new SmsBlendException(e.getMessage());
}
//请求头
Map<String, String> header = new HashMap<>();
header.put("Authorization", signature);
header.put("X-Qiniu-Date", signDate);
header.put("Content-Type", "application/json");
return header;
}
}

View File

@ -1,10 +1,14 @@
package org.dromara.sms4j.example;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.comm.constant.SupplierConstant;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.ArrayList;
import java.util.LinkedHashMap;
/**
* 主类
*
@ -15,7 +19,37 @@ public class Sms4jApplication {
public static void main(String[] args) {
SpringApplication.run(Sms4jApplication.class, args);
SmsFactory.getBySupplier(SupplierConstant.QINIU).sendMessage("13539060844", "1234");
// SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.QINIU).sendMessage("135****0844", "1234");
// LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
// linkedHashMap.put("code", "2234");
// SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.QINIU).sendMessage("135****0844", "1752130467315859456",linkedHashMap);
// System.out.println(smsResponse);
// LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
// linkedHashMap.put("code", "3234");
// SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.QINIU).sendMessage("135****0844", linkedHashMap);
// System.out.println(smsResponse);
// ArrayList<String> list = new ArrayList<>();
// list.add("135****0844");
// list.add("175****5952");
//
// SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.QINIU)
// .massTexting(list, "1234");
// System.out.println(smsResponse);
// ArrayList<String> list = new ArrayList<>();
// list.add("135****0844");
// list.add("175****65952");
//
// LinkedHashMap<String, String> hashMap = new LinkedHashMap<>();
// hashMap.put("code", "2234");
// SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.QINIU)
// .massTexting(list,"1752130467315859456", hashMap);
// System.out.println(smsResponse);
}
}

View File

@ -87,11 +87,12 @@ sms:
# sid: d2a****777
# url: https://sms.idowe.com/**/**/**/send
qiniu:
access-key-id: z_fGcmAj0XOlM_K0KhurGbesdh1ec_nJNCjmo8Ag
access-key-secret: xz1AzgPsVnhQaPHy1y_tvajgn3fIVL5bw_qy_hLK
access-key-id: EQcDflLTCYnU1hqQaYw-qkvYCmqIYLhog1lkWHb2
access-key-secret: NeS2ptvZQoIypnN6u97WQmerr2DdLe7wxFfQvji1
templateId: 1752130467315859456
signatureId: 1751851165571624960
templateName: code
sms-oa:
config-type: yaml
oas: