Browse Source

fix encode

he2802 2 years ago
parent
commit
f75c6cf546

+ 1 - 1
zhongzheng-admin/src/main/resources/application.yml

@@ -141,7 +141,7 @@ mybatis-plus:
   executorType: SIMPLE
   # 指定外部化 MyBatis Properties 配置,通过该配置可以抽离配置,实现不同环境的配置部署
   configurationProperties: null
- # typeHandlersPackage: com.zhongzheng.common.type
+  type-handlers-package: com.zhongzheng.common.type
   configuration:
     # 自动驼峰命名规则(camel case)映射
     # 如果您的数据库命名符合规则无需使用 @TableField 注解指定数据库字段名

+ 23 - 40
zhongzheng-common/src/main/java/com/zhongzheng/common/type/EncryptHandler.java

@@ -2,10 +2,7 @@ package com.zhongzheng.common.type;
 
 import cn.hutool.crypto.SecureUtil;
 import cn.hutool.crypto.symmetric.AES;
-import org.apache.ibatis.type.BaseTypeHandler;
-import org.apache.ibatis.type.JdbcType;
-import org.apache.ibatis.type.MappedJdbcTypes;
-import org.apache.ibatis.type.MappedTypes;
+import org.apache.ibatis.type.*;
 import org.springframework.util.StringUtils;
 
 import java.nio.charset.StandardCharsets;
@@ -16,61 +13,47 @@ import java.sql.SQLException;
 
 
 @MappedJdbcTypes(JdbcType.VARCHAR)
-@MappedTypes({String.class})
-public class EncryptHandler extends BaseTypeHandler<String> {
+public class EncryptHandler implements TypeHandler<String> {
     /**
      * 线上运行后勿修改,会影响已加密数据解密
      */
     private static final byte[] KEYS = "base20230213zzkj".getBytes(StandardCharsets.UTF_8);
 
-    /**
-     * 设置参数
-     */
+
+
+    public String decrypt(String value) {
+        if (null == value) {
+            return null;
+        }
+        if (value.length()<20 ) {
+            return value;
+        }
+        return SecureUtil.aes(KEYS).decryptStr(value);
+    }
+
     @Override
-    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
-        if (StringUtils.isEmpty(parameter)) {
-            ps.setString(i, null);
+    public void setParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
+        if (StringUtils.isEmpty(s)) {
+            preparedStatement.setString(i, null);
             return;
         }
         AES aes = SecureUtil.aes(KEYS);
-        String encrypt = aes.encryptHex(parameter);
-        ps.setString(i, encrypt);
+        String encrypt = aes.encryptHex(s);
+        preparedStatement.setString(i, encrypt);
     }
 
-    /**
-     * 获取值
-     */
     @Override
-    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
-        System.out.println(columnName+"CCOi"+rs.getString(columnName));
-        return decrypt(rs.getString(columnName));
+    public String getResult(ResultSet resultSet, String s) throws SQLException {
+        return decrypt(resultSet.getString(s));
     }
 
-    /**
-     * 获取值
-     */
     @Override
-    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
+    public String getResult(ResultSet resultSet, int i) throws SQLException {
         return null;
-
     }
 
-    /**
-     * 获取值
-     */
     @Override
-    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
+    public String getResult(CallableStatement callableStatement, int i) throws SQLException {
         return null;
-
-    }
-
-    public String decrypt(String value) {
-        if (null == value) {
-            return null;
-        }
-        if (value.length()<20 ) {
-            return value;
-        }
-        return SecureUtil.aes(KEYS).decryptStr(value);
     }
 }

+ 0 - 1
zhongzheng-system/src/main/java/com/zhongzheng/modules/user/vo/UserVo.java

@@ -51,7 +51,6 @@ public class UserVo {
 	/** 身份证号 */
 	@Excel(name = "身份证号")
 	@ApiModelProperty("身份证号")
-//	@TableField(typeHandler = EncryptHandler.class)
 	private String idCard;
 	/** 手机号码 */
 	@Excel(name = "手机号码")

+ 1 - 1
zhongzheng-system/src/main/resources/mapper/modules/user/UserMapper.xml

@@ -10,7 +10,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <result property="nickname" column="nickname"/>
         <result property="realname" column="realname"/>
         <result property="sex" column="sex"/>
-        <result property="idCard" column="id_card"/>
+        <result property="idCard" column="id_card" typeHandler="com.zhongzheng.common.type.EncryptHandler"/>
         <result property="telphone" column="telphone"/>
         <result property="userLevel" column="user_level"/>
         <result property="userBirth" column="user_birth"/>