徐昊 10 месяцев назад
Родитель
Сommit
f91ee86aa4

+ 47 - 0
src/main/java/nckd/fi/all/common/utils/ApiHttpUtils.java

@@ -0,0 +1,47 @@
+package nckd.fi.all.common.utils;
+
+
+import com.alibaba.fastjson.JSON;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+
+public class ApiHttpUtils {
+
+    public static String Posthttp(String url, String Params) throws Exception {
+        // 获得Http客户端
+        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
+        // 创建Post请求
+        //设置请求路径
+        HttpPost httpPost = new HttpPost(url);
+        httpPost.setHeader("Content-type", "application/json;charset=utf-8");
+        StringEntity entity = new StringEntity(Params,"UTF-8");
+        // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
+        // 设置编码格式
+        entity.setContentEncoding("UTF-8");
+        // 发送Json格式的数据请求
+        entity.setContentType("application/json");
+        httpPost.setEntity(entity);
+        // 响应模型(发送post请求)
+        CloseableHttpResponse response = httpClient.execute(httpPost);
+        // 从响应模型中获取响应实体
+        HttpEntity responseEntity = response.getEntity();
+        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
+        if (responseEntity != null) {
+            jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity()));
+            return jsonObject.toJSONString();
+        }
+        // 释放资源
+        if (httpClient != null) {
+            httpClient.close();
+        }
+        if (response != null) {
+            response.close();
+        }
+        return "";
+    }
+}

+ 308 - 0
src/main/java/nckd/fi/all/common/utils/CodecUtil.java

