场景:自定义拦截器做数据的加解密。
-
注册自定义拦截器
@Configuration public class MyBatisConfig { // TODO: 注册自定义拦截器 @Bean @Order(999) public EncryptionInterceptor encryptionInterceptor() { return new EncryptionInterceptor(); } }
-
使用mybatis拦截器拦截查询。
@Intercepts({ @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}), @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}) }) public class EncryptionInterceptor implements Interceptor { @Autowired private EncryptionConfig encryptionConfig; @Override public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); MappedStatement ms = (MappedStatement) args[0]; Object parameter = args[1]; // 判断操作类型是insert, update 或 delete if (ms.getSqlCommandType().equals(SqlCommandType.INSERT) || ms.getSqlCommandType().equals(SqlCommandType.UPDATE) || ms.getSqlCommandType().equals(SqlCommandType.DELETE)) { // TODO: 加密字段 encryptFields(parameter); } else if (ms.getSqlCommandType().equals(SqlCommandType.SELECT)) { // TODO: 查询操作,在执行后需要对结果进行解密 Object result = invocation.proceed(); List<EncryptionConfig.Models> models = encryptionConfig.getModels(); for (EncryptionConfig.Models model : models) { if (judgmentModel(parameter, model)) { decryptFields(result); } } return result; } return invocation.proceed(); } private Boolean judgmentModel(Object parameter, EncryptionConfig.Models model) { MetaObject metaObject = SystemMetaObject.forObject(parameter); if (metaObject.getOriginalObject() instanceof MapperMethod.ParamMap) { if (metaObject.hasGetter("ew")) { Object param1 = metaObject.getValue("ew"); if (param1 != null) { Object originalObject = SystemMetaObject.forObject(param1).getOriginalObject(); if (originalObject instanceof QueryWrapper) { DataMap entity = (DataMap) ((QueryWrapper<?>) originalObject).getEntity(); if (entity != null) { Object modelFieldName = entity.get(FieldConstants._d_modelFieldName); if (modelFieldName != null) { String modelName = modelFieldName.toString(); return model.getModel().equals(modelName); } } } } } } return false; } private void encryptFields(Object parameter) throws Exception { List<EncryptionConfig.Models> models = encryptionConfig.getModels(); MetaObject metaObject = SystemMetaObject.forObject(parameter); if (metaObject.getOriginalObject() instanceof MapperMethod.ParamMap) { if (metaObject.hasGetter("et")) { Object param1 = metaObject.getValue("et"); // 确保 param1 不为空 if (param1 != null) { MetaObject param1MetaObject = SystemMetaObject.forObject(param1); if (param1MetaObject.getOriginalObject() instanceof LinkedHashMap) { // 再检查 _d_model 字段 if (param1MetaObject.hasGetter(FieldConstants._d_modelFieldName)) { String value1 = (String) param1MetaObject.getValue(FieldConstants._d_modelFieldName); for (EncryptionConfig.Models model : models) { if (model.getModel().equals(value1)) { for (String field : model.getFields()) { // 使用反射获取字段值并进行加密 Object value = metaObject.getValue("et." + field); if (value != null) { String encryptedValue = EncryptionUtil.encrypt(value.toString()); metaObject.setValue("et." + field, encryptedValue); } } } } } } } } } } private void decryptFields(Object result) throws Exception { List<EncryptionConfig.Models> models = encryptionConfig.getModels(); if (result instanceof ArrayList) { ArrayList<?> resultValue = (ArrayList<?>) result; for (EncryptionConfig.Models model : models) { for (String field : model.getFields()) { for (Object o : resultValue) { DataMap dataMap = (DataMap) o; Object fieldName = dataMap.get(field); if (fieldName != null) { String encryptedValue = fieldName.toString(); String decryptedValue = EncryptionUtil.decrypt(encryptedValue); dataMap.setValue(field, decryptedValue); } } } } } } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { } }
-
定义实体类,用于筛选模型
@Configuration @ConfigurationProperties(prefix = "encryption") public class EncryptionConfig { private List<Models> models; public static class Models { private String model; private List<String> fields; public String getModel() { return model; } public void setModel(String model) { this.model = model; } public List<String> getFields() { return fields; } public void setFields(List<String> fields) { this.fields = fields; } } public List<Models> getModels() { return models; } public void setModels(List<Models> models) { this.models = models; } }
Oinone社区 作者:yexiu原创文章,如若转载,请注明出处:https://doc.oinone.top/dai-ma-shi-jian/19717.html
访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验