tao.mo

指定传输协议SSLv1.2

mt
2022年3月8日13:24:00
......@@ -97,6 +97,18 @@
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.8</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
</dependencies>
<profiles>
......
package com.erry.iclear.dateInterface.service;
import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.common.HttpHeader;
import com.erry.iclear.dateInterface.api.request.Mawb;
import com.erry.iclear.dateInterface.api.request.MawbDetail;
import com.erry.iclear.dateInterface.api.request.ReceiptMessage;
......@@ -16,15 +13,31 @@ import com.erry.iclear.dateInterface.repository.DeclearTaskRepository;
import com.erry.iclear.dateInterface.repository.HsCodeRepository;
import com.erry.iclear.dateInterface.repository.ReceiptMessageRepository;
import com.erry.iclear.dateInterface.util.DateUtils;
import com.erry.iclear.dateInterface.util.HttpUtils;
import com.erry.iclear.dateInterface.util.JsonToJava;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.net.ssl.SSLContext;
import javax.transaction.Transactional;
import java.io.IOException;
import java.math.BigDecimal;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
/**
......@@ -73,14 +86,10 @@ public class ApiService {
* @throws Exception
*/
public TokenResult getToken() throws Exception{
Map<String,Object> map = new HashMap<String,Object>();
Map<String,String> map = new HashMap<String,String>();
map.put("username", this.username);
map.put("password", this.password);
HttpConfig config = HttpConfig.custom()
.url(this.tokenUrl)
.map(map)
.encoding("utf-8");
String tokenJson = HttpClientUtil.post(config);
String tokenJson = HttpUtils.doPost(this.tokenUrl,map);
TokenResult tokenResult = JsonToJava.parseJson(tokenJson,TokenResult.class);
return tokenResult;
}
......@@ -91,13 +100,9 @@ public class ApiService {
* @throws Exception
*/
public void refreshToken(String token) throws Exception{
Map<String,Object> map = new HashMap<String,Object>();
Map<String,String> map = new HashMap<String,String>();
map.put("token", token);
HttpConfig config = HttpConfig.custom()
.url(this.refreshTokenUrl)
.map(map)
.encoding("utf-8");
HttpClientUtil.post(config);
HttpUtils.doPost(this.refreshTokenUrl,map);
}
/**
......@@ -124,10 +129,7 @@ public class ApiService {
* @param receiptMessageList
*/
public void sendCustoms(String token ,List<ReceiptMessage> receiptMessageList){
Header[] headers = HttpHeader.custom()
.other("Content-Type", "application/json")
.other("Authorization", token)
.build();
Map<String,String> headerMap = this.createHeaderMap(token);
List<DeclearTask> declearTaskList = new ArrayList<DeclearTask>();
receiptMessageList.forEach(d->{
DeclearTask declearTask = new DeclearTask();
......@@ -138,12 +140,7 @@ public class ApiService {
declearTask.setPushTime(new Date());
String objJson = JsonToJava.toJson(d);
//推送海关回执数据
HttpConfig config = HttpConfig.custom()
.headers(headers)
.url(this.customsUrl)
.encoding("utf-8")
.json(objJson);
String json = HttpClientUtil.post(config);
String json = HttpUtils.doPost(this.customsUrl,objJson,headerMap);
//接口响应时间
declearTask.setResponseTime(new Date());
TokenResult tokenResult = JsonToJava.parseJson(json,TokenResult.class);
......@@ -228,10 +225,7 @@ public class ApiService {
* @param mawbList
*/
public void sendMawb(String token ,List<Mawb> mawbList){
Header[] headers = HttpHeader.custom()
.other("Content-Type", "application/json")
.other("Authorization", token)
.build();
Map<String,String> headerMap = this.createHeaderMap(token);
List<DeclearTask> declearTaskList = new ArrayList<DeclearTask>();
mawbList.forEach(d->{
DeclearTask declearTask = new DeclearTask();
......@@ -242,12 +236,7 @@ public class ApiService {
declearTask.setPushTime(new Date());
String objJson = JsonToJava.toJson(d);
//推送海关回执数据
HttpConfig config = HttpConfig.custom()
.headers(headers)
.url(this.completeMsgUrl)
.encoding("utf-8")
.json(objJson);
String json = HttpClientUtil.post(config);
String json = HttpUtils.doPost(this.customsUrl,objJson,headerMap);
//接口响应时间
declearTask.setResponseTime(new Date());
TokenResult tokenResult = JsonToJava.parseJson(json,TokenResult.class);
......@@ -317,4 +306,16 @@ public class ApiService {
}
pushMawbAcc(token,declearTaskList);
}
/**
* 获取headerMap
* @param token
* @return
*/
private Map<String,String> createHeaderMap(String token){
Map<String,String> headerMap = new HashMap<String,String>();
headerMap.put("Content-Type", "application/json");
headerMap.put("Authorization", token);
return headerMap;
}
}
......
package com.erry.iclear.dateInterface.util;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* 利用HttpClient进行post请求的工具类
* mt
* 2022年3月8日11:09:58
*/
public class HttpUtils {
private static String CHARSET = "utf-8";
/**
* post请求
* @param url
* @param json
* @return
*/
public static String doPost(String url,String json,Map<String,String> headerMap){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//设置参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
StringEntity entity = new StringEntity(json, HttpUtils.CHARSET);//解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
Iterator headerIterator = headerMap.entrySet().iterator();
while(headerIterator.hasNext()){
Entry<String,String> elem = (Entry<String, String>) headerIterator.next();
httpPost.addHeader(elem.getKey(),elem.getValue());
}
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,HttpUtils.CHARSET);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
/**
* post请求 map参数
* @param url
* @param paramMap
* @return
*/
public static String doPost(String url,Map<String,String> paramMap){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//设置参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
Iterator iterator = paramMap.entrySet().iterator();
while(iterator.hasNext()){
Entry<String,String> elem = (Entry<String, String>) iterator.next();
list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
}
if(list.size() > 0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,HttpUtils.CHARSET);
httpPost.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,HttpUtils.CHARSET);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
/**
* post请求,包含header
* @param url
* @param paramMap
* @param headerMap
* @return
*/
public static String doPost(String url,Map<String,String> paramMap,Map<String,String> headerMap){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//设置参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
Iterator iterator = paramMap.entrySet().iterator();
while(iterator.hasNext()){
Entry<String,String> elem = (Entry<String, String>) iterator.next();
list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
}
if(list.size() > 0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,HttpUtils.CHARSET);
httpPost.setEntity(entity);
}
Iterator headerIterator = headerMap.entrySet().iterator();
while(headerIterator.hasNext()){
Entry<String,String> elem = (Entry<String, String>) headerIterator.next();
httpPost.addHeader(elem.getKey(),elem.getValue());
}
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,HttpUtils.CHARSET);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}
\ No newline at end of file
package com.erry.iclear.dateInterface.util;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* 用于进行Https请求的HttpClient,指定传输协议为TLSv1.2
* mt
* 2022年3月8日11:09:37
*/
public class SSLClient extends DefaultHttpClient{
public SSLClient() throws Exception{
super(); //传输协议需要根据自己的判断 
SSLContext ctx = SSLContext.getInstance("TLSv1.2");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
\ No newline at end of file
......@@ -20,21 +20,21 @@ task:
api:
token-info:
#获取token账号 非prod
#username: iclear_external_ctm
username: iclear_export_prod
#获取token密码
#password: toQu3arch6
password: 1qaz@WSX
address:
#IClear校验运单地址
iclear-check-url: https://www.iclrccd-fedex.com/chmConsignmentController/checkChmAndDetaliD
#IClearC类提交运单地址
iclear-submit-url: http://www.iclrccd-fedex.com/chmConsignmentController/submitConsignments
iclear-submit-url: https://www.iclrccd-fedex.com/chmConsignmentController/submitConsignments
#IClear校验C类运单地址
iclear-check-url-c: http://www.iclrccd-fedex.com/chmConsignmentController/checkChmAndDetaliC
iclear-check-url-c: https://www.iclrccd-fedex.com/chmConsignmentController/checkChmAndDetaliC
#获取token地址
#token-url: https://api.iclrccd-fedex.com/export/login
token-url: https://apictm.iclrccd-fedex.com/login
#刷新token地址
#refresh-token-url: https://api.iclrccd-fedex.com/export/admin/user/token/refresh
refresh-token-url: https://apictm.iclrccd-fedex.com/admin/user/token/refresh
#传输海关回执地址
#customs-url: https://api.iclrccd-fedex.com/export/outbound/customs
customs-url: https://apictm.iclrccd-fedex.com/export/outbound/customs
#推送完整报文地址
#complete-msg-url: https://api.iclrccd-fedex.com/export/export/outbound/mawb
\ No newline at end of file
complete-msg-url: https://apictm.iclrccd-fedex.com/export/outbound/mawb
\ No newline at end of file
......
......@@ -2,13 +2,11 @@ package com.erry.iclear.dateInterface;
import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.common.HttpHeader;
import com.erry.iclear.dateInterface.api.response.TokenResult;
import com.erry.iclear.dateInterface.util.HttpUtils;
import com.erry.iclear.dateInterface.util.JsonToJava;
import org.apache.http.Header;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import java.util.HashMap;
import java.util.Map;
......@@ -51,4 +49,41 @@ public class TokenTest extends IClearApiApplicationTests {
String rs = HttpClientUtil.post(config1);
System.out.println(rs);
}
public static void main(String[] args) throws Exception{
// Map<String,Object> map = new HashMap<String,Object>();
// map.put("username", "iclear_export_prod");
// map.put("password", "1qaz@WSX");
// HttpConfig config = HttpConfig.custom()
// .url("https://apictm.iclrccd-fedex.com/login")
// .map(map)
// .encoding("utf-8");
// String token = HttpClientUtil.post(config);
// System.out.println(token);
Map<String,String> map = new HashMap<String,String>();
map.put("username", "iclear_export_prod");
map.put("password", "1qaz@WSX");
// HttpConfig config = HttpConfig.custom()
// .url("https://apictm.iclrccd-fedex.com/login")
// .map(map)
// .encoding("utf-8");
// String token = HttpClientUtil.post(config);
//
// String json = JSONObject.toJSONString(map);
// String str = executePost("https://apictm.iclrccd-fedex.com/login",json,20000);
// System.out.println(token);
String json = "{\"username\":\"iclear_export_prod\",\"password\":\"1qaz@WSX\"}";
String token = httpUtils.doPost(url,map);
System.out.println(token);
}
private static String url = "https://apictm.iclrccd-fedex.com/login";
private static String charset = "utf-8";
private static HttpUtils httpUtils = new HttpUtils();
}
......