@@ -0,0 +1,308 @@
+package webApiPlugin.utils;
+
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.util.Base64Utils;
+
+import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * @author bi lei
+ * @date 2020/12/18 10:35
+ */
+public class CodecUtil {
+    private static final Logger logger = LoggerFactory.getLogger(CodecUtil.class);
+    private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
+
+    /**
+     * AES加密密钥
+     */
+    public static final byte[] AES_SECRET_KEY_BYTES = Base64Utils.decodeFromString("KSKiOQgLKcdaCZLbnkgG7V==");
+
+    /**
+     * SHA1加密密钥(用于增加加密的复杂度)
+     */
+    public static final String SHA1_SECRET_KEY = "uErQ0KY3J2CwttyuaeEYR2==";
+
+    /**
+     * 对业务数据进行加密,用AES加密再用Base64编码
+     *
+     * @param data 待加密数据
+     * @return
+     */
+    public static String aesEncrypt(String data) {
+        try {
+            // 加密算法/工作模式/填充方式
+            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
+            byte[] dataBytes = data.getBytes("UTF-8");
+            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(AES_SECRET_KEY_BYTES, "AES"));
+            byte[] result = cipher.doFinal(dataBytes);
+            return Base64Utils.encodeToString(result);
+        } catch (Exception e) {
+            logger.error("执行CodecUtil.aesEncrypt失败:data={},异常:{}", data, e);
+        }
+        return null;
+    }
+
+    /**
+     * 对业务数据进行加密,用AES解密
+     *
+     * @param encryptedDataBase64
+     * @return
+     */
+    public static String aesDecrypt(String encryptedDataBase64) {
+        try {
+            // 加密算法/工作模式/填充方式
+            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
+            byte[] dataBytes = Base64Utils.decodeFromString(encryptedDataBase64);
+            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(AES_SECRET_KEY_BYTES, "AES"));
+            byte[] result = cipher.doFinal(dataBytes);
+            return new String(result);
+        } catch (Exception e) {
+            logger.error("执行CodecUtil.aesDecrypt失败:data={},异常:{}", encryptedDataBase64, e);
+        }
+        return null;
+    }
+
+
+    /**
+     * 对业务数据进行加密,用AES加密再用Base64编码
+     *
+     * @param data 待加密数据
+     * @return
+     */
+    public static String aesEncrypt(String data,String chartsetName) {
+        try {
+            // 加密算法/工作模式/填充方式
+            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
+            if(chartsetName == null || chartsetName.length()==0){
+                chartsetName = "UTF-8";
+            }
+            byte[] dataBytes = data.getBytes(chartsetName);
+            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(AES_SECRET_KEY_BYTES, "AES"));
+            byte[] result = cipher.doFinal(dataBytes);
+            return Base64Utils.encodeToString(result);
+        } catch (Exception e) {
+            logger.error("执行CodecUtil.aesEncrypt失败:data={},异常:{}", data, e);
+        }
+        return null;
+    }
+
+    /**
+     * 对业务数据进行加密,用AES解密
+     *
+     * @param encryptedDataBase64
+     * @return
+     */
+    public static String aesDecryp(String encryptedDataBase64,String chartsetName) {
+        try {
+            // 加密算法/工作模式/填充方式
+            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
+            byte[] dataBytes = Base64Utils.decodeFromString(encryptedDataBase64);
+            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(AES_SECRET_KEY_BYTES, "AES"));
+            byte[] result = cipher.doFinal(dataBytes);
+            if(chartsetName == null || chartsetName.length()==0){
+                chartsetName = "UTF-8";
+            }
+            return new String(result,chartsetName);
+        } catch (Exception e) {
+            logger.error("执行CodecUtil.aesDecrypt失败:data={},异常:{}", encryptedDataBase64, e);
+        }
+        return null;
+    }
+
+
+    /**
+     * 对业务数据进行加密,用AES解密
+     *
+     * @param encryptedDataBase64
+     * @return
+     */
+    public static String aesDecrypt(String encryptedDataBase64,byte[] aesSecretKeyBytes) {
+        try {
+            // 加密算法/工作模式/填充方式
+            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
+            byte[] dataBytes = Base64Utils.decodeFromString(encryptedDataBase64);
+            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aesSecretKeyBytes, "AES"));
+            byte[] result = cipher.doFinal(dataBytes);
+            return new String(result);
+        } catch (Exception e) {
+            logger.error("执行CodecUtil.aesDecrypt失败:data={},异常:{}", encryptedDataBase64, e);
+        }
+        return null;
+    }
+
+    /**
+     * 对业务数据进行加密,用AES解密
+     *
+     * @param encryptedDataBase64
+     * @return
+     */
+    public static String aesDecrypt(String encryptedDataBase64,byte[] aesSecretKeyBytes,String chartsetName) {
+        try {
+            // 加密算法/工作模式/填充方式
+            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
+            byte[] dataBytes = Base64Utils.decodeFromString(encryptedDataBase64);
+            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aesSecretKeyBytes, "AES"));
+            byte[] result = cipher.doFinal(dataBytes);
+
+            if(chartsetName == null || chartsetName.length()==0){
+                chartsetName = "UTF-8";
+            }
+            return new String(result,chartsetName);
+        } catch (Exception e) {
+            logger.error("执行CodecUtil.aesDecrypt失败:data={},异常:{}", encryptedDataBase64, e);
+        }
+        return null;
+    }
+
+    /**
+     * 对业务数据进行加密,用AES加密再用Base64编码
+     *
+     * @param data 待加密数据
+     * @return
+     */
+    public static String aesEncrypt(String data,byte[] AES_SECRET_KEY_BYTES) {
+        try {
+            // 加密算法/工作模式/填充方式
+            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
+            byte[] dataBytes = data.getBytes(DEFAULT_CHARSET);
+            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(AES_SECRET_KEY_BYTES, "AES"));
+            byte[] result = cipher.doFinal(dataBytes);
+            return Base64Utils.encodeToString(result);
+        } catch (Exception e) {
+            logger.error("执行CodecUtil.aesEncrypt失败:data={},异常:{}", data, e);
+        }
+        return null;
+    }
+
+    /**
+     * 对业务数据进行加密,用AES解密
+     *
+     * @param encryptedDataBase64
+     * @return
+     */
+    public static String aesDecryp(String encryptedDataBase64,byte[] AES_SECRET_KEY_BYTES) {
+        try {
+            // 加密算法/工作模式/填充方式
+            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
+            byte[] dataBytes = Base64Utils.decodeFromString(encryptedDataBase64);
+            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(AES_SECRET_KEY_BYTES, "AES"));
+            byte[] result = cipher.doFinal(dataBytes);
+            return new String(result,DEFAULT_CHARSET);
+        } catch (Exception e) {
+            logger.error("执行CodecUtil.aesDecrypt失败:data={},异常:{}", encryptedDataBase64, e);
+        }
+        return null;
+    }
+
+
+
+    /**
+     * 对数据进行加密,用SHA1加密再转换为16进制
+     *
+     * @param data
+     * @return
+     */
+    public static String sha1Encrypt(String data,String SHA1_SECRET_KEY ) {
+        return DigestUtils.sha1Hex(data + SHA1_SECRET_KEY);
+    }
+
+    /**
+     * 对数据进行加密,用SHA1加密再转换为16进制
+     *
+     * @param data
+     * @return
+     */
+    public static String sha1Encrypt(String data) {
+        return DigestUtils.sha1Hex(data + SHA1_SECRET_KEY);
+    }
+
+    /**
+     * AES密钥长度,支持128、192、256
+     */
+    private static final int AES_SECRET_KEY_LENGTH = 128;
+
+    private static String generateAESSecretKeyBase64(String key) {
+        try {
+            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
+            keyGenerator.init(AES_SECRET_KEY_LENGTH);
+            SecretKey secretKey = keyGenerator.generateKey();
+            return Base64Utils.encodeToString(secretKey.getEncoded());
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    static class LoginDemoVo {
+        private String account;
+        private String password;
+
+        public String getAccount() {
+            return account;
+        }
+
+        public void setAccount(String account) {
+            this.account = account;
+        }
+
+        public String getPassword() {
+            return password;
+        }
+
+        public void setPassword(String password) {
+            this.password = password;
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+
+        long timestamp = System.currentTimeMillis();
+        JSONObject r = new JSONObject();
+//        r.put("poundNo","");
+        r.put("subtaskId","13111111111");
+        r.put("validate","11111111");
+        r.put("beginDate","2024-04-03 11:11:11");
+        r.put("endDate","2024-04-03 11:11:11");
+        r.put("poundNo","");
+        r.put("unit",3);
+        r.put("batchNo",3);
+        r.put("productName",3);
+        r.put("netWeight",3);
+//        r.put("loadingStartDate","2024-04-03 11:11:11");
+//        r.put("loadingEndDate","2024-04-03 11:11:11");
+        r.put("remark",3);
+        r.put("operationStatus",3);
+        r.put("firstWeighingTime","2024-04-03 11:11:11");
+        r.put("secondWeighingTime","2024-04-03 11:11:11");
+        String data = JSON.toJSONString(r);
+        //对参数进行加密
+        String encryptedData = CodecUtil.aesEncrypt(data);
+        //生成签名
+        String sign = CodecUtil.sha1Encrypt(encryptedData + timestamp);
+        //封装请求参数
+        r = new JSONObject();
+        r.put("encryptedData",encryptedData);
+        r.put("timestamp",timestamp);
+        r.put("sign",sign);
+        //加密后的请求
+        System.out.println("加密后的请求:" + JSON.toJSONString(r));
+        //请求url
+//        String url = "http://123.183.159.70:5066/sso-server/user/register";
+        //返回结果
+//        String result2 = HttpRequestor.doPost(url, JSON.toJSONString(r));
+
+//        System.out.println(result2);
+
+    }
+}

+ 79 - 0
src/main/java/nckd/fi/all/common/utils/FileOutputStreamExample.java

@@ -0,0 +1,79 @@
+package nckd.fi.all.common.utility;
+
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.Base64;
+
+import static kd.bos.algox.flink.enhance.krpc.impl.DispatcherImpl.log;
+
+public class FileOutputStreamExample {
+    public static void main(String[] args) {
+        String filePath = "C:/Users/TR/Desktop/测试1.pdf";
+        String imgStrToBase64 = getImgStrToBase64(filePath);
+
+    }
+    /**
+     * 将网络链接图片或者本地图片文件转换成Base64编码字符串
+     *
+     * @param imgStr 网络图片Url/本地图片目录路径
+     * @return
+     */
+    public static String getImgStrToBase64(String imgStr) {
+        InputStream inputStream = null;
+        ByteArrayOutputStream outputStream = null;
+        byte[] buffer = null;
+        try {
+            //判断网络链接图片文件/本地目录图片文件
+            if (imgStr.startsWith("http://") || imgStr.startsWith("https://")) {
+                // 创建URL
+                URL url = new URL(imgStr);
+                // 创建链接
+                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+                conn.setRequestMethod("GET");
+                conn.setConnectTimeout(5000);
+                inputStream = conn.getInputStream();
+                outputStream = new ByteArrayOutputStream();
+                // 将内容读取内存中
+                buffer = new byte[1024];
+                int len = -1;
+                while ((len = inputStream.read(buffer)) != -1) {
+                    outputStream.write(buffer, 0, len);
+                }
+                buffer = outputStream.toByteArray();
+            } else {
+                inputStream = new FileInputStream(imgStr);
+                int count = 0;
+                while (count == 0) {
+                    count = inputStream.available();
+                }
+                buffer = new byte[count];
+                inputStream.read(buffer);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (inputStream != null) {
+                try {
+                    // 关闭inputStream流
+                    inputStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (outputStream != null) {
+                try {
+                    // 关闭outputStream流
+                    outputStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        // 对字节数组Base64编码
+        return Base64.getEncoder().encodeToString(buffer);
+    }
+}