kongxiangyang hace 1 día
padre
commit
4cfc8c69e2

+ 0 - 2
crm-manager/src/main/java/com/crm/manager/controller/OcrController.java

@@ -3,8 +3,6 @@ package com.crm.manager.controller;
 import com.crm.manager.service.OcrService;
 import com.crm.rely.backend.core.dto.base.BaseResultDto;
 import com.crm.rely.backend.model.entity.ocr.OcrCheckEntity;
-import com.crm.rely.backend.util.AESUtil;
-import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;

+ 0 - 1
crm-manager/src/main/java/com/crm/manager/controller/TruForController.java

@@ -6,7 +6,6 @@ import com.crm.rely.backend.model.entity.trufor.TruForCheckEntity;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController

+ 10 - 4
crm-manager/src/main/java/com/crm/manager/service/impl/OcrServiceImpl.java

@@ -5,6 +5,7 @@ import com.crm.manager.config.ImageConfig;
 import com.crm.manager.repository.ImageRecognizeConfigRepository;
 import com.crm.manager.service.OcrService;
 import com.crm.manager.service.SysConfigService;
+import com.crm.manager.util.HutoolAesUtil;
 import com.crm.rely.backend.core.constant.Constants;
 import com.crm.rely.backend.core.dto.base.BaseResultDto;
 import com.crm.rely.backend.core.exception.ServiceException;
