AliyunSmsUtil.java
8.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.zhongzhi.common.utils;
import com.alibaba.fastjson.JSON;
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.zhongzhi.common.configure.AliyunSmsProperties;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.zhongzhi.common.constant.SMSTemplate;
import com.zhongzhi.common.exception.HttpException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
/**
* <p>
* 阿里云短信
* </p>
*
* @author DengMin
* @since 2021/1/27
*/
@Slf4j
@Component
public class AliyunSmsUtil {
@Autowired
private AliyunSmsProperties aliyunSmsProperties;
/**
* 阿里云短信 -- 验证码
* @param phone
* @param code
* @return
*/
public SendSmsResponse send(String phone, int code) {
try {
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunSmsProperties.getAccessKeyId(), aliyunSmsProperties.getAccessKeySecret());
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", aliyunSmsProperties.getProduct(), aliyunSmsProperties.getDomain());
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号
request.setPhoneNumbers(phone);
//必填:短信签名-可在短信控制台中找到
request.setSignName("双创平台");
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode("SMS_229640297");
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam("{\"code\":\"" + code + "\"}");
//hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
if (sendSmsResponse != null && sendSmsResponse.getCode().equals("OK")) {
return sendSmsResponse;
}
throw new Exception(sendSmsResponse.getMessage());
} catch (Exception e) {
log.error("---短信发送失败:" + e);
throw new HttpException(10011);
}
}
public static void main(String[] args) {
// AliyunSmsUtil.sendNew("15201936167",000123);
}
/**
* 三网平台短信
* @param msg
* @param phone
* @param templateId
*/
public void sendTemplateMsg(String msg, String phone, String templateId) {
Map<String, Object> map = new HashMap<>();
try {
map.put("cpcode", aliyunSmsProperties.getCpcode());
map.put("msg", msg);
map.put("mobiles", phone);
map.put("excode", aliyunSmsProperties.getExcode());
map.put("templetid", templateId);
String md5source = aliyunSmsProperties.getCpcode() + msg + phone + aliyunSmsProperties.getExcode() + templateId + aliyunSmsProperties.getKey();
map.put("sign", makeMD5(new String(md5source.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)).toLowerCase());
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.postForEntity(aliyunSmsProperties.getRcsapi(), map, String.class);
String body = responseEntity.getBody();
JSONObject object = JSON.parseObject(body);
if (Integer.valueOf(object.get("resultcode").toString()) != 0) {
log.error(object.get("resultmsg").toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
// /**
// * 三网平台短信 新版本 https://flaginfo-cloud.yuque.com/staff-rozzgq/ofcpak
// * @param msg
// * @param phone
// * @param templateId
// */
// public static void sendTemplateMsgNew(String msg, String phone, String templateId) {
// String info = null;
// try{
// HttpClient httpclient = new HttpClient();
// PostMethod post = new PostMethod("https://opassapi.infocloud.cc/sms/Api/SendGBK.do");//
// post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"gb2312");
// post.addParameter("SpCode", templateId);
// post.addParameter("LoginName", "7db124d0baf189d1e485350xxx");
// post.addParameter("Password", "3df50b24fb8288b9da99d16bf527e9dbf13659748534dca6e0820xxx");
// post.addParameter("MessageContent", msg);
// post.addParameter("UserNumber", phone);
// post.addParameter("SerialNumber", "");
// post.addParameter("f", "1");
// httpclient.executeMethod(post);
// info = new String(post.getResponseBody(),"gbk");
// System.out.println(info);
// }catch (Exception e) {
// e.printStackTrace();
// }
// }
public void sendTemplateMsgP(String msg, String phone, String templateId) {
Map<String, Object> map = new HashMap<>();
try {
map.put("cpcode", aliyunSmsProperties.getCpcode());
map.put("msg", msg);
map.put("mobiles", phone);
map.put("excode", aliyunSmsProperties.getExcode());
map.put("templetid", templateId);
String md5source = aliyunSmsProperties.getCpcode() + msg + phone + aliyunSmsProperties.getExcode() + templateId + aliyunSmsProperties.getKey();
map.put("sign", makeMD5(new String(md5source.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)).toLowerCase());
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.postForEntity(aliyunSmsProperties.getRcsapi(), map, String.class);
String body = responseEntity.getBody();
JSONObject object = JSON.parseObject(body);
if (Integer.valueOf(object.get("resultcode").toString()) != 0) {
log.error(object.get("resultmsg").toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String makeMD5(String plainText) {
String re_md5 = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes(StandardCharsets.UTF_8));
byte[] b = md.digest();
int i;
StringBuffer buf = new StringBuffer();
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0) {
i += 256;
}
if (i < 16) {
buf.append("0");
}
buf.append(Integer.toHexString(i));
}
re_md5 = buf.toString().toLowerCase();
} catch (Exception e) {
e.printStackTrace();
}
return re_md5;
}
// public static String getByteString( byte[] buff_out )
// {
// StringBuffer strBuf = new StringBuffer(buff_out.length * 3);
// strBuf.append("Length[");
// strBuf.append(buff_out.length);
// strBuf.append("];Content[");
// for ( int i = 0 ; i < buff_out.length ; ++i ) {
// int l = buff_out[i] & 0x0F;
// int h = (buff_out[i] & 0xF0) >> 4;
//
// char ll = (char) (l > 9 ? 'a' + l - 10 : '0' + l);
// char hh = (char) (h > 9 ? 'a' + h - 10 : '0' + h);
//
// strBuf.append(hh);
// strBuf.append(ll);
// strBuf.append(" ");
// }
// strBuf.append("]");
// return strBuf.toString().toUpperCase();
// }
}