mybatis-plus 字段加解密
【代码】mybatis-plus 字段加解密。
·
0.注册拦截器
package org.springblade.common.config;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import org.springblade.common.Interceptor.EncryptInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BladeMybatisConfiguration {
@Bean
public ConfigurationCustomizer configurationCustomizer(EncryptInterceptor interceptor) {
return configuration -> configuration.addInterceptor(interceptor);
}
}
1.存入和更新加密
package org.springblade.common.handler;
import lombok.Generated;
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.springblade.core.api.crypto.bean.CryptoInfoBean;
import org.springblade.core.api.crypto.config.ApiCryptoProperties;
import org.springblade.core.api.crypto.enums.CryptoType;
import org.springblade.core.api.crypto.util.ApiCryptoUtil;
import org.springframework.stereotype.Component;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@Component
@MappedTypes(String.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class BladeEncryptTypeHandler extends BaseTypeHandler<String> {
@Generated
private final ApiCryptoProperties apiCryptoProperties;
public BladeEncryptTypeHandler(ApiCryptoProperties apiCryptoProperties) {
this.apiCryptoProperties = apiCryptoProperties;
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
// 存入数据库时加密
CryptoInfoBean cryptoInfoBean = new CryptoInfoBean(CryptoType.AES, this.apiCryptoProperties.getAesKey());
ps.setString(i, ApiCryptoUtil.encryptData(this.apiCryptoProperties, parameter.getBytes(), cryptoInfoBean));
}
@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
return rs.getString(columnName);
}
@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return rs.getString(columnIndex);
}
@Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return cs.getString(columnIndex);
}
}
2.数据查询解密
package org.springblade.common.Interceptor;
import lombok.Generated;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.springblade.common.crypto.annotation.BladeMyBatisEncryptField;
import org.springblade.core.api.crypto.bean.CryptoInfoBean;
import org.springblade.core.api.crypto.config.ApiCryptoProperties;
import org.springblade.core.api.crypto.enums.CryptoType;
import org.springblade.core.api.crypto.util.ApiCryptoUtil;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Properties;
@Component
@Intercepts({
@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {java.sql.Statement.class})
})
public class EncryptInterceptor implements Interceptor {
@Generated
private final ApiCryptoProperties apiCryptoProperties;
public EncryptInterceptor(ApiCryptoProperties apiCryptoProperties) {
this.apiCryptoProperties = apiCryptoProperties;
}
@Override
public Object intercept(Invocation invocation) throws Throwable {
// 执行原始操作
Object result = invocation.proceed();
if (result instanceof List) {
for (Object obj : (List<?>) result) {
decryptFields(obj);
}
} else {
decryptFields(result);
}
return result;
}
/**
* 字段解密
*
* @param obj 解密对象
* @throws IllegalAccessException 参数异常
*/
private void decryptFields(Object obj) throws IllegalAccessException {
if (obj == null) return;
for (Field field : obj.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(BladeMyBatisEncryptField.class)) {
field.setAccessible(true);
Object value = field.get(obj);
if (value instanceof String) {
try {
//默认会执行两次,多次解密会爆异常,所以解密出错则原始输出
CryptoInfoBean cryptoInfoBean = new CryptoInfoBean(CryptoType.AES, this.apiCryptoProperties.getAesKey());
byte[] bytes = ApiCryptoUtil.decryptData(this.apiCryptoProperties, ((String) value).getBytes(), cryptoInfoBean);
field.set(obj, new String(bytes));
} catch (Exception e) {
field.set(obj, value);
}
}
}
}
}
@Override
public Object plugin(Object target) {
return Interceptor.super.plugin(target);
}
@Override
public void setProperties(Properties properties) {
Interceptor.super.setProperties(properties);
}
}
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.modules.biz.conflict.pojo.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springblade.common.crypto.annotation.BladeMyBatisEncryptField;
import org.springblade.common.handler.BladeEncryptTypeHandler;
import org.springblade.core.tenant.mp.TenantEntity;
import org.springblade.core.tool.jackson.Sensitive;
import org.springblade.core.tool.sensitive.SensitiveType;
import java.io.Serial;
/**
* 矛盾纠纷纠纷当事人 实体类
*
* @author BladeX
* @since 2025-07-01
*/
@Data
@TableName("com_conflict_dispute_person")
@Schema(description = "ConflictDisputePerson对象")
@EqualsAndHashCode(callSuper = true)
public class ConflictDisputePersonEntity extends TenantEntity {
@Serial
private static final long serialVersionUID = 1L;
/**
* 排序
*/
@Schema(description = "排序")
private Integer sort;
/**
* 姓名
*/
@Schema(description = "姓名")
private String name;
/**
* 身份证号
*/
@BladeMyBatisEncryptField
@TableField(typeHandler = BladeEncryptTypeHandler.class)
@Schema(description = "身份证号")
private String idCard;
/**
* 电话号码
*/
@BladeMyBatisEncryptField
@TableField(typeHandler = BladeEncryptTypeHandler.class)
@Schema(description = "电话号码")
private String phone;
/**
* 性别
*/
@Schema(description = "性别")
private String sex;
/**
* 是否是重点群体
*/
@Schema(description = "是否是重点群体")
private String isImport;
@Schema(description = "乡镇街道")
private String townStreet;
@Schema(description = "村社区")
private String village;
@BladeMyBatisEncryptField
@TableField(typeHandler = BladeEncryptTypeHandler.class)
@Schema(description = "居住地")
private String habitation;
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)