use of com.diboot.core.data.encrypt.IEncryptStrategy in project diboot by dibo-software.
the class QueryBuilder method dtoToWrapper.
/**
* 转换具体实现
*
* @param dto
* @return
*/
private static <DTO> QueryWrapper<?> dtoToWrapper(DTO dto, Collection<String> fields) {
QueryWrapper<?> wrapper;
// 转换
LinkedHashMap<String, FieldAndValue> fieldValuesMap = extractNotNullValues(dto, fields);
if (V.isEmpty(fieldValuesMap)) {
return new QueryWrapper<>();
}
// 只解析有值的
fields = fieldValuesMap.keySet();
// 是否有join联表查询
boolean hasJoinTable = ParserCache.hasJoinTable(dto, fields);
if (hasJoinTable) {
wrapper = new DynamicJoinQueryWrapper<>(dto.getClass(), fields);
} else {
wrapper = new ExtQueryWrapper<>();
}
// 构建 ColumnName
List<AnnoJoiner> annoJoinerList = ParserCache.getBindQueryAnnos(dto.getClass());
BiFunction<BindQuery, Field, String> buildColumnName = (bindQuery, field) -> {
if (bindQuery != null) {
String key = field.getName() + bindQuery;
for (AnnoJoiner annoJoiner : annoJoinerList) {
if (key.equals(annoJoiner.getKey())) {
if (V.notEmpty(annoJoiner.getJoin())) {
// 获取注解Table
return annoJoiner.getAlias() + "." + annoJoiner.getColumnName();
} else {
return (hasJoinTable ? "self." : "") + annoJoiner.getColumnName();
}
}
}
}
return (hasJoinTable ? "self." : "") + BeanUtils.getColumnName(field);
};
// 忽略空字符串"",空集合等
BiFunction<Object, BindQuery, Boolean> ignoreEmpty = (value, bindQuery) -> bindQuery != null && // 忽略空字符串"",空集合等
bindQuery.strategy().equals(Strategy.IGNORE_EMPTY) && (// 字符串""
value instanceof String && S.isEmpty((String) value) || // 空集合
(value instanceof Collection && ((Collection<?>) value).size() == 0));
// 查找加密策略
BiFunction<BindQuery, String, IEncryptStrategy> findEncryptStrategy = (bindQuery, defFieldName) -> {
if (ENABLE_DATA_PROTECT) {
Class<?> clazz = bindQuery == null || bindQuery.entity() == NullType.class ? dto.getClass() : bindQuery.entity();
String fieldName = bindQuery == null || S.isEmpty(bindQuery.field()) ? defFieldName : bindQuery.field();
return ParserCache.getFieldEncryptorMap(clazz).get(fieldName);
}
return null;
};
// 构建QueryWrapper
for (Map.Entry<String, FieldAndValue> entry : fieldValuesMap.entrySet()) {
FieldAndValue fieldAndValue = entry.getValue();
Field field = fieldAndValue.getField();
// 忽略注解 @TableField(exist = false) 的字段
TableField tableField = field.getAnnotation(TableField.class);
if (tableField != null && !tableField.exist()) {
continue;
}
// 忽略字段
BindQuery query = field.getAnnotation(BindQuery.class);
if (query != null && query.ignore()) {
continue;
}
BindQuery.List queryList = field.getAnnotation(BindQuery.List.class);
Object value = fieldAndValue.getValue();
// 构建Query
if (queryList != null) {
wrapper.and(queryWrapper -> {
for (BindQuery bindQuery : queryList.value()) {
if (ignoreEmpty.apply(value, bindQuery)) {
continue;
}
IEncryptStrategy encryptor = findEncryptStrategy.apply(bindQuery, entry.getKey());
Comparison comparison = encryptor == null ? bindQuery.comparison() : Comparison.EQ;
String columnName = buildColumnName.apply(bindQuery, field);
buildQuery(queryWrapper.or(), comparison, columnName, encryptor == null ? value : encryptor.encrypt(value.toString()));
}
});
} else {
if (ignoreEmpty.apply(value, query)) {
continue;
}
IEncryptStrategy encryptor = findEncryptStrategy.apply(query, entry.getKey());
Comparison comparison = query != null && encryptor == null ? query.comparison() : Comparison.EQ;
String columnName = buildColumnName.apply(query, field);
buildQuery(wrapper, comparison, columnName, encryptor == null ? value : encryptor.encrypt(value.toString()));
}
}
return wrapper;
}
use of com.diboot.core.data.encrypt.IEncryptStrategy in project diboot by dibo-software.
the class ParserCache method getFieldEncryptorMap.
/**
* 获取该类保护字段加密器Map
*
* @param clazz 类型
* @return 非null,Map<字段名,加密器>
*/
@NonNull
public static Map<String, IEncryptStrategy> getFieldEncryptorMap(@NonNull Class<?> clazz) {
return FIELD_ENCRYPTOR_MAP.computeIfAbsent(clazz.getName(), k -> {
Map<String, IEncryptStrategy> fieldEncryptorMap = new HashMap<>(4);
for (Field field : BeanUtils.extractFields(clazz, ProtectField.class)) {
if (!field.getType().isAssignableFrom(String.class)) {
log.error("`@ProtectField` 仅支持 String 类型字段。");
continue;
}
ProtectField protect = field.getAnnotation(ProtectField.class);
IEncryptStrategy encryptor = getEncryptor(protect.encryptor());
fieldEncryptorMap.put(field.getName(), encryptor);
}
return fieldEncryptorMap.isEmpty() ? Collections.emptyMap() : fieldEncryptorMap;
});
}
Aggregations