|
|
@@ -0,0 +1,80 @@
|
|
|
+package com.zhongzheng.common.util;
|
|
|
+
|
|
|
+import cn.hutool.crypto.SecureUtil;
|
|
|
+import cn.hutool.crypto.symmetric.AES;
|
|
|
+import org.apache.ibatis.type.JdbcType;
|
|
|
+import org.apache.ibatis.type.MappedJdbcTypes;
|
|
|
+import org.apache.ibatis.type.TypeHandler;
|
|
|
+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)
|
|
|
+public class EncryptHandler implements TypeHandler<String> {
|
|
|
+
|
|
|
+ private static final byte[] KEYS = "base20260327yxxt".getBytes(StandardCharsets.UTF_8);
|
|
|
+
|
|
|
+ public static final boolean ENCRYPT_STATUS = true; //是否开启字段加密
|
|
|
+
|
|
|
+
|
|
|
+ public static String decrypt(String value) {
|
|
|
+ if (null == value) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (value.length()<=20 ) {
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+ return SecureUtil.aes(KEYS).decryptStr(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String encrypt(String value){
|
|
|
+ if (null == value) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if(ENCRYPT_STATUS){
|
|
|
+ AES aes = SecureUtil.aes(KEYS);
|
|
|
+ String encrypt = aes.encryptHex(value);
|
|
|
+
|
|
|
+ return encrypt;
|
|
|
+ }else{
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
|
|
|
+ if (StringUtils.isEmpty(s)) {
|
|
|
+ preparedStatement.setString(i, null);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String encrypt = encrypt(s);
|
|
|
+ preparedStatement.setString(i, encrypt);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getResult(ResultSet resultSet, String s) throws SQLException {
|
|
|
+ if(ENCRYPT_STATUS){
|
|
|
+ return decrypt(resultSet.getString(s));
|
|
|
+ }else{
|
|
|
+ return resultSet.getString(s);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getResult(ResultSet resultSet, int i) throws SQLException {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getResult(CallableStatement callableStatement, int i) throws SQLException {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|