From ffb382ba34aca2dce08ab9e4ef09adc946cf23c8 Mon Sep 17 00:00:00 2001
From: ZY <zc857179121@qq.com>
Date: 星期日, 27 四月 2025 17:26:31 +0800
Subject: [PATCH] 双数据源
---
src/main/java/com/zy/nc/Test.java | 430 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 430 insertions(+), 0 deletions(-)
diff --git a/src/main/java/com/zy/nc/Test.java b/src/main/java/com/zy/nc/Test.java
new file mode 100644
index 0000000..38a96ae
--- /dev/null
+++ b/src/main/java/com/zy/nc/Test.java
@@ -0,0 +1,430 @@
+package com.zy.nc;
+
+
+import com.alibaba.excel.util.StringUtils;
+import com.alibaba.fastjson.JSONObject;
+import com.core.common.Cools;
+import com.zy.nc.util.*;
+
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.nio.charset.Charset;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * 1.浠巖esources/config.properties涓鍙栨祴璇昦pi鐩稿叧鐨勬暟鎹� 2.杩愯绋嬪簭锛屾祴璇曟煡鐪嬫祴璇曠粨鏋�
+ *
+ * @author lizhmf
+ * @date 2019骞�6鏈�20鏃ヤ笂鍗�10:53:11
+ */
+public class Test {
+
+ // app_secret
+ private static String client_secret = null;
+ // 鍏挜
+ private static String pubKey = null;
+ // app_id
+ private static String client_id = null;
+ // ncc鐢ㄦ埛鍚�
+ private static String username = null;
+ private static String usercode = null;
+ // ncc鐢ㄦ埛鍚嶅瘑鐮�
+ private static String pwd = null;
+ // ncc璐﹀
+ private static String busi_center = null;
+ private static String dsname = null;
+ // 鑾峰彇token鏂瑰紡锛歝lient_credentials銆乸assword
+ private static String grant_type = null;
+ // 鏈嶅姟鍣╥p锛歱ort
+ private static String baseUrl = null;
+ // 杩斿洖鍊煎帇缂╁姞瀵嗙骇鍒�
+ private static String secret_level = null;
+ // 璇锋眰鍙傛暟
+ private static String requestBody = null;
+ // openapi璇锋眰璺緞
+ private static String apiUrl = null;
+
+ // 璁块棶api鑾峰彇鍒扮殑access_token
+ public static String token = null;
+ // 閲嶅璋冪敤妫�鏌�
+ public static String repeat_check = null;
+ // 鎺ュ彛璋冪敤涓氬姟鏍囪瘑
+ public static String busi_id = null;
+
+ /**
+ * 鍚姩鍏ュ彛
+ *
+ * @param args
+ */
+ public static void main(String[] args) {
+ try {
+// // 鍒濆鍖栨暟鎹�
+ init();
+// // 璇锋眰token
+ if (token == null) {
+ token = getToken();
+ }
+ System.out.println("getTokenData:" + token);
+ // 娴嬭瘯openapi
+ testApi(token);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * 鑾峰彇access_token
+ *
+ * @return
+ * @throws Exception
+ */
+ private static String getToken() throws Exception {
+ String token = null;
+ if ("password".equals(grant_type)) {
+ // 瀵嗙爜妯″紡
+ token = getTokenByPWD();
+ } else if ("client_credentials".equals(grant_type)) {
+ // 瀹㈡埛绔ā寮�
+ token = getTokenByClient();
+ }
+ return token;
+ }
+
+ /**
+ * 瀹㈡埛绔ā寮忚幏鍙杢oken
+ *
+ * @return
+ * @throws Exception
+ */
+ private static String getTokenByClient() throws Exception {
+ Map<String, String> paramMap = new HashMap<String, String>();
+ // 瀵嗙爜妯″紡璁よ瘉
+ paramMap.put("grant_type", "client_credentials");
+ // 绗笁鏂瑰簲鐢╥d
+ paramMap.put("client_id", client_id);
+ // 绗笁鏂瑰簲鐢╯ecret 鍏挜鍔犲瘑
+ String secret_entryption = Encryption.pubEncrypt(pubKey, client_secret);
+ System.out.println("secret_entryption锛氾細" + secret_entryption);
+ paramMap.put("client_secret", URLEncoder.encode(secret_entryption, "utf-8"));
+ // 璐﹀缂栫爜
+ paramMap.put("biz_center", busi_center);
+ // // TODO 浼犻�掓暟鎹簮鍜宯cc鐧诲綍鐢ㄦ埛
+// paramMap.put("dsname", dsname);
+ paramMap.put("usercode", usercode);
+
+ // 绛惧悕
+ String sign = SHA256Util.getSHA256(client_id + client_secret + pubKey, pubKey);
+ paramMap.put("signature", sign);
+ System.out.println("##gettoken sign::" + sign);
+
+ String url = baseUrl + "/nccloud/opm/accesstoken";
+ String mediaType = "application/x-www-form-urlencoded";
+ String token = doPost(url, paramMap, mediaType, null, "");
+ return token;
+ }
+
+ /**
+ * 瀵嗙爜妯″紡鑾峰彇token
+ *
+ * @return
+ * @throws Exception
+ */
+ @SuppressWarnings("unused")
+ private static String getTokenByPWD() throws Exception {
+ Map<String, String> paramMap = new HashMap<String, String>();
+ // 瀵嗙爜妯″紡璁よ瘉
+ paramMap.put("grant_type", "password");
+ // 绗笁鏂瑰簲鐢╥d
+ paramMap.put("client_id", client_id);
+ // 绗笁鏂瑰簲鐢╯ecret 鍏挜鍔犲瘑
+ paramMap.put("client_secret", URLEncoder.encode(Encryption.pubEncrypt(pubKey, client_secret), "utf-8"));
+// paramMap.put("client_secret", client_secret);
+ // ncc鐢ㄦ埛鍚�
+ paramMap.put("username", username);
+ // 瀵嗙爜 鍏挜鍔犲瘑
+ paramMap.put("password", URLEncoder.encode(Encryption.pubEncrypt(pubKey, pwd), "utf-8"));
+// paramMap.put("password", pwd);
+ // 璐﹀缂栫爜
+ paramMap.put("biz_center", busi_center);
+ // 绛惧悕
+ String sign = SHA256Util.getSHA256(client_id + client_secret + username + pwd + pubKey, pubKey);
+ System.out.println("sign_getToken::" + sign);
+ paramMap.put("signature", sign);
+
+ String url = baseUrl + "/nccloud/opm/accesstoken";
+ String mediaType = "application/x-www-form-urlencoded";
+ String token = doPost(url, paramMap, mediaType, null, "");
+ return token;
+ }
+
+ /**
+ * 璇锋眰openapi
+ *
+ * @param token
+ * @throws Exception
+ */
+ private static void testApi(String token) throws Exception {
+ // token杞璞★紝鑾峰彇api璁块棶鎵�鐢╰oken鍜宻ecret
+ ResultMessageUtil returnData = JSONObject.parseObject(token, ResultMessageUtil.class);
+ Map<String, Object> data = (Map<String, Object>) returnData.getData();
+ String access_token = (String) data.get("access_token");
+ String security_key = (String) data.get("security_key");
+ String refresh_token = (String) data.get("refresh_token");
+ long expire_in = new Double((double) data.get("expires_in")).longValue();
+ long ts = new Double((double) data.get("ts")).longValue();
+ if (ts + expire_in < System.currentTimeMillis()) {
+ token = getToken();
+ returnData = JSONObject.parseObject(token, ResultMessageUtil.class);
+ data = (Map<String, Object>) returnData.getData();
+ access_token = (String) data.get("access_token");
+ security_key = (String) data.get("security_key");
+ refresh_token = (String) data.get("refresh_token");
+ }
+ System.out.println("銆怉CCESS_TOKEN銆�:" + access_token);
+
+ // 璇锋眰璺緞
+ String url = baseUrl + apiUrl;
+ // header 鍙傛暟
+ Map<String, String> headermap = new HashMap<>();
+ headermap.put("access_token", access_token);
+ headermap.put("client_id", client_id);
+
+ StringBuffer sb = new StringBuffer();
+ sb.append(client_id);
+ if (!Cools.isEmpty(requestBody)) {
+ // sb.append(requestBody.replaceAll("\\s*|\t|\r|\n", "").trim());
+ sb.append(requestBody);
+ }
+ sb.append(pubKey);
+ String sign = SHA256Util.getSHA256(sb.toString(), pubKey);
+ headermap.put("signature", sign);
+
+ if (!Cools.isEmpty(busi_id)) {
+ headermap.put("busi_id", busi_id);
+ }
+ if (!Cools.isEmpty(repeat_check)) {
+ headermap.put("repeat_check", repeat_check);
+ }
+// headermap.put("ucg_flag", "y");
+
+ String mediaType = "application/json;charset=utf-8";
+
+ // 琛ㄤ綋鏁版嵁json
+ // 鏍规嵁瀹夊叏绾у埆閫夋嫨鍔犲瘑鎴栧帇缂╄姹傝〃浣撳弬鏁�
+ String json = dealRequestBody(requestBody, security_key, secret_level);
+
+ // 杩斿洖鍊�
+ String result = doPost(url, null, mediaType, headermap, json);
+ String result2 = dealResponseBody(result, security_key, secret_level);
+ System.out.println("銆怰ESULT銆�:" + result);
+ System.out.println("result瑙e瘑:" + result2);
+ }
+
+ /**
+ * 杩斿洖鍊艰繘琛岃繃鍔犲瘑鍜屽帇缂╋紝瀵硅繑鍥炲�艰繘琛岃В鍘嬪拰瑙e瘑
+ *
+ * @param source
+ * @param security_key
+ * @param level
+ * @return
+ * @throws Exception
+ */
+ private static String dealResponseBody(String source, String security_key, String level) throws Exception {
+ String result = null;
+
+ if (StringUtils.isEmpty(level) || SecretConst.LEVEL0.equals(level)) {
+ result = source;
+ } else if (SecretConst.LEVEL1.equals(level)) {
+ result = Decryption.symDecrypt(security_key, source);
+ } else if (SecretConst.LEVEL2.equals(level)) {
+ result = CompressUtil.gzipDecompress(source);
+ } else if (SecretConst.LEVEL3.equals(level)) {
+ result = CompressUtil.gzipDecompress(Decryption.symDecrypt(security_key, source));
+ } else if (SecretConst.LEVEL4.equals(level)) {
+ result = Decryption.symDecrypt(security_key, CompressUtil.gzipDecompress(source));
+ } else {
+ throw new Exception("鏃犳晥鐨勫畨鍏ㄧ瓑绾�");
+ }
+
+ return result;
+ }
+
+ /**
+ * 鍒濆鍖栧弬鏁�
+ */
+ private static void init() {
+ // TODO Auto-generated method stub
+ Properties properties = new Properties();
+
+ String filepath = "config.properties";
+ ClassLoader classloader = Thread.currentThread().getContextClassLoader();
+ InputStream inputStream = classloader.getResourceAsStream(filepath);
+ try {
+ InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
+ properties.load(reader);
+
+ client_secret = new String(properties.getProperty("client_secret").getBytes("utf-8"), "utf-8");
+ client_id = properties.getProperty("client_id");
+ pubKey = properties.getProperty("pubKey");
+ username = properties.getProperty("username");
+ usercode = properties.getProperty("usercode");
+ pwd = properties.getProperty("pwd");
+ busi_center = properties.getProperty("busi_center");
+ dsname = properties.getProperty("dsname");
+ baseUrl = properties.getProperty("baseUrl");
+ requestBody = new String(properties.getProperty("requestBody").getBytes("utf-8"), "utf-8");
+ apiUrl = properties.getProperty("apiUrl");
+ grant_type = properties.getProperty("grant_type");
+ secret_level = properties.getProperty("secret_level");
+ repeat_check = properties.getProperty("repeat_check");
+ busi_id = properties.getProperty("busi_id");
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ // 鏍规嵁瀹夊叏绾у埆璁剧疆锛岃〃浣撴槸鍚﹀姞瀵嗘垨鍘嬬缉
+ private static String dealRequestBody(String source, String security_key, String level) throws Exception {
+ String result = null;
+ if (StringUtils.isEmpty(level) || SecretConst.LEVEL0.equals(level)) {
+ result = source;
+ } else if (SecretConst.LEVEL1.equals(level)) {
+ result = Encryption.symEncrypt(security_key, source);
+ } else if (SecretConst.LEVEL2.equals(level)) {
+ result = CompressUtil.gzipCompress(source);
+ } else if (SecretConst.LEVEL3.equals(level)) {
+ result = Encryption.symEncrypt(security_key, CompressUtil.gzipCompress(source));
+ } else if (SecretConst.LEVEL4.equals(level)) {
+ result = CompressUtil.gzipCompress(Encryption.symEncrypt(security_key, source));
+ } else {
+ throw new Exception("鏃犳晥鐨勫畨鍏ㄧ瓑绾�");
+ }
+
+ return result;
+ }
+
+ /**
+ * 鍙戦�乸ost璇锋眰
+ *
+ * @param baseUrl
+ * @param paramMap
+ * @param mediaType
+ * @param headers
+ * @param json
+ * @return
+ */
+ private static String doPost(String baseUrl, Map<String, String> paramMap, String mediaType,
+ Map<String, String> headers, String json) throws Exception {
+
+ HttpURLConnection urlConnection = null;
+ InputStream in = null;
+ OutputStream out = null;
+ BufferedReader bufferedReader = null;
+ String result = null;
+ try {
+ StringBuffer sb = new StringBuffer();
+ sb.append(baseUrl);
+ if (paramMap != null) {
+ sb.append("?");
+ for (Map.Entry<String, String> entry : paramMap.entrySet()) {
+ String key = entry.getKey();
+ String value = entry.getValue();
+ sb.append(key + "=" + value).append("&");
+ }
+ baseUrl = sb.toString().substring(0, sb.toString().length() - 1);
+ }
+
+ URL urlObj = new URL(baseUrl);
+ urlConnection = (HttpURLConnection) urlObj.openConnection();
+ urlConnection.setConnectTimeout(50000);
+ urlConnection.setRequestMethod("POST");
+ urlConnection.setDoOutput(true);
+ urlConnection.setDoInput(true);
+ urlConnection.setUseCaches(false);
+ urlConnection.addRequestProperty("content-type", mediaType);
+ if (headers != null) {
+ for (String key : headers.keySet()) {
+ urlConnection.addRequestProperty(key, headers.get(key));
+ }
+ }
+ out = urlConnection.getOutputStream();
+ out.write(json.getBytes("utf-8"));
+ out.flush();
+ int resCode = urlConnection.getResponseCode();
+ System.out.println("鐘舵�佺爜锛氾細" + resCode);
+// if (resCode == HttpURLConnection.HTTP_OK || resCode == HttpURLConnection.HTTP_CREATED || resCode == HttpURLConnection.HTTP_ACCEPTED) {
+ in = urlConnection.getInputStream();
+// } else {
+// in = urlConnection.getErrorStream();
+// }
+ bufferedReader = new BufferedReader(new InputStreamReader(in, "utf-8"));
+ StringBuffer temp = new StringBuffer();
+ String line = bufferedReader.readLine();
+ while (line != null) {
+ temp.append(line).append("\r\n");
+ line = bufferedReader.readLine();
+ }
+ String ecod = urlConnection.getContentEncoding();
+ if (ecod == null) {
+ ecod = Charset.forName("utf-8").name();
+ }
+ result = new String(temp.toString().getBytes("utf-8"), ecod);
+ System.out.println(result);
+ } catch (Exception e) {
+ System.out.println(e);
+ throw e;
+ } finally {
+ if (null != bufferedReader) {
+ try {
+ bufferedReader.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (null != out) {
+ try {
+ out.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (null != in) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ urlConnection.disconnect();
+ }
+ return result;
+ }
+
+ class SecretConst {
+ /**
+ * LEVEL0 涓嶅帇缂┿�佷笉鍔犲瘑
+ */
+ public static final String LEVEL0 = "L0";
+ /**
+ * LEVEL1 鍙姞瀵嗐�佷笉鍘嬬缉
+ */
+ public static final String LEVEL1 = "L1";
+ /**
+ * LEVEL2 鍙帇缂┿�佷笉鍔犲瘑
+ */
+ public static final String LEVEL2 = "L2";
+ /**
+ * LEVEL3 鍏堝帇缂┿�佸悗鍔犲瘑
+ */
+ public static final String LEVEL3 = "L3";
+ /**
+ * LEVEL4 鍏堝姞瀵嗐�佸悗鍘嬬缉
+ */
+ public static final String LEVEL4 = "L4";
+ }
+}
--
Gitblit v1.9.1