src/main/java/com/zy/nc/Test.java
New file @@ -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.从resources/config.properties中读取测试api相关的数据 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方式:client_credentials、password private static String grant_type = null; // 服务器ip:port 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; } /** * 客户端模式获取token * * @return * @throws Exception */ private static String getTokenByClient() throws Exception { Map<String, String> paramMap = new HashMap<String, String>(); // 密码模式认证 paramMap.put("grant_type", "client_credentials"); // 第三方应用id paramMap.put("client_id", client_id); // 第三方应用secret 公钥加密 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 传递数据源和ncc登录用户 // 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"); // 第三方应用id paramMap.put("client_id", client_id); // 第三方应用secret 公钥加密 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访问所用token和secret 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("【ACCESS_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("【RESULT】:" + result); System.out.println("result解密:" + result2); } /** * 返回值进行过加密和压缩,对返回值进行解压和解密 * * @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; } /** * 发送post请求 * * @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"; } } src/main/java/com/zy/nc/util/Base64Util.java
New file @@ -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); } } src/main/java/com/zy/nc/util/CipherConstant.java
New file @@ -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"; } src/main/java/com/zy/nc/util/CompressUtil.java
New file @@ -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; } } src/main/java/com/zy/nc/util/Decryption.java
New file @@ -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; } } src/main/java/com/zy/nc/util/Encryption.java
New file @@ -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; } } src/main/java/com/zy/nc/util/KeyPairs.java
New file @@ -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()); } } src/main/java/com/zy/nc/util/KeysFactory.java
New file @@ -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; } } src/main/java/com/zy/nc/util/ResultMessageUtil.java
New file @@ -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; } } src/main/java/com/zy/nc/util/SHA256Util.java
New file @@ -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(); } } src/main/java/com/zy/nc/util/SecureRandomProxy.java
New file @@ -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; } } src/main/java/com/zy/nc/vo/SaleOutBodyVO.java
New file @@ -0,0 +1,4 @@ package com.zy.nc.vo; public class SaleOutBodyVO { } src/main/java/com/zy/nc/vo/SaleOutHeadVO.java
New file @@ -0,0 +1,4 @@ package com.zy.nc.vo; public class SaleOutHeadVO { } 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: src/main/resources/config.properties
New file @@ -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付款申请|礼品||测试 线下支付回传、境外银行swift、外币汇率","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付款申请|礼品||测试 线下支付回传、境外银行swift、外币汇率","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付款申请|礼品||测试 线下支付回传、境外银行swift、外币汇率","remark":"测试 线下支付回传、境外银行swift、外币汇率","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 src/main/resources/lib/caffeine-2.8.8.jarBinary files differ