一、官网下载对接文档
http://mas.10086.cn/login
二、登录云平台配置账户
管理–>接口管理–>新建短信接口建立自己的用户信息
三、建立好账户后导出证书(用于对接)
四、编写Java对接代码
1.application.yml
smscode:
apId: xxxx
secretKey: xxxx
ecName: xxxx
sign: xxxx
url: https://112.35.10.201:28888/sms/submit
注:网关签名编码既第三步导出签名的编码
2.SMSUtils
import lombok.Data;
import org.apache.commons.codec.binary.Base64;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
@Component
@ConfigurationProperties("smscode")
@Data
public class SMSUtils {
private String apId;
private String secretKey;
private String ecName;
private String sign;
private String addSerial;
public String url;
public int sendMsg(String mobiles, String content) throws IOException {
SendReq sendReq = new SendReq();
sendReq.setApId(apId);
sendReq.setEcName(ecName);
sendReq.setSecretKey(secretKey);
sendReq.setContent(content);
sendReq.setMobiles(mobiles);
sendReq.setAddSerial(addSerial);
sendReq.setSign(sign);
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(sendReq.getEcName());
stringBuffer.append(sendReq.getApId());
stringBuffer.append(sendReq.getSecretKey());
stringBuffer.append(sendReq.getMobiles());
stringBuffer.append(sendReq.getContent());
stringBuffer.append(sendReq.getSign());
stringBuffer.append(sendReq.getAddSerial());
sendReq.setMac(Md5Util.MD5(stringBuffer.toString()).toLowerCase());
String reqText = JSON.toJSONString(sendReq);
String encode = Base64.encodeBase64String(reqText.getBytes("UTF-8"));
String resStr = HttpRequest.post(url)
.header("contentType", "utf-8")
.body(encode)
.execute()
.body();
System.out.println("发送短信结果:" + resStr);
SendRes sendRes = JSON.parseObject(resStr, SendRes.class);
if (sendRes.isSuccess() && !"".equals(sendRes.getMsgGroup()) && "success".equals(sendRes.getRspcod())) {
return 1;
} else {
return 0;
}
}
}
3.Md5Util
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
public class Md5Util {
static final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
public final static String MD5(String plainText) {
try {
byte[] strTemp = plainText.getBytes("UTF-8");
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}
public final static String MD5(byte[] plainText) {
try {
byte[] strTemp = plainText;
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}
public static String HmacSHA1ToBase64(String data, String key) throws Exception {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(data.getBytes());
return Base64.encodeBase64String(rawHmac);
}
public static boolean valid(String text, String md5) {
return md5.equals(MD5(text)) || md5.equals(MD5(text).toUpperCase());
}
public static String MD5(String... params) {
StringBuilder sb = new StringBuilder();
for (String param : params) {
sb.append(param);
}
return MD5(sb.toString());
}
}
4.SendReq
import lombok.Data;
@Data
public class SendReq {
private String ecName;
private String apId;
private String secretKey;
private String mobiles;
private String content;
private String sign;
private String addSerial;
private String mac;
}
5.SendRes
import lombok.Data;
@Data
public class SendRes {
private String rspcod;
private String msgGroup;
private boolean success;
}
|