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

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

上级 af624763
...@@ -6,19 +6,14 @@ import org.apache.commons.collections.MapUtils; ...@@ -6,19 +6,14 @@ 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;
}else{
Map encryptParam = new LinkedCaseInsensitiveMap(map.size()); Map encryptParam = new LinkedCaseInsensitiveMap(map.size());
Set<Map.Entry> set = map.entrySet(); Set<Map.Entry> set = map.entrySet();
if(CollectionUtils.isNotEmpty(set)){ if(CollectionUtils.isNotEmpty(set)){
...@@ -27,6 +22,10 @@ public class MapCryptHandler implements CryptHandler{ ...@@ -27,6 +22,10 @@ public class MapCryptHandler implements CryptHandler{
Object value = entry.getValue(); Object value = entry.getValue();
if(encryptKeyList.contains(key.toLowerCase())){ if(encryptKeyList.contains(key.toLowerCase())){
encryptParam.put(key,CryptHandlerFactory.getCryptHandler(value).encrypt(value,cryptProperties,mapid)); encryptParam.put(key,CryptHandlerFactory.getCryptHandler(value).encrypt(value,cryptProperties,mapid));
}else if(value != null && !(value instanceof String)){
// liyinqiao Map 的 value 非是普通字符串时 需要对内部结构做加密
Object enCodeParam = CryptHandlerFactory.getCryptHandler(value).encrypt(value,cryptProperties,mapid);
encryptParam.put(key,enCodeParam);
}else{ }else{
encryptParam.put(key,value); encryptParam.put(key,value);
} }
...@@ -34,7 +33,6 @@ public class MapCryptHandler implements CryptHandler{ ...@@ -34,7 +33,6 @@ public class MapCryptHandler implements CryptHandler{
} }
return encryptParam; return encryptParam;
} }
}
private List<String> getMapEncryptKeys(Map map, String mapid, CryptProperties cryptProperties) { private List<String> getMapEncryptKeys(Map map, String mapid, CryptProperties cryptProperties) {
List<String> result = new ArrayList<String>(); List<String> result = new ArrayList<String>();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论