@@ -20,6 +21,7 @@ import org.jsoup.Connection;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.io.UnsupportedEncodingException;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -43,10 +45,14 @@ public class OcrServiceImpl implements OcrService {
      */
     @Override
     public BaseResultDto result(OcrCheckEntity entity) {
-        entity.setImageBase64(AESUtil.decrypt(entity.getImageBase64(),getPropertyKey()));
-        // 这里开始调用 python 接口
-        ImageConfig imageConfig = getImagePythonConfig();
-        return BaseResultDto.success(Constants.SUCCESS,AESUtil.encrypt(JSON.toJSONString(idCardOcr(entity.getImageBase64(),imageConfig.getOcrRequestUrl())),getPropertyKey()));
+        try {
+            entity.setImageBase64(HutoolAesUtil.decrypt(entity.getImageBase64(),getPropertyKey()));
+            // 这里开始调用 python 接口
+            ImageConfig imageConfig = getImagePythonConfig();
+            return BaseResultDto.success(Constants.SUCCESS, HutoolAesUtil.encrypt(JSON.toJSONString(idCardOcr(entity.getImageBase64(),imageConfig.getOcrRequestUrl())),getPropertyKey()));
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     /**

+ 11 - 5
crm-manager/src/main/java/com/crm/manager/service/impl/TruForServiceImpl.java

@@ -5,6 +5,7 @@ import com.crm.manager.config.ImageConfig;
 import com.crm.manager.repository.ImageRecognizeConfigRepository;
 import com.crm.manager.service.SysConfigService;
 import com.crm.manager.service.TruForService;
+import com.crm.manager.util.HutoolAesUtil;
 import com.crm.rely.backend.core.constant.Constants;
 import com.crm.rely.backend.core.dto.base.BaseResultDto;
 import com.crm.rely.backend.core.exception.ServiceException;
@@ -18,6 +19,7 @@ import org.jsoup.Connection;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.io.UnsupportedEncodingException;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -34,11 +36,15 @@ public class TruForServiceImpl implements TruForService {
 
     @Override
     public BaseResultDto result(TruForCheckEntity entity) {
-        entity.setImageBase64(AESUtil.decrypt(entity.getImageBase64(),getPropertyKey()));
-        // 这里开始调用 python 接口
-        ImageConfig imageConfig = getImagePythonConfig();
-        ImageDetectResult imageDetectResult = imageForgedDetect(entity.getImageBase64(),imageConfig.getForgeryRequestUrl());
-        return BaseResultDto.success(Constants.SUCCESS,AESUtil.encrypt(JSON.toJSONString(imageDetectResult),getPropertyKey()));
+        try {
+            entity.setImageBase64(HutoolAesUtil.decrypt(entity.getImageBase64(),getPropertyKey()));
+            // 这里开始调用 python 接口
+            ImageConfig imageConfig = getImagePythonConfig();
+            ImageDetectResult imageDetectResult = imageForgedDetect(entity.getImageBase64(),imageConfig.getForgeryRequestUrl());
+            return BaseResultDto.success(Constants.SUCCESS,HutoolAesUtil.encrypt(JSON.toJSONString(imageDetectResult),getPropertyKey()));
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     /**

+ 33 - 0
crm-manager/src/main/java/com/crm/manager/util/HutoolAesUtil.java

@@ -0,0 +1,33 @@
+package com.crm.manager.util;
+
+import cn.hutool.crypto.SecureUtil;
+import cn.hutool.crypto.symmetric.AES;
+import io.netty.util.CharsetUtil;
+
+import java.io.UnsupportedEncodingException;
+
+public class HutoolAesUtil {
+
+    public static String encrypt(String content,String key) throws UnsupportedEncodingException {
+        AES aes = SecureUtil.aes(key.getBytes(CharsetUtil.UTF_8));
+        // encryptBase64(明文, 字符集对象)
+        return aes.encryptBase64(content, CharsetUtil.UTF_8);
+    }
+
+    public static String decrypt(String cipherBase64,String key) throws UnsupportedEncodingException {
+        AES aes = SecureUtil.aes(key.getBytes(CharsetUtil.UTF_8));
+        // decryptStr(密文Base64, 字符集对象)
+        return aes.decryptStr(cipherBase64);
+    }
+
+    public static void main(String[] args) {
+        try {
+            String content = "123456";
+            String cipherBase64 = encrypt(content,"89745199521cb7946bae04c59ba1a115");
+            System.out.println(cipherBase64);
+            System.out.println(decrypt(cipherBase64,"89745199521cb7946bae04c59ba1a115"));
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 82 - 0
crm-manager/src/main/java/com/crm/manager/util/IdCardUtil.java

@@ -0,0 +1,82 @@
+package com.crm.manager.util;
+
+/**
+ * 18位身份证工具类(国标GB 11643-1999)
+ * 功能:校验身份证合法性、计算第18位校验码、15位转18位
+ */
+public class IdCardUtil {
+
+    // 17位加权因子
+    private static final int[] WEIGHT = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
+    // 校验码对照表(余数0-11对应)
+    private static final char[] CHECK_CODE = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
+
+    /**
+     * 校验18位身份证是否合法
+     * @param idCard 18位身份证号码
+     * @return true=合法,false=非法
+     */
+    public static boolean isValid18IdCard(String idCard) {
+        // 基础校验:长度必须18位
+        if (idCard == null || idCard.length() != 18) {
+            return false;
+        }
+
+        // 前17位必须是数字
+        String front17 = idCard.substring(0, 17);
+        if (!front17.matches("\\d+")) {
+            return false;
+        }
+
+        // 计算校验码
+        char calcCode = getCheckCode(front17);
+        // 获取身份证最后一位
+        char lastCode = idCard.charAt(17);
+
+        // 不区分大小写校验X
+        return Character.toUpperCase(calcCode) == Character.toUpperCase(lastCode);
+    }
+
+    /**
+     * 计算18位身份证的第18位校验码
+     * @param front17 身份证前17位
+     * @return 校验码(0-9、X)
+     */
+    public static char getCheckCode(String front17) {
+        int sum = 0;
+        for (int i = 0; i < 17; i++) {
+            int num = front17.charAt(i) - '0';
+            sum += num * WEIGHT[i];
+        }
+        // 取余
+        int remainder = sum % 11;
+        return CHECK_CODE[remainder];
+    }
+
+    /**
+     * 15位身份证升级为18位
+     * @param idCard15 15位身份证
+     * @return 18位身份证
+     */
+    public static String convert15To18(String idCard15) {
+        if (idCard15 == null || idCard15.length() != 15 || !idCard15.matches("\\d+")) {
+            throw new IllegalArgumentException("非法15位身份证");
+        }
+        // 15位=6位地址+6位生日(YYMMDD)+3位顺序码
+        // 18位=6位地址+8位生日(YYYYMMDD)+3位顺序码+1位校验码
+        String front17 = idCard15.substring(0, 6) + "19" + idCard15.substring(6);
+        char checkCode = getCheckCode(front17);
+        return front17 + checkCode;
+    }
+
+    // 测试
+    public static void main(String[] args) {
+        // 测试1:合法身份证(可替换成真实身份证测试)
+        String id18 = "110101200606049510";
+        System.out.println("18位身份证校验结果:" + isValid18IdCard(id18)); // true
+
+//        // 测试2:15位转18位
+//        String id15 = "110101900307451";
+//        System.out.println("15位转18位:" + convert15To18(id15)); // 11010119900307451X
+    }
+}

+ 2 - 1
crm-manager/src/main/resources/application-test.yml

@@ -12,7 +12,8 @@ spring:
 logging:
   level:
     com.crm.manager.dao.mapper: trace
-  config: classpath:logback-prod.xml
+  config: classpath:logback-dev.xml
+
 #  redis:
 #    host: 127.0.0.1
 login:

+ 6 - 0
crm-model/pom.xml

@@ -76,5 +76,11 @@
             <artifactId>commons-lang3</artifactId>
             <version>3.8.1</version>
         </dependency>
+
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-crypto</artifactId>
+            <version>5.8.25</version>
+        </dependency>
     </dependencies>
 </project>