KuaidiServiceImpl.java
20 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package com.huaheng.api.WAYBILLNO.service;
import com.huaheng.pc.config.KDCertification.domain.KDCertification;
import com.huaheng.pc.config.KDCertification.service.KDCertificationService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.TreeMap;
@Service
public class KuaidiServiceImpl implements KuaidiService {
@Resource
private KDCertificationService kdCertificationService;
private static String appKey = "dEalu7vYJMAA"; //昆山华恒焊接股份有限公司
private static String appSecret = "5447e797b54442778d12da773172d397";
static {
try {
SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
sslcontext.init(null, new TrustManager[]{new MyX509TrustManager()}, new java.security.SecureRandom());
HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslsession) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 校验回调信息的sign
*
* @param callbackData 回调的参数数据
* @return 校验是否通过
*/
@Override
public boolean callbackVerify(Map<String, String> callbackData) {
callbackData = transformToTreeMap(callbackData);
StringBuilder sbd = new StringBuilder(appSecret);
for (Map.Entry<String, String> entry : callbackData.entrySet()) {
if (!"sign".equals(entry.getKey())) {
sbd.append(entry.getKey()).append(entry.getKey());
}
}
sbd.append(appSecret);
return new MD5().encode(sbd.toString()).equals(callbackData.get("sign"));
}
/**
* 生成授权地址
*
* @param state state
* @return 授权地址
* @throws UnsupportedEncodingException
*/
@Override
public String authorize(String state) throws Exception {
KDCertification kdCertification = new KDCertification();
kdCertification = kdCertificationService.getOne(kdCertification);
String timestamp = String.valueOf(System.currentTimeMillis());
state = null == state ? "" : state;
Map<String, String> data = new TreeMap<>();
data.put("client_id", appKey);
data.put("response_type", "code");
data.put("redirect_uri", kdCertification.getRedirectUrl());
data.put("state", state);
data.put("timestamp", timestamp);
String sign = generateSignByClass(data, kdCertification.getAppSecret());
return "https://b.kuaidi100.com/open/oauth/authorize?response_type=code&client_id=" + appKey + "&redirect_uri=" + URLEncoder.encode(kdCertification.getRedirectUrl(), "UTF-8") + "&state=" + state + "×tamp=" + timestamp + "&sign=" + sign;
}
/**
* 获取登录
*
* @param state
* @param appkeys
* @param appsecrets
* @return
* @throws Exception
*/
public static String authorizeByClass(String state, String appkeys, String appsecrets) throws Exception {
// KDCertification kdCertification = new KDCertification();
// LambdaQueryWrapper<KDCertification> wrapper = Wrappers.lambdaQuery();
// wrapper.eq(KDCertification::getWarehouseCode, warehouseCode);
// kdCertification = kdCertificationService.getOne(kdCertification);
String redirectUrl = "http://222.92.108.42:443/wms/api/kuaidi/test";
String timestamp = String.valueOf(System.currentTimeMillis());
state = null == state ? "" : state;
Map<String, String> data = new TreeMap<>();
data.put("client_id", appkeys);
data.put("response_type", "code");
data.put("redirect_uri", redirectUrl);
data.put("state", state);
data.put("timestamp", timestamp);
String sign = generateSignByClass(data, appsecrets);
return "https://b.kuaidi100.com/open/oauth/authorize?response_type=code&client_id=" + appkeys + "&redirect_uri=" + URLEncoder.encode(redirectUrl, "UTF-8") + "&state=" + state + "×tamp=" + timestamp + "&sign=" + sign;
}
/**
* 用授权得到的code换取accessToken
* 废弃接口
*
* @param code 临时凭证
* @return
*/
@Override
public String accessToken(String code) throws Exception {
KDCertification kdCertification = new KDCertification();
kdCertification = kdCertificationService.getOne(kdCertification);
if (null == code || code.length() < 1) {
throw new RuntimeException("无效参数");
}
String timestamp = String.valueOf(System.currentTimeMillis());
Map<String, String> data = new TreeMap<>();
data.put("client_id", appKey);
data.put("client_secret", appSecret);
data.put("grant_type", "authorization_code");
data.put("code", code);
data.put("redirect_uri", kdCertification.getRedirectUrl());
data.put("timestamp", timestamp);
data.put("sign", generateSign(data));
return httpsRequest("https://b.kuaidi100.com/open/oauth/accessToken", data);
}
public static String accessTokenByClass(String code, String appKeys, String appSecrets) throws Exception {
if (null == code || code.length() < 1) {
throw new RuntimeException("无效参数");
}
String redirectUrl = "http://222.92.108.42:443/wms/api/kuaidi/test";
String timestamp = String.valueOf(System.currentTimeMillis());
Map<String, String> data = new TreeMap<>();
data.put("client_id", appKeys);
data.put("client_secret", appSecrets);
data.put("grant_type", "authorization_code");
data.put("code", code);
data.put("redirect_uri", redirectUrl);
data.put("timestamp", timestamp);
data.put("sign", generateSignByClass(data, appSecrets));
return httpsRequestStatic("https://b.kuaidi100.com/open/oauth/accessToken", data);
}
/**
* 刷新accessToken
*
* @param refreshToken 刷新token
* @return
*/
@Override
public String refreshToken(String refreshToken) throws Exception {
if (null == refreshToken || refreshToken.length() < 1) {
throw new RuntimeException("无效参数");
}
String timestamp = String.valueOf(System.currentTimeMillis());
Map<String, String> data = new TreeMap<>();
data.put("client_id", appKey);
data.put("client_secret", appSecret);
data.put("grant_type", "refresh_token");
data.put("refresh_token", refreshToken);
data.put("timestamp", timestamp);
data.put("sign", generateSign(data));
return httpsRequest("https://b.kuaidi100.com/open/oauth/refreshToken", data);
}
/**
* 刷新accessToken
*
* @param kdCertification 刷新token
* @return
*/
@Override
public String refreshTokenByClass(KDCertification kdCertification) throws Exception {
if (null == kdCertification.getRefreshToken() || kdCertification.getRefreshToken().length() < 1) {
throw new RuntimeException("无效参数");
}
String timestamp = String.valueOf(System.currentTimeMillis());
Map<String, String> data = new TreeMap<>();
data.put("client_id", kdCertification.getAppKey());
data.put("client_secret", kdCertification.getAppSecret());
data.put("grant_type", "refresh_token");
data.put("refresh_token", kdCertification.getRefreshToken());
data.put("timestamp", timestamp);
data.put("sign", generateSignByClass(data, kdCertification.getAppSecret()));
System.out.println(data);
return httpsRequest("https://b.kuaidi100.com/open/oauth/refreshToken", data);
}
/**
* 导入订单
*
* @param accessToken 身份凭证
* @param orderData 订单信息
* @return
*/
@Override
public String send(String accessToken, String orderData) throws Exception {
if (null == accessToken || accessToken.length() < 1
|| null == orderData || orderData.length() < 1) {
throw new RuntimeException("无效参数");
}
String timestamp = String.valueOf(System.currentTimeMillis());
Map<String, String> data = new TreeMap<>();
data.put("appid", appKey);
data.put("access_token", accessToken);
data.put("data", orderData);
data.put("timestamp", timestamp);
data.put("sign", generateSign(data));
return httpsRequest("https://b.kuaidi100.com/v6/open/api/send", data);
}
/**
* 修改订单
*
* @param accessToken 身份凭证
* @param orderData 订单信息
* @return
*/
@Override
public String update(String accessToken, String orderData) throws Exception {
if (null == accessToken || accessToken.length() < 1
|| null == orderData || orderData.length() < 1) {
throw new RuntimeException("无效参数");
}
String timestamp = String.valueOf(System.currentTimeMillis());
Map<String, String> data = new TreeMap<>();
data.put("appid", appKey);
data.put("access_token", accessToken);
data.put("data", orderData);
data.put("timestamp", timestamp);
data.put("sign", generateSign(data));
return httpsRequest("https://b.kuaidi100.com/v6/open/api/update", data);
}
/**
* 快速打印
*
* @param accessToken 身份凭证
* @param printList 订单号列表
* @return 打印订单的地址
*/
@Override
public String quickPrint(String accessToken, String printList) {
if (null == accessToken || accessToken.length() < 1
|| null == printList || printList.length() < 1) {
throw new RuntimeException("无效参数");
}
String timestamp = String.valueOf(System.currentTimeMillis());
Map<String, String> data = new TreeMap<>();
data.put("appid", appKey);
data.put("access_token", accessToken);
data.put("printlist", printList);
data.put("timestamp", timestamp);
String sign = generateSign(data);
return "https://b.kuaidi100.com/v6/open/api/print?appid=" + appKey + "&access_token=" + accessToken + "&printlist=" + printList + "×tamp=" + timestamp + "&sign=" + sign;
}
/**
* 自动打印
*
* @param accessToken 身份凭证
* @param printList 订单号列表
* @return
*/
@Override
public String autoPrint(String accessToken, String printList) throws Exception {
String timestamp = String.valueOf(System.currentTimeMillis());
Map<String, String> data = new TreeMap<>();
data.put("appid", appKey);
data.put("access_token", accessToken);
data.put("printlist", printList);
data.put("timestamp", timestamp);
data.put("sign", generateSign(data));
return httpsRequest("https://b.kuaidi100.com/v6/open/api/autoPrint", data);
}
/**
* 生成签名
*
* @param data 请求数据
* @return 签名
*/
private static String generateSign(Map<String, String> data) {
data = transformToTreeMap(data);
StringBuilder sbd = new StringBuilder(appSecret);
for (Map.Entry<String, String> entry : data.entrySet()) {
sbd.append(entry.getKey()).append(entry.getValue());
}
sbd.append(appSecret);
return new MD5().encode(sbd.toString());
}
/**
* 生成签名
*
* @param data 请求数据
* @return 签名
*/
private static String generateSignByClass(Map<String, String> data, String appSecrets) {
data = transformToTreeMap(data);
StringBuilder sbd = new StringBuilder(appSecrets);
for (Map.Entry<String, String> entry : data.entrySet()) {
sbd.append(entry.getKey()).append(entry.getValue());
}
sbd.append(appSecrets);
return new MD5().encode(sbd.toString());
}
public static void main(String[] args) throws Exception {
// String redirectUrl = "http://222.92.108.42:443/wms/api/kuaidi/test";
// String timestamp = String.valueOf(System.currentTimeMillis());
// String state = "";
// Map<String, String> data = new TreeMap<>();
// data.put("client_id", "wKa32LXi8F9t");
// data.put("response_type", "code");
// data.put("redirect_uri", redirectUrl);
// data.put("state", state);
// data.put("timestamp", timestamp);
// String sign = generateSignByClass(data, "918dc02a3348480380f607da0bb75c2b");
// System.out.println(timestamp);
// System.out.println(sign);
String appkey = "wKa32LXi8F9t";
String appscrets = "918dc02a3348480380f607da0bb75c2b";
// String str = authorizeByClass("", appkey, appscrets);
// System.out.println(str);
String str1 = accessTokenByClass("lhzANxEd", appkey, appscrets);
System.out.println(str1);
}
/**
* 把map转换为treeMap
*
* @param map 任意类型的map
* @return treeMap
*/
private static Map<String, String> transformToTreeMap(Map<String, String> map) {
return map instanceof TreeMap ? map : new TreeMap<>(map);
}
/**
* 发送http请求
*
* @param url 请求地址
* @param data 请求参数
* @return 接口返回的结果
*/
private String httpsRequest(String url, Map<String, String> data) throws Exception {
URL apiUrl = new URL(url);
StringBuilder sbd = new StringBuilder();
StringBuilder paramSbd = new StringBuilder();
if (null != data) {
for (Map.Entry<String, String> entry : data.entrySet()) {
paramSbd.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
}
String param = paramSbd.length() > 0 ? paramSbd.substring(0, paramSbd.length() - 1) : "";
try {
HttpURLConnection httpConn = (HttpURLConnection) apiUrl.openConnection();
//设置参数
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setUseCaches(false);
httpConn.setRequestMethod("POST");
//设置请求属性
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConn.setRequestProperty("Charset", "UTF-8");
httpConn.setRequestProperty("Accept", "application/json;charset=UTF-8");
//建立输入流,向指向的URL传入参数
OutputStream out = httpConn.getOutputStream();
out.write(param.getBytes("UTF-8"));
out.flush();
out.close();
//获得响应状态
int resultCode = httpConn.getResponseCode();
if (HttpURLConnection.HTTP_OK == resultCode) {
String readLine;
BufferedReader responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
while ((readLine = responseReader.readLine()) != null) {
sbd.append(readLine).append("\n");
}
responseReader.close();
System.out.println(sbd.toString());
} else {
throw new RuntimeException("http请求失败:" + httpConn.getResponseCode());
}
} catch (Exception e) {
throw new RuntimeException("http请求失败", e);
}
return sbd.toString();
}
/**
* 发送http请求
*
* @param url 请求地址
* @param data 请求参数
* @return 接口返回的结果
*/
private static String httpsRequestStatic(String url, Map<String, String> data) throws Exception {
URL apiUrl = new URL(url);
StringBuilder sbd = new StringBuilder();
StringBuilder paramSbd = new StringBuilder();
if (null != data) {
for (Map.Entry<String, String> entry : data.entrySet()) {
paramSbd.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
}
String param = paramSbd.length() > 0 ? paramSbd.substring(0, paramSbd.length() - 1) : "";
try {
HttpURLConnection httpConn = (HttpURLConnection) apiUrl.openConnection();
//设置参数
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setUseCaches(false);
httpConn.setRequestMethod("POST");
//设置请求属性
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConn.setRequestProperty("Charset", "UTF-8");
httpConn.setRequestProperty("Accept", "application/json;charset=UTF-8");
//建立输入流,向指向的URL传入参数
OutputStream out = httpConn.getOutputStream();
out.write(param.getBytes("UTF-8"));
out.flush();
out.close();
//获得响应状态
int resultCode = httpConn.getResponseCode();
if (HttpURLConnection.HTTP_OK == resultCode) {
String readLine;
BufferedReader responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
while ((readLine = responseReader.readLine()) != null) {
sbd.append(readLine).append("\n");
}
responseReader.close();
System.out.println(sbd.toString());
} else {
throw new RuntimeException("http请求失败:" + httpConn.getResponseCode());
}
} catch (Exception e) {
throw new RuntimeException("http请求失败", e);
}
return sbd.toString();
}
static class MD5 {
// 获得MD5摘要算法的 MessageDigest 对象
private MessageDigest _mdInst = null;
private char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private MessageDigest getMdInst() {
if (_mdInst == null) {
try {
_mdInst = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
return _mdInst;
}
String encode(String s) {
try {
byte[] btInput = s.getBytes("UTF-8");
// 使用指定的字节更新摘要
getMdInst().update(btInput);
// 获得密文
byte[] md = getMdInst().digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (byte byte0 : md) {
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
static class MyX509TrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate certificates[], String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] ax509certificate, String s) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
}
}