【记录】mybait-plus字段入库加密,出库解密方案
引入mybatis-plus-boot-starter
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
编写AES加解密工具
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
public class AESUtils {
public final static String AES_KEY = "admin1234567890.";
public static final String LIKE_AES = "AES_DECRYPT(FROM_BASE64({}), '" + AESUtils.AES_KEY + "') LIKE {0}";
public static final String EQUAL_AES = "AES_DECRYPT(FROM_BASE64({}), '" + AESUtils.AES_KEY + "') = {0}";
public static <T, R> Wrapper<T> applyAesLike(AbstractWrapper<T, R, ?> wrapper, boolean condition, String column, Object val) {
return wrapper.apply(condition, StrUtil.format(AESUtils.LIKE_AES, column), "%" + val + "%");
}
public static <T, R> Wrapper<T> applyAesLikeLeft(AbstractWrapper<T, R, ?> wrapper, boolean condition, String column, Object val) {
return wrapper.apply(condition, StrUtil.format(AESUtils.LIKE_AES, column), "%" + val);
}
public static <T, R> Wrapper<T> applyAesLikeRight(AbstractWrapper<T, R, ?> wrapper, boolean condition, String column