|
@@ -0,0 +1,76 @@
|
|
|
|
+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.springframework.util.StringUtils;
|
|
|
|
+
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
+import java.sql.CallableStatement;
|
|
|
|
+import java.sql.PreparedStatement;
|
|
|
|
+import java.sql.ResultSet;
|
|
|
|
+import java.sql.SQLException;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@MappedJdbcTypes(JdbcType.VARCHAR)
|
|
|
|
+@MappedTypes({String.class})
|
|
|
|
+public class EncryptHandler extends BaseTypeHandler<String> {
|
|
|
|
+ /**
|
|
|
|
+ * 线上运行后勿修改,会影响已加密数据解密
|
|
|
|
+ */
|
|
|
|
+ private static final byte[] KEYS = "base20230213zzkj".getBytes(StandardCharsets.UTF_8);
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置参数
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
|
|
|
|
+ if (StringUtils.isEmpty(parameter)) {
|
|
|
|
+ ps.setString(i, null);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ AES aes = SecureUtil.aes(KEYS);
|
|
|
|
+ String encrypt = aes.encryptHex(parameter);
|
|
|
|
+ ps.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));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取值
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
|
+ return null;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取值
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public String getNullableResult(CallableStatement cs, int columnIndex) 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);
|
|
|
|
+ }
|
|
|
|
+}
|