提交 9bafec32 authored 作者: liyinqiao's avatar liyinqiao

可能存在Map中value是个List<Map>或是Map,可能存在需要内部结构的key做加密的场景,增加对value内部结构加解密处理逻辑

上级 af624763
...@@ -6,34 +6,32 @@ import org.apache.commons.collections.MapUtils; ...@@ -6,34 +6,32 @@ import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.util.LinkedCaseInsensitiveMap; import org.springframework.util.LinkedCaseInsensitiveMap;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class MapCryptHandler implements CryptHandler{ public class MapCryptHandler implements CryptHandler{
@Override @Override
public Object encrypt(Object param, CryptProperties cryptProperties,String mapid) { public Object encrypt(Object param, CryptProperties cryptProperties,String mapid) {
Map map = (Map)param; Map map = (Map)param;
List<String> encryptKeyList =getMapEncryptKeys(map,mapid,cryptProperties); // liyinqiao 获取最外层的key中有加密配置的key集合 也有可能是内部结构的key需要加密,所以去掉判空直接返回的逻辑。
if(CollectionUtils.isEmpty(encryptKeyList)){ List<String> encryptKeyList = getMapEncryptKeys(map,mapid,cryptProperties);
return param; Map encryptParam = new LinkedCaseInsensitiveMap(map.size());
}else{ Set<Map.Entry> set = map.entrySet();
Map encryptParam = new LinkedCaseInsensitiveMap(map.size()); if(CollectionUtils.isNotEmpty(set)){
Set<Map.Entry> set = map.entrySet(); for (Map.Entry entry : set) {
if(CollectionUtils.isNotEmpty(set)){ String key = (String)entry.getKey();
for (Map.Entry entry : set) { Object value = entry.getValue();
String key = (String)entry.getKey(); if(encryptKeyList.contains(key.toLowerCase())){
Object value = entry.getValue(); encryptParam.put(key,CryptHandlerFactory.getCryptHandler(value).encrypt(value,cryptProperties,mapid));
if(encryptKeyList.contains(key.toLowerCase())){ }else if(value != null && !(value instanceof String)){
encryptParam.put(key,CryptHandlerFactory.getCryptHandler(value).encrypt(value,cryptProperties,mapid)); // liyinqiao Map 的 value 非是普通字符串时 需要对内部结构做加密
}else{ Object enCodeParam = CryptHandlerFactory.getCryptHandler(value).encrypt(value,cryptProperties,mapid);
encryptParam.put(key,value); encryptParam.put(key,enCodeParam);
} }else{
encryptParam.put(key,value);
} }
} }
return encryptParam;
} }
return encryptParam;
} }
private List<String> getMapEncryptKeys(Map map, String mapid, CryptProperties cryptProperties) { private List<String> getMapEncryptKeys(Map map, String mapid, CryptProperties cryptProperties) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论