updated
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package com.mgm.mcrm.common.services;
|
||||
|
||||
import com.mgm.mcrm.common.utils.LogUtil;
|
||||
import org.apache.commons.lang3.Strings;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.util.List;
|
||||
|
||||
public class ConfigurableTrustHandler implements X509TrustManager, HostnameVerifier {
|
||||
private final boolean skipValidation;
|
||||
private final X509TrustManager defaultTrustManager;
|
||||
private final List<String> allowedServers;
|
||||
|
||||
public ConfigurableTrustHandler(boolean skipValidation, X509TrustManager defaultTrustManager, List<String> allowedServers ) {
|
||||
this.skipValidation = skipValidation;
|
||||
this.defaultTrustManager = defaultTrustManager;
|
||||
this.allowedServers = allowedServers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
||||
if (skipValidation) {
|
||||
return new java.security.cert.X509Certificate[0];
|
||||
}else{
|
||||
return defaultTrustManager != null ? defaultTrustManager.getAcceptedIssuers() : new java.security.cert.X509Certificate[0];
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(java.security.cert.X509Certificate[] chain,
|
||||
String authType) throws java.security.cert.CertificateException {
|
||||
if (!skipValidation && defaultTrustManager != null) {
|
||||
defaultTrustManager.checkServerTrusted(chain, authType);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkClientTrusted(java.security.cert.X509Certificate[] chain,
|
||||
String authType) throws java.security.cert.CertificateException {
|
||||
if (!skipValidation && defaultTrustManager != null) {
|
||||
defaultTrustManager.checkClientTrusted(chain, authType);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean verify(String hostname, SSLSession paramSSLSession) {
|
||||
boolean isAllow = skipValidation;
|
||||
if(!skipValidation && allowedServers!=null && !allowedServers.isEmpty()){
|
||||
for(String allowItem:allowedServers) {
|
||||
if(Strings.CI.startsWith(hostname, allowItem)) {
|
||||
isAllow=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!skipValidation && !isAllow) {
|
||||
LogUtil.warn(ConfigurableTrustHandler.class, "API Server of ["+hostname+"] is not allowed");
|
||||
}
|
||||
return isAllow;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.mgm.mcrm.common.services;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.mgm.mcrm.common.dtos.HttpResult;
|
||||
import com.mgm.mcrm.common.dtos.config.ApiGatewaySetting;
|
||||
import com.mgm.mcrm.common.enums.HttpMethods;
|
||||
import com.mgm.mcrm.common.httpclient.HttpClientExecutor;
|
||||
import com.mgm.mcrm.common.httpclient.HttpRequestOptions;
|
||||
@@ -9,12 +10,16 @@ import jakarta.annotation.Resource;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
import org.apache.hc.core5.http.message.BasicHeader;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -35,11 +40,20 @@ public class HttpService {
|
||||
@Qualifier("httpRequestConfig")
|
||||
private RequestConfig requestConfig;
|
||||
|
||||
@Value("${daylight.url}")
|
||||
private String daylightUrl;
|
||||
@Resource
|
||||
private ApiGatewaySetting apiGatewaySetting;
|
||||
|
||||
|
||||
private Header[] supplementDaylightProxySecurity(Header[] headers){
|
||||
List<Header> newHeaders = (headers == null ? new ArrayList<>() : new ArrayList<>(Arrays.asList(headers)));
|
||||
newHeaders.add(new BasicHeader("application-id",apiGatewaySetting.getDaylightAppId()));
|
||||
newHeaders.add(new BasicHeader("Authorization",apiGatewaySetting.getDaylightAppSecret()));
|
||||
return newHeaders.toArray(new Header[0]);
|
||||
}
|
||||
|
||||
public String sslGet(String url, Header[] headers, Map<String, Object> map) throws Exception {
|
||||
|
||||
String daylightUrl=apiGatewaySetting.getDaylightProxyUrl();
|
||||
// todo 在這裏需要把請求頭的信息插入到數據庫裏
|
||||
log.info("======sslGet======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map));
|
||||
// 使用 HttpRequestOptions 配置 GET 请求(带参数)
|
||||
@@ -50,7 +64,7 @@ public class HttpService {
|
||||
.encoding("utf-8")
|
||||
.client(httpClient);
|
||||
|
||||
String result = HttpClientExecutor.get(options, headers);
|
||||
String result = HttpClientExecutor.get(options, supplementDaylightProxySecurity(headers));
|
||||
log.info("====== GET request result:{}", result);
|
||||
|
||||
return result;
|
||||
@@ -65,6 +79,7 @@ public class HttpService {
|
||||
* @throws Exception 異常
|
||||
*/
|
||||
public String sslPostForm(String url, Header[] headers, Map<String, Object> map) throws Exception {
|
||||
String daylightUrl=apiGatewaySetting.getDaylightProxyUrl();
|
||||
log.info("======sslPostForm======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map));
|
||||
// 使用 HttpRequestOptions 配置 POST 请求(form-urlencoded 格式)
|
||||
HttpRequestOptions options = HttpRequestOptions.create()
|
||||
@@ -75,7 +90,7 @@ public class HttpService {
|
||||
.encoding("utf-8")
|
||||
.client(httpClient);
|
||||
|
||||
String result = HttpClientExecutor.post(options, headers);
|
||||
String result = HttpClientExecutor.post(options, supplementDaylightProxySecurity(headers));
|
||||
log.info("====== POST Form request result:{}", result);
|
||||
|
||||
return result;
|
||||
@@ -90,6 +105,7 @@ public class HttpService {
|
||||
* @throws Exception 異常
|
||||
*/
|
||||
public String sslPostJson(String url, Header[] headers, Map<String, Object> map) throws Exception {
|
||||
String daylightUrl=apiGatewaySetting.getDaylightProxyUrl();
|
||||
log.info("======sslPostJson======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map));
|
||||
// 使用 HttpRequestOptions 配置 POST 请求(JSON 格式)
|
||||
HttpRequestOptions options = HttpRequestOptions.create()
|
||||
@@ -100,12 +116,13 @@ public class HttpService {
|
||||
.encoding("utf-8")
|
||||
.client(httpClient);
|
||||
|
||||
String result = HttpClientExecutor.post(options, headers);
|
||||
String result = HttpClientExecutor.post(options, supplementDaylightProxySecurity(headers));
|
||||
log.info("====== POST Json request result:{}", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public String sslPostJsonContent(String url, Header[] headers, String jsonContent) throws Exception {
|
||||
String daylightUrl=apiGatewaySetting.getDaylightProxyUrl();
|
||||
// 使用 HttpRequestOptions 配置 POST 请求(JSON 格式)
|
||||
HttpRequestOptions options = HttpRequestOptions.create()
|
||||
.url(daylightUrl + url)
|
||||
@@ -115,7 +132,7 @@ public class HttpService {
|
||||
.encoding("utf-8")
|
||||
.client(httpClient);
|
||||
|
||||
return HttpClientExecutor.post(options, headers);
|
||||
return HttpClientExecutor.post(options, supplementDaylightProxySecurity(headers));
|
||||
}
|
||||
|
||||
|
||||
@@ -128,6 +145,7 @@ public class HttpService {
|
||||
* @throws Exception 異常
|
||||
*/
|
||||
public String sslPut(String url, Header[] headers, Map<String, Object> map) throws Exception {
|
||||
String daylightUrl=apiGatewaySetting.getDaylightProxyUrl();
|
||||
log.info("======sslPut======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map));
|
||||
HttpRequestOptions options = HttpRequestOptions.create()
|
||||
.url(daylightUrl + url)
|
||||
@@ -136,7 +154,7 @@ public class HttpService {
|
||||
.json(JSON.toJSONString(map))
|
||||
.encoding("utf-8")
|
||||
.client(httpClient);
|
||||
String result = HttpClientExecutor.execute(options, headers);
|
||||
String result = HttpClientExecutor.execute(options, supplementDaylightProxySecurity(headers));
|
||||
log.info("====== PUT request result:{}", result);
|
||||
|
||||
return result;
|
||||
@@ -151,13 +169,14 @@ public class HttpService {
|
||||
* @throws Exception 異常
|
||||
*/
|
||||
public String sslPutDelete(String url, Header[] headers, Map<String, Object> map) throws Exception {
|
||||
String daylightUrl=apiGatewaySetting.getDaylightProxyUrl();
|
||||
log.info("======sslPutDelete======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map));
|
||||
HttpRequestOptions options = HttpRequestOptions.create()
|
||||
.url(daylightUrl + url)
|
||||
.timeout(requestConfig)
|
||||
.method(HttpMethods.DELETE)
|
||||
.client(httpClient);
|
||||
String result = HttpClientExecutor.execute(options, headers);
|
||||
String result = HttpClientExecutor.execute(options, supplementDaylightProxySecurity(headers));
|
||||
System.out.println("DELETE 请求结果:\n" + result);
|
||||
log.info("====== DELETE request result:{}", result);
|
||||
return result;
|
||||
@@ -172,6 +191,7 @@ public class HttpService {
|
||||
* @throws Exception 異常
|
||||
*/
|
||||
public HttpResult sslPostHttpResult(String url, Header[] headers, Map<String,Object> map) throws Exception {
|
||||
String daylightUrl=apiGatewaySetting.getDaylightProxyUrl();
|
||||
log.info("======sslPutDelete======url:{}===========headers:{}", daylightUrl + url,JSON.toJSONString(headers));
|
||||
HttpRequestOptions postConfig = HttpRequestOptions.create()
|
||||
.url(daylightUrl + url)
|
||||
@@ -179,14 +199,14 @@ public class HttpService {
|
||||
.json(JSON.toJSONString(map))
|
||||
.method(HttpMethods.POST)
|
||||
.client(httpClient);
|
||||
HttpResult result = HttpClientExecutor.sendAndPostResp(postConfig, headers);
|
||||
HttpResult result = HttpClientExecutor.sendAndPostResp(postConfig, supplementDaylightProxySecurity(headers));
|
||||
System.out.println("POST 请求结果:\n" + result);
|
||||
log.info("====== POST request result:{}", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public HttpResult sslGetHttpResult(String url, Header[] headers, Map<String, Object> map) throws Exception {
|
||||
|
||||
String daylightUrl=apiGatewaySetting.getDaylightProxyUrl();
|
||||
// todo 在這裏需要把請求頭的信息插入到數據庫裏
|
||||
log.info("======sslGet======url:{}===========headers:{} =============== map:{}", daylightUrl + url,JSON.toJSONString(headers),JSON.toJSONString(map));
|
||||
// 使用 HttpConfig 配置 GET 请求(带参数)
|
||||
@@ -197,7 +217,7 @@ public class HttpService {
|
||||
.encoding("utf-8")
|
||||
.client(httpClient);
|
||||
|
||||
HttpResult result = HttpClientExecutor.sendAndGetResp(getConfig, headers);
|
||||
HttpResult result = HttpClientExecutor.sendAndGetResp(getConfig, supplementDaylightProxySecurity(headers));
|
||||
log.info("====== GET request result:{}", result.getResult());
|
||||
|
||||
Map<String, String> mapResult = JSON.parseObject(result.getResult(), Map.class);
|
||||
|
||||
Reference in New Issue
Block a user