mirror of https://gitee.com/dromara/sms4j.git
七牛云初始化
This commit is contained in:
parent
55c957ca27
commit
9e32e3403a
|
@ -59,5 +59,6 @@ public abstract class SupplierConstant {
|
|||
* 鼎众
|
||||
*/
|
||||
public static final String DINGZHONG = "dingzhong";
|
||||
public static final String QINIU = "qiniu";
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
package org.dromara.sms4j.qiniu.config;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.dromara.sms4j.comm.constant.SupplierConstant;
|
||||
import org.dromara.sms4j.provider.config.BaseConfig;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* @Date: 2024/1/30 15:56 30
|
||||
* @描述: QiNiuConfig
|
||||
**/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class QiNiuConfig extends BaseConfig {
|
||||
|
||||
|
||||
private String baseUrl = "https://sms.qiniuapi.com";
|
||||
private String templateName;
|
||||
|
||||
private String singleMsgUrl = "/v1/message/single";
|
||||
|
||||
|
||||
private String signatureId;
|
||||
|
||||
@Override
|
||||
public String getSupplier() {
|
||||
return SupplierConstant.QINIU;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package org.dromara.sms4j.qiniu.config;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.dromara.sms4j.comm.constant.SupplierConstant;
|
||||
import org.dromara.sms4j.netease.config.NeteaseConfig;
|
||||
import org.dromara.sms4j.netease.config.NeteaseFactory;
|
||||
import org.dromara.sms4j.netease.service.NeteaseSmsImpl;
|
||||
import org.dromara.sms4j.provider.factory.AbstractProviderFactory;
|
||||
import org.dromara.sms4j.qiniu.service.QiNiuSmsImpl;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* @Date: 2024/1/30 16:06 29
|
||||
* @描述: QiNiuFactory
|
||||
**/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class QiNiuFactory extends AbstractProviderFactory<QiNiuSmsImpl, QiNiuConfig> {
|
||||
|
||||
private static final QiNiuFactory INSTANCE = new QiNiuFactory();
|
||||
|
||||
|
||||
public static QiNiuFactory instance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public QiNiuSmsImpl createSms(QiNiuConfig qiNiuConfig) {
|
||||
return new QiNiuSmsImpl(qiNiuConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSupplier() {
|
||||
return SupplierConstant.QINIU;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
package org.dromara.sms4j.qiniu.service;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.comm.constant.SupplierConstant;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.netease.config.NeteaseConfig;
|
||||
import org.dromara.sms4j.provider.service.AbstractSmsBlend;
|
||||
import org.dromara.sms4j.qiniu.config.QiNiuConfig;
|
||||
import org.dromara.sms4j.qiniu.util.QiNiuUtils;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* @Date: 2024/1/30 16:06 59
|
||||
* @描述: QiNiuSmsImpl
|
||||
**/
|
||||
@Slf4j
|
||||
public class QiNiuSmsImpl extends AbstractSmsBlend<QiNiuConfig> {
|
||||
@Override
|
||||
public String getSupplier() {
|
||||
return SupplierConstant.QINIU;
|
||||
}
|
||||
|
||||
public QiNiuSmsImpl(QiNiuConfig config, Executor pool, DelayedTime delayed) {
|
||||
super(config, pool, delayed);
|
||||
}
|
||||
|
||||
public QiNiuSmsImpl(QiNiuConfig config) {
|
||||
super(config);
|
||||
}
|
||||
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SmsResponse sendMessage(String phone, LinkedHashMap<String, String> messages) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SmsResponse sendMessage(String phone, String templateId, LinkedHashMap<String, String> messages) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SmsResponse massTexting(List<String> phones, String message) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SmsResponse massTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
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 lombok.Data;
|
||||
import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.qiniu.config.QiNiuConfig;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
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 static org.apache.commons.codec.digest.HmacUtils.hmacSha1;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* @Date: 2024/1/30 16:37 50
|
||||
* @描述: QiNiuUtils
|
||||
**/
|
||||
@Data
|
||||
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);
|
||||
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(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);
|
||||
|
||||
return "Qiniu " + qiNiuConfig.getAccessKeyId() + ":" + encodedSignature;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package org.dromara.sms4j.example;
|
||||
|
||||
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;
|
||||
|
||||
|
@ -13,6 +15,7 @@ public class Sms4jApplication {
|
|||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Sms4jApplication.class, args);
|
||||
SmsFactory.getBySupplier(SupplierConstant.QINIU).sendMessage("13539060844", "1234");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -79,13 +79,19 @@ sms:
|
|||
access-key-secret: 你的Access Key Secret
|
||||
sdkAppId: 你的应用ID
|
||||
#自定义广州掌骏短信,添加factory全路径。config,factory,SmsImpl复制其他默认实现即可,修改对应的supplier和发送核心逻辑即可
|
||||
zhangjun:
|
||||
supplier: zhangjun
|
||||
factory: org.dromara.sms4j.example.zhangjun.ZhangJunFactory
|
||||
templateId: d2a****777
|
||||
appId: 64c52d2a****77775fe72e3
|
||||
sid: d2a****777
|
||||
url: https://sms.idowe.com/**/**/**/send
|
||||
# zhangjun:
|
||||
# supplier: zhangjun
|
||||
# factory: org.dromara.sms4j.example.zhangjun.ZhangJunFactory
|
||||
# templateId: d2a****777
|
||||
# appId: 64c52d2a****77775fe72e3
|
||||
# sid: d2a****777
|
||||
# url: https://sms.idowe.com/**/**/**/send
|
||||
qiniu:
|
||||
access-key-id: z_fGcmAj0XOlM_K0KhurGbesdh1ec_nJNCjmo8Ag
|
||||
access-key-secret: xz1AzgPsVnhQaPHy1y_tvajgn3fIVL5bw_qy_hLK
|
||||
templateId: 1752130467315859456
|
||||
signatureId: 1751851165571624960
|
||||
templateName: code
|
||||
sms-oa:
|
||||
config-type: yaml
|
||||
oas:
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.dromara.sms4j.netease.config.NeteaseFactory;
|
|||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.provider.factory.BaseProviderFactory;
|
||||
import org.dromara.sms4j.provider.factory.ProviderFactoryHolder;
|
||||
import org.dromara.sms4j.qiniu.config.QiNiuFactory;
|
||||
import org.dromara.sms4j.starter.adepter.ConfigCombineMapAdeptor;
|
||||
import org.dromara.sms4j.tencent.config.TencentFactory;
|
||||
import org.dromara.sms4j.unisms.config.UniFactory;
|
||||
|
@ -119,6 +120,7 @@ public class SmsBlendsInitializer {
|
|||
ProviderFactoryHolder.registerFactory(ZhutongFactory.instance());
|
||||
ProviderFactoryHolder.registerFactory(LianLuFactory.instance());
|
||||
ProviderFactoryHolder.registerFactory(DingZhongFactory.instance());
|
||||
ProviderFactoryHolder.registerFactory(QiNiuFactory.instance());
|
||||
if(SmsUtils.isClassExists("com.jdcloud.sdk.auth.CredentialsProvider")) {
|
||||
ProviderFactoryHolder.registerFactory(JdCloudFactory.instance());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue