|
|
@@ -0,0 +1,49 @@
|
|
|
+package com.mrxu.framework.boot.mybatisplus.encrypt;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.crypto.SecureUtil;
|
|
|
+import com.mrxu.framework.common.util.MrxuAssert;
|
|
|
+import org.apache.ibatis.type.BaseTypeHandler;
|
|
|
+import org.apache.ibatis.type.JdbcType;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.sql.CallableStatement;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class AesEncryptTypeHandler <T> extends BaseTypeHandler<T> {
|
|
|
+
|
|
|
+ @Value("${mybatis-plus.mrxu-encrypt.aesKey:EF8FF97BD3EE2217B0850ACCF3732DF5}")
|
|
|
+ private String aesKey = "EF8FF97BD3EE2217B0850ACCF3732DF3";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
|
|
|
+ MrxuAssert.isNotEmpty(aesKey,"请设置aes加密秘钥:mybatis-plus.mrxu-encrypt.aesKey");
|
|
|
+ ps.setString(i, SecureUtil.aes(aesKey.getBytes(StandardCharsets.UTF_8)).encryptBase64((String)parameter));
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
+ MrxuAssert.isNotEmpty(aesKey,"请设置aes加密秘钥:mybatis-plus.mrxu-encrypt.aesKey");
|
|
|
+ String columnValue = rs.getString(columnName);
|
|
|
+ return StrUtil.isBlank(columnValue) ? (T)columnValue : (T) SecureUtil.aes(aesKey.getBytes(StandardCharsets.UTF_8)).decryptStr(columnValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
+ MrxuAssert.isNotEmpty(aesKey,"请设置aes加密秘钥:mybatis-plus.mrxu-encrypt.aesKey");
|
|
|
+ String columnValue = rs.getString(columnIndex);
|
|
|
+ return StrUtil.isBlank(columnValue) ? (T)columnValue : (T)SecureUtil.aes(aesKey.getBytes(StandardCharsets.UTF_8)).decryptStr(columnValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
+ MrxuAssert.isNotEmpty(aesKey,"请设置aes加密秘钥:mybatis-plus.mrxu-encrypt.aesKey");
|
|
|
+ String columnValue = cs.getString(columnIndex);
|
|
|
+ return StrUtil.isBlank(columnValue) ? (T)columnValue : (T)SecureUtil.aes(aesKey.getBytes(StandardCharsets.UTF_8)).decryptStr(columnValue);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|