修改人:邹江涛

修改内容:尝试调用付款处理接口1.1
修改时间:2024/11/14
This commit is contained in:
zoujiangtao 2024-11-14 16:26:08 +08:00
parent 268063abab
commit be0769c064
1 changed files with 79 additions and 43 deletions

View File

@ -3,32 +3,20 @@ package shkd.sys.sys.mservice;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.logging.Log; import kd.bos.logging.Log;
import kd.bos.logging.LogFactory; import kd.bos.logging.LogFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import shkd.sys.sys.common.ApiEntity; import shkd.sys.sys.common.ApiEntity;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac; import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import java.util.Base64; import java.util.Base64;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -45,41 +33,89 @@ public class BIPService {
String access_token = null; String access_token = null;
long currentTimeMillis = System.currentTimeMillis(); long currentTimeMillis = System.currentTimeMillis();
try { try {
RestTemplate restTemplate = new RestTemplate(); // 禁用 SSL 证书验证
HttpHeaders httpHeaders = new HttpHeaders(); disableSSLCertificateChecking();
HttpEntity<Object> objectHttpEntity = new HttpEntity<>(httpHeaders); // RestTemplate restTemplate = new RestTemplate();
String signature = generateSignature("appKey22564a240d3140d0b15582aca71a748ctimestamp" + currentTimeMillis); String appKey = "22564a240d3140d0b15582aca71a748c";
logger.info("getBIPToken\ntimestamp{}\nsignature{}", currentTimeMillis, signature); String timestamp = String.valueOf(currentTimeMillis);
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://biptest.ctny.com.cn/iuap-api-auth/open-auth/selfAppAuth/getAccessToken") String signature = generateSignature("appKey" + appKey + "timestamp" + timestamp);
.queryParam("appKey", "22564a240d3140d0b15582aca71a748c")
.queryParam("timestamp", currentTimeMillis) // 构建URL
.queryParam("signature", signature); String urlString = "https://biptest.ctny.com.cn/iuap-api-auth/open-auth/selfAppAuth/getAccessToken" +
ResponseEntity<String> exchange = restTemplate.exchange(builder.build().toString(), HttpMethod.GET, objectHttpEntity, String.class); "?appKey=22564a240d3140d0b15582aca71a748c" +
JSONObject jsonObject = JSON.parseObject(exchange.getBody()); "&timestamp=" + currentTimeMillis +
if ("00000".equals(jsonObject.getString("code"))){ "&signature=" + signature;
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
// 读取响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 解析响应
JSONObject jsonObject = JSON.parseObject(response.toString());
JSONObject data = jsonObject.getJSONObject("data"); JSONObject data = jsonObject.getJSONObject("data");
access_token = data.getString("access_token"); access_token = data.getString("access_token");
logger.info("getBIPToken → access_token{}", access_token); logger.info("getBIPToken → 接口调用成功access_token: {}", access_token);
return access_token; } else {
logger.error("getBIPToken → 接口调用失败,状态码: {}", responseCode);
} }
} catch (Exception e) { } catch (Exception e) {
logger.info("报错信息 error" + e); logger.error("getBIPToken → 调用接口报错: {}", e.getMessage());
} }
return access_token; return access_token;
} }
private static RestTemplate createRestTemplateIgnoringSsl() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { public static SSLContext createInsecureSSLContext() throws Exception {
SSLContext sslContext = new SSLContextBuilder() // 创建一个信任所有证书的 TrustManager
.loadTrustMaterial(null, (chain, authType) -> true) TrustManager[] trustAllCerts = new TrustManager[]{
.build(); new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); public void checkClientTrusted(X509Certificate[] certs, String authType) {
CloseableHttpClient httpClient = HttpClients.custom() }
.setSSLSocketFactory(socketFactory)
.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); public void checkServerTrusted(X509Certificate[] certs, String authType) {
return new RestTemplate(factory); }
}
};
// 安装信任所有证书的 TrustManager
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
return sslContext;
}
public static void disableSSLCertificateChecking() {
try {
// 创建一个信任所有证书的 SSLContext
SSLContext sslContext = createInsecureSSLContext();
// 设置 HttpsURLConnection 使用这个 SSLContext
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
// 创建一个 HostnameVerifier信任所有主机名
HostnameVerifier allHostsValid = (hostname, session) -> true;
// 安装这个 HostnameVerifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
} catch (Exception e) {
e.printStackTrace();
}
} }
private static String generateSignature(String toSign) throws Exception { private static String generateSignature(String toSign) throws Exception {