tao.mo

指定传输协议SSLv1.2

mt
2022年3月8日13:24:00
...@@ -97,6 +97,18 @@ ...@@ -97,6 +97,18 @@
97 <artifactId>fastjson</artifactId> 97 <artifactId>fastjson</artifactId>
98 <version>1.2.38</version> 98 <version>1.2.38</version>
99 </dependency> 99 </dependency>
100 +
101 +
102 + <dependency>
103 + <groupId>org.apache.httpcomponents</groupId>
104 + <artifactId>httpcore</artifactId>
105 + <version>4.4.8</version>
106 + </dependency>
107 + <dependency>
108 + <groupId>org.apache.httpcomponents</groupId>
109 + <artifactId>httpclient</artifactId>
110 + <version>4.5.3</version>
111 + </dependency>
100 </dependencies> 112 </dependencies>
101 113
102 <profiles> 114 <profiles>
......
1 package com.erry.iclear.dateInterface.service; 1 package com.erry.iclear.dateInterface.service;
2 2
3 -import com.arronlong.httpclientutil.HttpClientUtil;
4 -import com.arronlong.httpclientutil.common.HttpConfig;
5 -import com.arronlong.httpclientutil.common.HttpHeader;
6 import com.erry.iclear.dateInterface.api.request.Mawb; 3 import com.erry.iclear.dateInterface.api.request.Mawb;
7 import com.erry.iclear.dateInterface.api.request.MawbDetail; 4 import com.erry.iclear.dateInterface.api.request.MawbDetail;
8 import com.erry.iclear.dateInterface.api.request.ReceiptMessage; 5 import com.erry.iclear.dateInterface.api.request.ReceiptMessage;
...@@ -16,15 +13,31 @@ import com.erry.iclear.dateInterface.repository.DeclearTaskRepository; ...@@ -16,15 +13,31 @@ import com.erry.iclear.dateInterface.repository.DeclearTaskRepository;
16 import com.erry.iclear.dateInterface.repository.HsCodeRepository; 13 import com.erry.iclear.dateInterface.repository.HsCodeRepository;
17 import com.erry.iclear.dateInterface.repository.ReceiptMessageRepository; 14 import com.erry.iclear.dateInterface.repository.ReceiptMessageRepository;
18 import com.erry.iclear.dateInterface.util.DateUtils; 15 import com.erry.iclear.dateInterface.util.DateUtils;
16 +import com.erry.iclear.dateInterface.util.HttpUtils;
19 import com.erry.iclear.dateInterface.util.JsonToJava; 17 import com.erry.iclear.dateInterface.util.JsonToJava;
20 import lombok.extern.slf4j.Slf4j; 18 import lombok.extern.slf4j.Slf4j;
21 import org.apache.http.Header; 19 import org.apache.http.Header;
20 +import org.apache.http.HttpEntity;
21 +import org.apache.http.HttpResponse;
22 +import org.apache.http.client.config.RequestConfig;
23 +import org.apache.http.client.methods.HttpPost;
24 +import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
25 +import org.apache.http.entity.ContentType;
26 +import org.apache.http.entity.StringEntity;
27 +import org.apache.http.impl.client.CloseableHttpClient;
28 +import org.apache.http.impl.client.HttpClients;
29 +import org.apache.http.ssl.SSLContexts;
30 +import org.apache.http.util.EntityUtils;
22 import org.springframework.beans.factory.annotation.Autowired; 31 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.beans.factory.annotation.Value; 32 import org.springframework.beans.factory.annotation.Value;
24 import org.springframework.stereotype.Service; 33 import org.springframework.stereotype.Service;
25 34
35 +import javax.net.ssl.SSLContext;
26 import javax.transaction.Transactional; 36 import javax.transaction.Transactional;
37 +import java.io.IOException;
27 import java.math.BigDecimal; 38 import java.math.BigDecimal;
39 +import java.security.KeyManagementException;
40 +import java.security.NoSuchAlgorithmException;
28 import java.util.*; 41 import java.util.*;
29 42
30 /** 43 /**
...@@ -73,14 +86,10 @@ public class ApiService { ...@@ -73,14 +86,10 @@ public class ApiService {
73 * @throws Exception 86 * @throws Exception
74 */ 87 */
75 public TokenResult getToken() throws Exception{ 88 public TokenResult getToken() throws Exception{
76 - Map<String,Object> map = new HashMap<String,Object>(); 89 + Map<String,String> map = new HashMap<String,String>();
77 map.put("username", this.username); 90 map.put("username", this.username);
78 map.put("password", this.password); 91 map.put("password", this.password);
79 - HttpConfig config = HttpConfig.custom() 92 + String tokenJson = HttpUtils.doPost(this.tokenUrl,map);
80 - .url(this.tokenUrl)
81 - .map(map)
82 - .encoding("utf-8");
83 - String tokenJson = HttpClientUtil.post(config);
84 TokenResult tokenResult = JsonToJava.parseJson(tokenJson,TokenResult.class); 93 TokenResult tokenResult = JsonToJava.parseJson(tokenJson,TokenResult.class);
85 return tokenResult; 94 return tokenResult;
86 } 95 }
...@@ -91,13 +100,9 @@ public class ApiService { ...@@ -91,13 +100,9 @@ public class ApiService {
91 * @throws Exception 100 * @throws Exception
92 */ 101 */
93 public void refreshToken(String token) throws Exception{ 102 public void refreshToken(String token) throws Exception{
94 - Map<String,Object> map = new HashMap<String,Object>(); 103 + Map<String,String> map = new HashMap<String,String>();
95 map.put("token", token); 104 map.put("token", token);
96 - HttpConfig config = HttpConfig.custom() 105 + HttpUtils.doPost(this.refreshTokenUrl,map);
97 - .url(this.refreshTokenUrl)
98 - .map(map)
99 - .encoding("utf-8");
100 - HttpClientUtil.post(config);
101 } 106 }
102 107
103 /** 108 /**
...@@ -124,10 +129,7 @@ public class ApiService { ...@@ -124,10 +129,7 @@ public class ApiService {
124 * @param receiptMessageList 129 * @param receiptMessageList
125 */ 130 */
126 public void sendCustoms(String token ,List<ReceiptMessage> receiptMessageList){ 131 public void sendCustoms(String token ,List<ReceiptMessage> receiptMessageList){
127 - Header[] headers = HttpHeader.custom() 132 + Map<String,String> headerMap = this.createHeaderMap(token);
128 - .other("Content-Type", "application/json")
129 - .other("Authorization", token)
130 - .build();
131 List<DeclearTask> declearTaskList = new ArrayList<DeclearTask>(); 133 List<DeclearTask> declearTaskList = new ArrayList<DeclearTask>();
132 receiptMessageList.forEach(d->{ 134 receiptMessageList.forEach(d->{
133 DeclearTask declearTask = new DeclearTask(); 135 DeclearTask declearTask = new DeclearTask();
...@@ -138,12 +140,7 @@ public class ApiService { ...@@ -138,12 +140,7 @@ public class ApiService {
138 declearTask.setPushTime(new Date()); 140 declearTask.setPushTime(new Date());
139 String objJson = JsonToJava.toJson(d); 141 String objJson = JsonToJava.toJson(d);
140 //推送海关回执数据 142 //推送海关回执数据
141 - HttpConfig config = HttpConfig.custom() 143 + String json = HttpUtils.doPost(this.customsUrl,objJson,headerMap);
142 - .headers(headers)
143 - .url(this.customsUrl)
144 - .encoding("utf-8")
145 - .json(objJson);
146 - String json = HttpClientUtil.post(config);
147 //接口响应时间 144 //接口响应时间
148 declearTask.setResponseTime(new Date()); 145 declearTask.setResponseTime(new Date());
149 TokenResult tokenResult = JsonToJava.parseJson(json,TokenResult.class); 146 TokenResult tokenResult = JsonToJava.parseJson(json,TokenResult.class);
...@@ -228,10 +225,7 @@ public class ApiService { ...@@ -228,10 +225,7 @@ public class ApiService {
228 * @param mawbList 225 * @param mawbList
229 */ 226 */
230 public void sendMawb(String token ,List<Mawb> mawbList){ 227 public void sendMawb(String token ,List<Mawb> mawbList){
231 - Header[] headers = HttpHeader.custom() 228 + Map<String,String> headerMap = this.createHeaderMap(token);
232 - .other("Content-Type", "application/json")
233 - .other("Authorization", token)
234 - .build();
235 List<DeclearTask> declearTaskList = new ArrayList<DeclearTask>(); 229 List<DeclearTask> declearTaskList = new ArrayList<DeclearTask>();
236 mawbList.forEach(d->{ 230 mawbList.forEach(d->{
237 DeclearTask declearTask = new DeclearTask(); 231 DeclearTask declearTask = new DeclearTask();
...@@ -242,12 +236,7 @@ public class ApiService { ...@@ -242,12 +236,7 @@ public class ApiService {
242 declearTask.setPushTime(new Date()); 236 declearTask.setPushTime(new Date());
243 String objJson = JsonToJava.toJson(d); 237 String objJson = JsonToJava.toJson(d);
244 //推送海关回执数据 238 //推送海关回执数据
245 - HttpConfig config = HttpConfig.custom() 239 + String json = HttpUtils.doPost(this.customsUrl,objJson,headerMap);
246 - .headers(headers)
247 - .url(this.completeMsgUrl)
248 - .encoding("utf-8")
249 - .json(objJson);
250 - String json = HttpClientUtil.post(config);
251 //接口响应时间 240 //接口响应时间
252 declearTask.setResponseTime(new Date()); 241 declearTask.setResponseTime(new Date());
253 TokenResult tokenResult = JsonToJava.parseJson(json,TokenResult.class); 242 TokenResult tokenResult = JsonToJava.parseJson(json,TokenResult.class);
...@@ -317,4 +306,16 @@ public class ApiService { ...@@ -317,4 +306,16 @@ public class ApiService {
317 } 306 }
318 pushMawbAcc(token,declearTaskList); 307 pushMawbAcc(token,declearTaskList);
319 } 308 }
309 +
310 + /**
311 + * 获取headerMap
312 + * @param token
313 + * @return
314 + */
315 + private Map<String,String> createHeaderMap(String token){
316 + Map<String,String> headerMap = new HashMap<String,String>();
317 + headerMap.put("Content-Type", "application/json");
318 + headerMap.put("Authorization", token);
319 + return headerMap;
320 + }
320 } 321 }
......
1 +package com.erry.iclear.dateInterface.util;
2 +
3 +import java.util.ArrayList;
4 +import java.util.Iterator;
5 +import java.util.List;
6 +import java.util.Map;
7 +import java.util.Map.Entry;
8 +import org.apache.http.HttpEntity;
9 +import org.apache.http.HttpResponse;
10 +import org.apache.http.NameValuePair;
11 +import org.apache.http.client.HttpClient;
12 +import org.apache.http.client.entity.UrlEncodedFormEntity;
13 +import org.apache.http.client.methods.HttpPost;
14 +import org.apache.http.entity.StringEntity;
15 +import org.apache.http.message.BasicNameValuePair;
16 +import org.apache.http.util.EntityUtils;
17 +
18 +/**
19 + * 利用HttpClient进行post请求的工具类
20 + * mt
21 + * 2022年3月8日11:09:58
22 + */
23 +public class HttpUtils {
24 + private static String CHARSET = "utf-8";
25 +
26 + /**
27 + * post请求
28 + * @param url
29 + * @param json
30 + * @return
31 + */
32 + public static String doPost(String url,String json,Map<String,String> headerMap){
33 + HttpClient httpClient = null;
34 + HttpPost httpPost = null;
35 + String result = null;
36 + try{
37 + httpClient = new SSLClient();
38 + httpPost = new HttpPost(url);
39 + //设置参数
40 + List<NameValuePair> list = new ArrayList<NameValuePair>();
41 + StringEntity entity = new StringEntity(json, HttpUtils.CHARSET);//解决中文乱码问题
42 + entity.setContentEncoding("UTF-8");
43 + entity.setContentType("application/json");
44 + httpPost.setEntity(entity);
45 +
46 + Iterator headerIterator = headerMap.entrySet().iterator();
47 + while(headerIterator.hasNext()){
48 + Entry<String,String> elem = (Entry<String, String>) headerIterator.next();
49 + httpPost.addHeader(elem.getKey(),elem.getValue());
50 + }
51 +
52 + HttpResponse response = httpClient.execute(httpPost);
53 + if(response != null){
54 + HttpEntity resEntity = response.getEntity();
55 + if(resEntity != null){
56 + result = EntityUtils.toString(resEntity,HttpUtils.CHARSET);
57 + }
58 + }
59 + }catch(Exception ex){
60 + ex.printStackTrace();
61 + }
62 + return result;
63 + }
64 +
65 + /**
66 + * post请求 map参数
67 + * @param url
68 + * @param paramMap
69 + * @return
70 + */
71 + public static String doPost(String url,Map<String,String> paramMap){
72 + HttpClient httpClient = null;
73 + HttpPost httpPost = null;
74 + String result = null;
75 + try{
76 + httpClient = new SSLClient();
77 + httpPost = new HttpPost(url);
78 + //设置参数
79 + List<NameValuePair> list = new ArrayList<NameValuePair>();
80 + Iterator iterator = paramMap.entrySet().iterator();
81 + while(iterator.hasNext()){
82 + Entry<String,String> elem = (Entry<String, String>) iterator.next();
83 + list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
84 + }
85 + if(list.size() > 0){
86 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,HttpUtils.CHARSET);
87 + httpPost.setEntity(entity);
88 + }
89 + HttpResponse response = httpClient.execute(httpPost);
90 + if(response != null){
91 + HttpEntity resEntity = response.getEntity();
92 + if(resEntity != null){
93 + result = EntityUtils.toString(resEntity,HttpUtils.CHARSET);
94 + }
95 + }
96 + }catch(Exception ex){
97 + ex.printStackTrace();
98 + }
99 + return result;
100 + }
101 +
102 + /**
103 + * post请求,包含header
104 + * @param url
105 + * @param paramMap
106 + * @param headerMap
107 + * @return
108 + */
109 + public static String doPost(String url,Map<String,String> paramMap,Map<String,String> headerMap){
110 + HttpClient httpClient = null;
111 + HttpPost httpPost = null;
112 + String result = null;
113 + try{
114 + httpClient = new SSLClient();
115 + httpPost = new HttpPost(url);
116 + //设置参数
117 + List<NameValuePair> list = new ArrayList<NameValuePair>();
118 + Iterator iterator = paramMap.entrySet().iterator();
119 + while(iterator.hasNext()){
120 + Entry<String,String> elem = (Entry<String, String>) iterator.next();
121 + list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
122 + }
123 + if(list.size() > 0){
124 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,HttpUtils.CHARSET);
125 + httpPost.setEntity(entity);
126 + }
127 + Iterator headerIterator = headerMap.entrySet().iterator();
128 + while(headerIterator.hasNext()){
129 + Entry<String,String> elem = (Entry<String, String>) headerIterator.next();
130 + httpPost.addHeader(elem.getKey(),elem.getValue());
131 + }
132 + HttpResponse response = httpClient.execute(httpPost);
133 + if(response != null){
134 + HttpEntity resEntity = response.getEntity();
135 + if(resEntity != null){
136 + result = EntityUtils.toString(resEntity,HttpUtils.CHARSET);
137 + }
138 + }
139 + }catch(Exception ex){
140 + ex.printStackTrace();
141 + }
142 + return result;
143 + }
144 +}
...\ No newline at end of file ...\ No newline at end of file
1 +package com.erry.iclear.dateInterface.util;
2 +
3 +import java.security.cert.CertificateException;
4 +import java.security.cert.X509Certificate;
5 +import javax.net.ssl.SSLContext;
6 +import javax.net.ssl.TrustManager;
7 +import javax.net.ssl.X509TrustManager;
8 +import org.apache.http.conn.ClientConnectionManager;
9 +import org.apache.http.conn.scheme.Scheme;
10 +import org.apache.http.conn.scheme.SchemeRegistry;
11 +import org.apache.http.conn.ssl.SSLSocketFactory;
12 +import org.apache.http.impl.client.DefaultHttpClient;
13 +
14 +/**
15 + * 用于进行Https请求的HttpClient,指定传输协议为TLSv1.2
16 + * mt
17 + * 2022年3月8日11:09:37
18 + */
19 +public class SSLClient extends DefaultHttpClient{
20 + public SSLClient() throws Exception{
21 + super(); //传输协议需要根据自己的判断 
22 + SSLContext ctx = SSLContext.getInstance("TLSv1.2");
23 + X509TrustManager tm = new X509TrustManager() {
24 + @Override
25 + public void checkClientTrusted(X509Certificate[] chain,
26 + String authType) throws CertificateException {
27 + }
28 + @Override
29 + public void checkServerTrusted(X509Certificate[] chain,
30 + String authType) throws CertificateException {
31 + }
32 + @Override
33 + public X509Certificate[] getAcceptedIssuers() {
34 + return null;
35 + }
36 + };
37 + ctx.init(null, new TrustManager[]{tm}, null);
38 + SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
39 + ClientConnectionManager ccm = this.getConnectionManager();
40 + SchemeRegistry sr = ccm.getSchemeRegistry();
41 + sr.register(new Scheme("https", 443, ssf));
42 + }
43 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -20,21 +20,21 @@ task: ...@@ -20,21 +20,21 @@ task:
20 api: 20 api:
21 token-info: 21 token-info:
22 #获取token账号 非prod 22 #获取token账号 非prod
23 - #username: iclear_external_ctm 23 + username: iclear_export_prod
24 #获取token密码 24 #获取token密码
25 - #password: toQu3arch6 25 + password: 1qaz@WSX
26 address: 26 address:
27 #IClear校验运单地址 27 #IClear校验运单地址
28 iclear-check-url: https://www.iclrccd-fedex.com/chmConsignmentController/checkChmAndDetaliD 28 iclear-check-url: https://www.iclrccd-fedex.com/chmConsignmentController/checkChmAndDetaliD
29 #IClearC类提交运单地址 29 #IClearC类提交运单地址
30 - iclear-submit-url: http://www.iclrccd-fedex.com/chmConsignmentController/submitConsignments 30 + iclear-submit-url: https://www.iclrccd-fedex.com/chmConsignmentController/submitConsignments
31 #IClear校验C类运单地址 31 #IClear校验C类运单地址
32 - iclear-check-url-c: http://www.iclrccd-fedex.com/chmConsignmentController/checkChmAndDetaliC 32 + iclear-check-url-c: https://www.iclrccd-fedex.com/chmConsignmentController/checkChmAndDetaliC
33 #获取token地址 33 #获取token地址
34 - #token-url: https://api.iclrccd-fedex.com/export/login 34 + token-url: https://apictm.iclrccd-fedex.com/login
35 #刷新token地址 35 #刷新token地址
36 - #refresh-token-url: https://api.iclrccd-fedex.com/export/admin/user/token/refresh 36 + refresh-token-url: https://apictm.iclrccd-fedex.com/admin/user/token/refresh
37 #传输海关回执地址 37 #传输海关回执地址
38 - #customs-url: https://api.iclrccd-fedex.com/export/outbound/customs 38 + customs-url: https://apictm.iclrccd-fedex.com/export/outbound/customs
39 #推送完整报文地址 39 #推送完整报文地址
40 - #complete-msg-url: https://api.iclrccd-fedex.com/export/export/outbound/mawb
...\ No newline at end of file ...\ No newline at end of file
40 + complete-msg-url: https://apictm.iclrccd-fedex.com/export/outbound/mawb
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -2,13 +2,11 @@ package com.erry.iclear.dateInterface; ...@@ -2,13 +2,11 @@ package com.erry.iclear.dateInterface;
2 2
3 import com.arronlong.httpclientutil.HttpClientUtil; 3 import com.arronlong.httpclientutil.HttpClientUtil;
4 import com.arronlong.httpclientutil.common.HttpConfig; 4 import com.arronlong.httpclientutil.common.HttpConfig;
5 -import com.arronlong.httpclientutil.common.HttpHeader;
6 import com.erry.iclear.dateInterface.api.response.TokenResult; 5 import com.erry.iclear.dateInterface.api.response.TokenResult;
6 +import com.erry.iclear.dateInterface.util.HttpUtils;
7 import com.erry.iclear.dateInterface.util.JsonToJava; 7 import com.erry.iclear.dateInterface.util.JsonToJava;
8 -import org.apache.http.Header;
9 import org.junit.Test; 8 import org.junit.Test;
10 import org.springframework.beans.factory.annotation.Value; 9 import org.springframework.beans.factory.annotation.Value;
11 -
12 import java.util.HashMap; 10 import java.util.HashMap;
13 import java.util.Map; 11 import java.util.Map;
14 12
...@@ -51,4 +49,41 @@ public class TokenTest extends IClearApiApplicationTests { ...@@ -51,4 +49,41 @@ public class TokenTest extends IClearApiApplicationTests {
51 String rs = HttpClientUtil.post(config1); 49 String rs = HttpClientUtil.post(config1);
52 System.out.println(rs); 50 System.out.println(rs);
53 } 51 }
52 +
53 + public static void main(String[] args) throws Exception{
54 +// Map<String,Object> map = new HashMap<String,Object>();
55 +// map.put("username", "iclear_export_prod");
56 +// map.put("password", "1qaz@WSX");
57 +// HttpConfig config = HttpConfig.custom()
58 +// .url("https://apictm.iclrccd-fedex.com/login")
59 +// .map(map)
60 +// .encoding("utf-8");
61 +// String token = HttpClientUtil.post(config);
62 +// System.out.println(token);
63 +
64 + Map<String,String> map = new HashMap<String,String>();
65 + map.put("username", "iclear_export_prod");
66 + map.put("password", "1qaz@WSX");
67 +// HttpConfig config = HttpConfig.custom()
68 +// .url("https://apictm.iclrccd-fedex.com/login")
69 +// .map(map)
70 +// .encoding("utf-8");
71 +// String token = HttpClientUtil.post(config);
72 +//
73 +// String json = JSONObject.toJSONString(map);
74 +
75 +// String str = executePost("https://apictm.iclrccd-fedex.com/login",json,20000);
76 +
77 +
78 +// System.out.println(token);
79 +
80 + String json = "{\"username\":\"iclear_export_prod\",\"password\":\"1qaz@WSX\"}";
81 +
82 + String token = httpUtils.doPost(url,map);
83 + System.out.println(token);
84 + }
85 +
86 + private static String url = "https://apictm.iclrccd-fedex.com/login";
87 + private static String charset = "utf-8";
88 + private static HttpUtils httpUtils = new HttpUtils();
54 } 89 }
......