From 1b5c0044b4178c7314781ac36f897b781de72064 Mon Sep 17 00:00:00 2001 From: ZY <zc857179121@qq.com> Date: 星期日, 27 四月 2025 12:50:30 +0800 Subject: [PATCH] 1 --- src/main/java/com/zy/nc/util/Base64Util.java | 31 + src/main/java/com/zy/nc/util/KeysFactory.java | 103 ++++ src/main/resources/config.properties | 59 ++ src/main/resources/lib/caffeine-2.8.8.jar | 0 src/main/java/com/zy/nc/vo/SaleOutHeadVO.java | 4 src/main/java/com/zy/nc/util/CipherConstant.java | 12 src/main/java/com/zy/nc/util/CompressUtil.java | 160 +++++++ src/main/java/com/zy/nc/util/SecureRandomProxy.java | 26 + src/main/java/com/zy/nc/Test.java | 430 ++++++++++++++++++++ src/main/java/com/zy/nc/util/Decryption.java | 103 ++++ src/main/java/com/zy/nc/util/Encryption.java | 100 ++++ src/main/java/com/zy/nc/util/KeyPairs.java | 27 + src/main/java/com/zy/nc/util/SHA256Util.java | 52 ++ src/main/java/com/zy/nc/vo/SaleOutBodyVO.java | 4 src/main/resources/application.yml | 60 ++ src/main/java/com/zy/nc/util/ResultMessageUtil.java | 45 ++ 16 files changed, 1,214 insertions(+), 2 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"; + } +} diff --git a/src/main/java/com/zy/nc/util/Base64Util.java b/src/main/java/com/zy/nc/util/Base64Util.java new file mode 100644 index 0000000..0050efb --- /dev/null +++ b/src/main/java/com/zy/nc/util/Base64Util.java @@ -0,0 +1,31 @@ +package com.zy.nc.util; + +import sun.misc.BASE64Decoder; +import sun.misc.BASE64Encoder; + +import java.io.IOException; + +/** + * Base64锟斤拷锟斤拷锟斤拷 + * + * @author liyang + */ +public class Base64Util { + + /** + * Base64锟斤拷锟斤拷 + */ + public static String encryptBASE64(byte[] key) { + return (new BASE64Encoder()).encodeBuffer(key); + } + + /** + * Base64锟斤拷锟斤拷 + * + * @throws IOException + */ + public static byte[] decryptBASE64(String key) throws IOException { + return (new BASE64Decoder()).decodeBuffer(key); + } + +} diff --git a/src/main/java/com/zy/nc/util/CipherConstant.java b/src/main/java/com/zy/nc/util/CipherConstant.java new file mode 100644 index 0000000..24613c5 --- /dev/null +++ b/src/main/java/com/zy/nc/util/CipherConstant.java @@ -0,0 +1,12 @@ +package com.zy.nc.util; + +public class CipherConstant { + // 锟斤拷锟斤拷锟姐法锟斤拷锟斤拷AES锟皆称硷拷锟斤拷 + public static final String AES = "AES"; + // public static final String AES_ALGORITHM = "AES"; + public static final String AES_ALGORITHM = "AES/CTR/NoPadding"; + // 锟斤拷锟斤拷锟姐法锟斤拷锟斤拷RSA锟角对称硷拷锟斤拷 + public static final String RSA = "RSA"; + // public static final String RSA_ALGORITHM = "RSA"; + public static final String RSA_ALGORITHM = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding"; +} diff --git a/src/main/java/com/zy/nc/util/CompressUtil.java b/src/main/java/com/zy/nc/util/CompressUtil.java new file mode 100644 index 0000000..3a762cc --- /dev/null +++ b/src/main/java/com/zy/nc/util/CompressUtil.java @@ -0,0 +1,160 @@ +package com.zy.nc.util; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.zip.*; + +/** + * 压锟斤拷锟斤拷锟斤拷锟斤拷 + * + * @author liyang + */ +public class CompressUtil { + + private static int buffSize = 1024; + + /** + * deflaterCompress 默锟斤拷压锟斤拷 + * + * @param source 原锟斤拷 + * @return + * @throws IOException + * @throws Exception + */ + public static String deflaterCompress(String source) throws Exception { + String value = null; + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + Deflater compressor = new Deflater(); + + try { + + byte[] input = source.getBytes(StandardCharsets.UTF_8); + // 锟斤拷锟斤拷压锟斤拷锟角硷拷 + compressor.setLevel(Deflater.DEFAULT_COMPRESSION); + compressor.setInput(input); + compressor.finish(); + final byte[] buf = new byte[buffSize]; + + while (!compressor.finished()) { + int count = compressor.deflate(buf); + bos.write(buf, 0, count); + } + value = Base64Util.encryptBASE64(bos.toByteArray()); + + } finally { + bos.close(); + compressor.end(); + } + + return value; + } + + /** + * deflaterDecompress 默锟较斤拷压 + * + * @param source 压锟斤拷锟斤拷锟侥憋拷 + * @return + * @throws IOException + * @throws @throws Exception + */ + public static String deflaterDecompress(String source) throws Exception { + String value = null; + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + Inflater decompressor = new Inflater(); + + try { + byte[] input = Base64Util.decryptBASE64(source); + + decompressor.setInput(input); + final byte[] buf = new byte[buffSize]; + + while (!decompressor.finished()) { + int count = decompressor.inflate(buf); + bos.write(buf, 0, count); + } + + value = new String(bos.toByteArray(), StandardCharsets.UTF_8); + } catch (IOException | DataFormatException e) { + throw new Exception("锟斤拷压锟届常 " + e.getMessage()); + } finally { + bos.close(); + decompressor.end(); + } + return value; + } + + /** + * gzipCompress 锟斤拷锟斤拷gzip压锟斤拷 + * + * @param source 原锟斤拷 + * @return + * @throws IOException + * @throws Exception + */ + public static String gzipCompress(String source) throws Exception { + String value = null; + + ByteArrayOutputStream out = null; + + try { + out = new ByteArrayOutputStream(); + GZIPOutputStream gzip = new GZIPOutputStream(out); + byte[] input = source.getBytes(StandardCharsets.UTF_8); + gzip.write(input); + gzip.close(); + value = Base64Util.encryptBASE64(out.toByteArray()); + } catch (IOException e) { + throw new Exception("压锟斤拷锟届常 " + e.getMessage()); + } finally { + if (out != null) { + out.close(); + } + } + + return value; + } + + /** + * gzipDecompress gzip锟斤拷压 + * + * @param source 压锟斤拷锟斤拷锟侥憋拷 + * @return + * @throws Exception + */ + public static String gzipDecompress(String source) throws Exception { + String value = null; + + ByteArrayOutputStream out = null; + ByteArrayInputStream in = null; + + try { + byte[] input = Base64Util.decryptBASE64(source); + out = new ByteArrayOutputStream(); + in = new ByteArrayInputStream(input); + GZIPInputStream ungzip = new GZIPInputStream(in); + + byte[] buffer = new byte[buffSize]; + int n; + while ((n = ungzip.read(buffer)) >= 0) { + out.write(buffer, 0, n); + } + ungzip.close(); + value = new String(out.toByteArray(), StandardCharsets.UTF_8); + } catch (IOException e) { + throw new Exception("压锟斤拷锟届常 " + e.getMessage()); + } finally { + if (in != null) { + in.close(); + } + if (out != null) { + out.close(); + } + + } + return value; + } +} diff --git a/src/main/java/com/zy/nc/util/Decryption.java b/src/main/java/com/zy/nc/util/Decryption.java new file mode 100644 index 0000000..2046a10 --- /dev/null +++ b/src/main/java/com/zy/nc/util/Decryption.java @@ -0,0 +1,103 @@ +package com.zy.nc.util; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.OAEPParameterSpec; +import javax.crypto.spec.PSource; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.Key; +import java.security.NoSuchAlgorithmException; +import java.security.spec.MGF1ParameterSpec; + +/** + * 锟斤拷锟斤拷锟斤拷 + * + * @author liyang + */ +public class Decryption { + + // RSA锟斤拷锟斤拷锟斤拷锟斤拷锟侥达拷小 + private static final int MAX_DECRYPT_BLOCK = 256; + + /** + * symDecrypt 锟皆称斤拷锟斤拷 + * + * @param strkey 锟皆筹拷锟斤拷钥 + * @param src 锟斤拷锟斤拷 + * @return 原锟斤拷 + * @throws IOException + * @throws Exception + */ + public static String symDecrypt(String strkey, String src) throws Exception { + + String target = null; + try { + Key key = KeysFactory.getSymKey(strkey); + // 锟斤拷锟斤拷 + Cipher cipher = Cipher.getInstance(CipherConstant.AES_ALGORITHM); + IvParameterSpec iv = new IvParameterSpec(strkey.substring(0, 16).getBytes()); + cipher.init(Cipher.DECRYPT_MODE, key, iv); + byte[] decodeResult = cipher.doFinal(Base64Util.decryptBASE64(src)); + target = new String(decodeResult, StandardCharsets.UTF_8); + + } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | + InvalidKeyException e) { + throw new Exception("锟斤拷锟斤拷失锟斤拷" + e.getMessage()); + } + + return target; + } + + /** + * priDecrypt 私钥锟斤拷锟斤拷 + * + * @param priKey 私钥 + * @param src 锟斤拷锟斤拷 + * @return 原锟斤拷 + * @throws IOException + * @throws Exception + */ + public static String priDecrypt(String priKey, String src) throws Exception { + String target = null; + ByteArrayOutputStream out = null; + try { + Key key = KeysFactory.getPrivateKey(priKey); + // 锟斤拷锟斤拷 + Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); + cipher.init(Cipher.DECRYPT_MODE, key, new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), PSource.PSpecified.DEFAULT)); + byte[] data = Base64Util.decryptBASE64(src); + int inputLen = data.length; + out = new ByteArrayOutputStream(); + int offSet = 0; + byte[] cache; + int i = 0; + // 锟斤拷锟斤拷锟捷分段斤拷锟斤拷 + while (inputLen - offSet > 0) { + if (inputLen - offSet > MAX_DECRYPT_BLOCK) { + cache = cipher.doFinal(data, offSet, MAX_DECRYPT_BLOCK); + } else { + cache = cipher.doFinal(data, offSet, inputLen - offSet); + } + out.write(cache, 0, cache.length); + i++; + offSet = i * MAX_DECRYPT_BLOCK; + } + target = new String(out.toByteArray()); + + } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | + BadPaddingException e) { + throw new Exception("锟斤拷锟斤拷失锟斤拷" + e.getMessage()); + } finally { + if (out != null) { + out.close(); + } + } + return target; + } +} diff --git a/src/main/java/com/zy/nc/util/Encryption.java b/src/main/java/com/zy/nc/util/Encryption.java new file mode 100644 index 0000000..08880e3 --- /dev/null +++ b/src/main/java/com/zy/nc/util/Encryption.java @@ -0,0 +1,100 @@ +package com.zy.nc.util; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.OAEPParameterSpec; +import javax.crypto.spec.PSource; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.Key; +import java.security.NoSuchAlgorithmException; +import java.security.spec.MGF1ParameterSpec; + +/** + * 锟斤拷锟斤拷锟斤拷 + * + * @author liyang + */ +public class Encryption { + + // RSA锟斤拷锟斤拷锟斤拷锟斤拷锟侥达拷小 + private static final int MAX_ENCRYPT_BLOCK = 117; + + /** + * symEncrypt 锟皆称硷拷锟斤拷 + * + * @param strkey 锟皆筹拷锟斤拷钥 + * @param src 原锟斤拷 + * @return 锟斤拷锟斤拷 + */ + public static String symEncrypt(String strkey, String src) throws Exception { + String target = null; + try { + Key key = KeysFactory.getSymKey(strkey); + //锟斤拷锟斤拷 + Cipher cipher = Cipher.getInstance(CipherConstant.AES_ALGORITHM); + IvParameterSpec iv = new IvParameterSpec(strkey.substring(0, 16).getBytes()); + cipher.init(Cipher.ENCRYPT_MODE, key, iv); + byte[] encodeResult = cipher.doFinal(src.getBytes(StandardCharsets.UTF_8)); + target = Base64Util.encryptBASE64(encodeResult); + } catch (NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedEncodingException | + InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) { + throw new Exception("锟斤拷锟斤拷失锟斤拷" + e.getMessage()); + } + return target; + } + + /** + * pubEncrypt 锟斤拷钥锟斤拷锟斤拷 + * + * @param pubKey 锟斤拷钥 + * @param src 原锟斤拷 + * @return 锟斤拷锟斤拷 + * @throws IOException + * @throws Exception + */ + public static String pubEncrypt(String pubKey, String src) throws Exception { + String target = null; + ByteArrayOutputStream out = null; + try { + Key key = KeysFactory.getPublicKey(pubKey); + + Cipher cipher = Cipher.getInstance(CipherConstant.RSA_ALGORITHM); + cipher.init(Cipher.ENCRYPT_MODE, key, new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), PSource.PSpecified.DEFAULT)); + byte[] data = src.getBytes(); + int inputLen = data.length; + out = new ByteArrayOutputStream(); + int offSet = 0; + byte[] cache; + int i = 0; + // 锟斤拷锟斤拷锟捷分段硷拷锟斤拷 + while (inputLen - offSet > 0) { + if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { + cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); + } else { + cache = cipher.doFinal(data, offSet, inputLen - offSet); + } + out.write(cache, 0, cache.length); + i++; + offSet = i * MAX_ENCRYPT_BLOCK; + } + + target = Base64Util.encryptBASE64(out.toByteArray()); + } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | + BadPaddingException e) { + throw new Exception("锟斤拷锟斤拷失锟斤拷" + e.getMessage()); + } finally { + if (out != null) { + out.close(); + } + } + return target; + } + +} diff --git a/src/main/java/com/zy/nc/util/KeyPairs.java b/src/main/java/com/zy/nc/util/KeyPairs.java new file mode 100644 index 0000000..e37edfd --- /dev/null +++ b/src/main/java/com/zy/nc/util/KeyPairs.java @@ -0,0 +1,27 @@ +package com.zy.nc.util; + +import java.security.KeyPair; + +/** + * KeyPairs + * 锟斤拷锟斤拷锟斤拷 + * + * @author liyang + * @date: 2019-5-20锟斤拷锟斤拷4:34:51 + */ +public class KeyPairs { + + private KeyPair keyPair; + + public KeyPairs(KeyPair keyPair) { + this.keyPair = keyPair; + } + + public String getPublicKey() { + return Base64Util.encryptBASE64(keyPair.getPublic().getEncoded()); + } + + public String getPrivateKey() { + return Base64Util.encryptBASE64(keyPair.getPrivate().getEncoded()); + } +} diff --git a/src/main/java/com/zy/nc/util/KeysFactory.java b/src/main/java/com/zy/nc/util/KeysFactory.java new file mode 100644 index 0000000..67c4d66 --- /dev/null +++ b/src/main/java/com/zy/nc/util/KeysFactory.java @@ -0,0 +1,103 @@ +package com.zy.nc.util; + +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; +import java.security.Key; +import java.security.KeyFactory; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; + +/** + * Key锟斤拷锟斤拷锟斤拷 + * + * @author liyang + */ +public class KeysFactory { + + /** + * buildAsymKey 锟斤拷锟斤拷一锟斤拷嵌猿锟斤拷锟皆� + * + * @return KeyPair key锟斤拷PublicKey锟斤拷PrivateKey + * @throws NoSuchAlgorithmException + */ + public static KeyPairs buildAsymKey() throws Exception { + + /* 锟斤拷始锟斤拷锟斤拷钥锟斤拷锟斤拷锟斤拷 */ + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(CipherConstant.RSA); + keyPairGenerator.initialize(2048, SecureRandomProxy.getRandomInstance()); + + /* 锟斤拷锟斤拷锟斤拷钥 */ + return new KeyPairs(keyPairGenerator.generateKeyPair()); + } + + /** + * buildAsymKey 锟斤拷锟斤拷一锟斤拷锟皆筹拷锟斤拷钥 + * + * @return 锟皆筹拷锟斤拷钥 + * @throws NoSuchAlgorithmException + * @throws Exception + */ + public static String buildSymKey() throws Exception { + // 锟斤拷锟斤拷Key + KeyGenerator keyGenerator = KeyGenerator.getInstance(CipherConstant.AES); + + keyGenerator.init(256, SecureRandomProxy.getRandomInstance()); + // 使锟斤拷锟斤拷锟斤拷锟斤拷锟街筹拷始锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟截讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷钥锟斤拷锟斤拷锟斤拷锟斤拷锟杰猴拷锟斤拷锟斤拷锟斤拷锟轿ㄒ伙拷潭锟斤拷摹锟� + SecretKey secretKey = keyGenerator.generateKey(); + + return Base64Util.encryptBASE64(secretKey.getEncoded()); + + } + + public static Key getPublicKey(String pubKey) throws Exception { + Key key = null; + + try { + byte[] keyBytes = Base64Util.decryptBASE64(pubKey); + KeyFactory keyFactory = KeyFactory.getInstance(CipherConstant.RSA); + + X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); + key = keyFactory.generatePublic(x509KeySpec); + + } catch (Exception e) { + throw new Exception("锟斤拷效锟斤拷锟斤拷钥 " + e.getMessage()); + } + + return key; + } + + public static Key getPrivateKey(String priKey) throws Exception { + Key key = null; + + try { + byte[] keyBytes = Base64Util.decryptBASE64(priKey); + + KeyFactory keyFactory = KeyFactory.getInstance(CipherConstant.RSA); + + PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); + key = keyFactory.generatePrivate(pkcs8KeySpec); + + } catch (Exception e) { + throw new Exception("锟斤拷效锟斤拷钥 " + e.getMessage()); + } + + return key; + } + + public static Key getSymKey(String symKey) throws Exception { + Key key = null; + + try { + byte[] keyBytes = Base64Util.decryptBASE64(symKey); + // Key转锟斤拷 + key = new SecretKeySpec(keyBytes, CipherConstant.AES); + } catch (Exception e) { + throw new Exception("锟斤拷效锟斤拷钥 " + e.getMessage()); + } + + return key; + } +} diff --git a/src/main/java/com/zy/nc/util/ResultMessageUtil.java b/src/main/java/com/zy/nc/util/ResultMessageUtil.java new file mode 100644 index 0000000..129123d --- /dev/null +++ b/src/main/java/com/zy/nc/util/ResultMessageUtil.java @@ -0,0 +1,45 @@ +package com.zy.nc.util; + +public class ResultMessageUtil { + + private boolean success; + + private Object data; + + private String code; + + private String errorMessage; + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + +} diff --git a/src/main/java/com/zy/nc/util/SHA256Util.java b/src/main/java/com/zy/nc/util/SHA256Util.java new file mode 100644 index 0000000..6eec9db --- /dev/null +++ b/src/main/java/com/zy/nc/util/SHA256Util.java @@ -0,0 +1,52 @@ +package com.zy.nc.util; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + +public class SHA256Util { + + public static String getSHA256(String str, String key) { + //锟斤拷锟斤拷 + byte[] salt = new byte[16]; + try { + SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); + random.setSeed(key.getBytes()); + random.nextBytes(salt); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + String salt_string = Base64Util.encryptBASE64(salt); + System.out.println("salt_String::" + salt_string); + return getSHA256(str + salt_string.replaceAll("\r|\n", "")); + } + + private static String getSHA256(String str) { + MessageDigest messageDigest; + String encodestr = ""; + try { + messageDigest = MessageDigest.getInstance("SHA-256"); + messageDigest.update(str.getBytes(StandardCharsets.UTF_8)); + encodestr = byte2Hex(messageDigest.digest()); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + return encodestr; + } + + private static String byte2Hex(byte[] bytes) { + StringBuffer stringBuffer = new StringBuffer(); + String temp = null; + for (int i = 0; i < bytes.length; i++) { + temp = Integer.toHexString(bytes[i] & 0xFF); + if (temp.length() == 1) { + // 1寰楀埌锟�?锟斤拷鐨勮繘琛岃ˉ0鎿嶄綔 + stringBuffer.append("0"); + } + stringBuffer.append(temp); + } + return stringBuffer.toString(); + } + +} diff --git a/src/main/java/com/zy/nc/util/SecureRandomProxy.java b/src/main/java/com/zy/nc/util/SecureRandomProxy.java new file mode 100644 index 0000000..1030078 --- /dev/null +++ b/src/main/java/com/zy/nc/util/SecureRandomProxy.java @@ -0,0 +1,26 @@ +package com.zy.nc.util; + +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + +/** + * @Describe + * @Author lizhmf + * @Date 2021/3/16 19:32 + * @Version 1.0 + */ +public class SecureRandomProxy { + + public static SecureRandom getRandomInstance() { + SecureRandom random = null; + try { + random = SecureRandom.getInstance("SHA1PRNG"); +// random = SecureRandom.getInstance("NativePRNG"); +// random = SecureRandom.getInstance("NativePRNGNonBlocking"); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + + return random; + } +} diff --git a/src/main/java/com/zy/nc/vo/SaleOutBodyVO.java b/src/main/java/com/zy/nc/vo/SaleOutBodyVO.java new file mode 100644 index 0000000..c09ae17 --- /dev/null +++ b/src/main/java/com/zy/nc/vo/SaleOutBodyVO.java @@ -0,0 +1,4 @@ +package com.zy.nc.vo; + +public class SaleOutBodyVO { +} diff --git a/src/main/java/com/zy/nc/vo/SaleOutHeadVO.java b/src/main/java/com/zy/nc/vo/SaleOutHeadVO.java new file mode 100644 index 0000000..257167c --- /dev/null +++ b/src/main/java/com/zy/nc/vo/SaleOutHeadVO.java @@ -0,0 +1,4 @@ +package com.zy.nc.vo; + +public class SaleOutHeadVO { +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f7428f3..125b490 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -74,8 +74,64 @@ # 鑷姩绉诲簱鍔熻兘寮�鍏� 绌挎 autoLocMoveUnilateralBoolean: false -comb: - limit: 5000 + + +# 鐢ㄥ弸NC瀵规帴 +NYNC: + ip: + prot: + #1.閿�鍞嚭搴撳崟鍙傜収鍙戣揣鍗曟柊澧� + saveRefDelivery: http://${ip}:${port}/nccloud/api/ic/saleout/saveRefDelivery + #2.鍏朵粬鍏ュ簱鍗曞弬鐓ц浆搴撴柊澧� + saveRefWhsTrans: http://ip:port/nccloud/api/ic/generalin/saveRefWhsTrans + #3.鏂板閲囪喘鍏ュ簱鍗� + purchasein: http://ip:port/nccloud/api/ic/purchasein/save + #4.1鏂板杞簱鍗� + whstrans: http://ip:port/nccloud/api/ic/whstrans/save + #4.2鍏朵粬鍑哄簱鍗曟柊澧� + generalout: http://ip:port/nccloud/api/ic/generalout/save + #4.3.鍏朵粬鍏ュ簱鍗曞弬鐓ц浆搴撴柊澧� + #saveRefWhsTrans: http://ip:port/nccloud/api/ic/generalin/saveRefWhsTrans + #5.1鏂板杞簱鍗� + #whstrans: http://ip:port/nccloud/api/ic/whstrans/save + #5.2鍏朵粬鍑哄簱鍗曟柊澧� + #generalout: http://ip:port/nccloud/api/ic/generalout/save + #5.3.鍏朵粬鍏ュ簱鍗曞弬鐓ц浆搴撴柊澧� + #saveRefWhsTrans: http://ip:port/nccloud/api/ic/generalin/saveRefWhsTrans + #6.璋冩嫧璁㈠崟鏌ヨ + #saveRefWhsTrans: http://ip:port/nccloud/api/ic/generalin/saveRefWhsTrans + #7.鐩樼偣鍗曟煡璇� + invcount: http://ip:port/nccloud/api/ic/invcount/query + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #erp瀵规帴 erp: diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties new file mode 100644 index 0000000..a47ff24 --- /dev/null +++ b/src/main/resources/config.properties @@ -0,0 +1,59 @@ +#####涓嶅彉鍙傛暟 +client_id=tchzt2 +client_secret=aad2433934c141b58937 +pubKey=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhxRFEtPfreu+ROWtC5IQZVy1Vkkqi0Fk7A4tGvyfLIw2qPNLWrTmCJeJwXLmgiB+EU0RW9U8jFYRAhSKaQY1HTjHMZcXdfH6m7WitT+sIyDVfTO3wVWtvjFBF1o6qi+T7pNsmCjstArnm/OS55kn0zJcRiTTwP1UU1LrkQbytf9ZHOcbKfqjIL8amsinjdxd6ioUM3JT3PpOXCIRDHsQgvAnFx2q7Y902S0PFzc40FwtuwsuKqesvEbIImUcq1wbIDtMegixy+TfJMWcByNLp2iv8/+Zos+hePhfWRPlD2x0vk30xDXMiUVgq0dhxC5C3yeD2QEbLxIlDqK2WebzZQIDAQAB +secret_level=L0 +#autotest=ture + +#client_id=intelliv +#client_secret=14af9d2c86ea4232a090 +#pubKey=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAvcAJGq8gUmfWXlkq7RnZu0LpPMn6p8/vtGEmrvWorymVmhP7IpxaWxr5/gUQcoZn0U1UXIGomFOIjIHq8pwg0O2AUNpFQYSyjjmzBu+Yzd7Kcn+0pucmu7PLX1zkPrMKSLcZ8gEtxa4YBA4x8L5af7uq+vIPXBgT2RZyWK0ceQIDAQAB +#secret_level=L0 + +#client_id=ublinker +#client_secret=4a7499ac19b744958e30 +#pubKey=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCS5avjb7GbHWNXB5XPC4gJRJHmvwyPMCvyKV6EJ7mq6kjiJBIf+t5Q8guRD41rswF7Nt+hWKs0rnWCc9ypqcTJwtbtHTkjOlD/I7C1KszyEbPT8mBRr0nQd203rfWZ+oKkPl1ENpmlDiNgStRjHZWvZM1ZzPd3yDhHZaUma0iCHwIDAQAB +#secret_level=L0 + +## 鏈嶅姟鍣ㄥ湴鍧� +#baseUrl=http://10.16.7.160:9088 +#baseUrl=http://10.10.4.166:9080 +baseUrl=http://192.168.5.200:1105 +#baseUrl=http://192.168.61.18:21322 +#baseUrl=https://ncc.yyncc.com + +## 璐﹀code +### ncc鐢ㄦ埛瀵嗙爜 +#busi_center=t8l33936 +#username=zhangmeng10 +#pwd=123456a + +#username=3 +#pwd=123456a! +#busi_center=1001 + +username=WMS +pwd=b5ee040e00584e518c06 +busi_center=fulai??0731 + +#username=01 +#pwd=123456a! +#busi_center=gh + +#usercode=34 + +#busi_id=111 +#repeat_check=y + +## 鑾峰彇token鏂瑰紡 +#grant_type=client_credentials +grant_type=password + +## 璇锋眰鍙傛暟 +requestBody={"head":{"pk_org":"Duxiaoman (Cayman) Limited","pk_currtype":"USD","bill_type":"F5","trade_type":"D5","source_flag":"17","bill_date":"2020-08-07 00:00:00","primal_money":"12400.00","memo":"P2P浠樻鐢宠|绀煎搧||娴嬭瘯 绾夸笅鏀粯鍥炰紶銆佸澶栭摱琛宻wift銆佸甯佹眹鐜�","def1":"ZPO810002020000319Y2","def2":"http://uatcaigou.duxiaoman-int.com/p2p/pages/plainPages.html#/payment_apply?tenantId=2&pageType=preview&paymentHeaderId=782&token=77f0842d6e9e5fadd1b165220ae27fd3","def3":"2008100100005","def30":"duxiaoman"},"body":[{"pk_org":"Duxiaoman (Cayman) Limited","pk_currtype":"USD","pk_balatype":"0","memo":"P2P浠樻鐢宠|绀煎搧||娴嬭瘯 绾夸笅鏀粯鍥炰紶銆佸澶栭摱琛宻wift銆佸甯佹眹鐜�","bill_type":"F5","trade_type":"D5","direction":"-1","pk_supplier":"1001AA10000000000L68","objecttype":"4","pk_oppaccount":"pk_oppaccount","accountcode":"110907597010206","accountopenbank":"MEGA INTERNATIONAL COMMERCIAL BANK CO., LTD., HONG KONG BRANCH","accountname":"鍖椾含浜笢涓栫邯淇℃伅鎶�鏈湁闄愬叕鍙�","bill_date":"2020-08-07 00:00:00","creationtime":"2020-08-10 11:27:39","pay_primal":"12400.00","def1":"","swift":"911910000001","paymemo":"P2P浠樻鐢宠|绀煎搧||娴嬭瘯 绾夸笅鏀粯鍥炰紶銆佸澶栭摱琛宻wift銆佸甯佹眹鐜�","remark":"娴嬭瘯 绾夸笅鏀粯鍥炰紶銆佸澶栭摱琛宻wift銆佸甯佹眹鐜�","accounttype":"0"}]} +#requestBody={} +## api璁块棶璺緞 +apiUrl=/nccloud/api/imag/invoice/ocr/syncrecord +#apiUrl=/nccloud/api/uapbd/ublinker/dealreq +#apiUrl=/nccloud/api/arap/gatheringbill/insert +#apiUrl=/nccloud/api/riasm/ctmsmanager/ctms/ctmstransfer \ No newline at end of file diff --git a/src/main/resources/lib/caffeine-2.8.8.jar b/src/main/resources/lib/caffeine-2.8.8.jar new file mode 100644 index 0000000..f7e648b --- /dev/null +++ b/src/main/resources/lib/caffeine-2.8.8.jar Binary files differ -- Gitblit v1.9.1