|
@@ -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);
|
|
|
}
|
|
|
}
|