概述
importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importorg.apache.commons.lang3.StringUtils;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Component;import java.util.*;
@Componentpublic classJsonEncryptUtil {//加密key
@Value("${EncryptionKey}")privateString encryptKey;//需要加密的日志节点
private static Map> encryptNodeMap = new HashMap<>(){
{
put("order/submit", Arrays.asList( //进件接口
"customerInfo.name","customerInfo.identityNo","customerInfo.mobile","cardInfo.creditCardNo","cardInfo.cashCardNo");
put("order/check", Arrays.asList( //验证接口
"name","identityNo"));}
};/*** 文本加密(忽略异常)
*
*@paramtext 入参
*@return加密字符串*/
publicString stringEncrypt(String text) {try{if (!StringUtils.isBlank(text)) {
text=EencryptionUtil.encrypt(text, encryptKey);
}
}catch(Exception e) {
text= "文本加密异常:" + e.getMessage() + "加密前信息:" +text;
}returntext;
}/*** json指定节点加密
*
*@paramjson 入参
*@return加密字符串*/
publicString jsonEncrypt(String url, String json) {
String result=json;try{if (!StringUtils.isBlank(json)) {for(String key : encryptNodeMap.keySet()){if(url.toLowerCase().endsWith(key.toLowerCase())){
result=GetAesJToken(JSON.parseObject(json.trim()), encryptNodeMap.get(key)).toString();
}
}
}
}catch(Exception e) {
result= "日志加密异常:" + e.getMessage() + "加密前信息:" +json;
}returnresult;
}/*** 根据节点逐一展开json对象并进行加密
*
*@paramobject 入参
*@paramnodeList 入参
*@return结果*/
private Object GetAesJToken(Object object, ListnodeList) {//如果为空,直接返回
if (object == null || nodeList.size() == 0) returnobject;
JSONObject jsonObject= null;//多层节点递归展开,单层节点直接加密
Map> deepLevelNodes = new HashMap<>();for(var node : nodeList) {
var nodeArr= Arrays.asList(node.split("\."));if (nodeArr.size() > 1) {if (deepLevelNodes.containsKey(nodeArr.get(0)))
deepLevelNodes.get(nodeArr.get(0)).add(com.ctrip.framework.apollo.core.utils.StringUtils.join(nodeArr.subList(1, nodeArr.size()), "."));elsedeepLevelNodes.put(nodeArr.get(0), new ArrayList<>(Arrays.asList(com.ctrip.framework.apollo.core.utils.StringUtils.join(nodeArr.subList(1, nodeArr.size()), "."))));
}else{
object=JsonNodeToAes(object, node);
}
}if (deepLevelNodes.size() > 0) {for(String key : deepLevelNodes.keySet()) {if(JSON.isValidObject(object.toString())) {
var jObject=JSON.parseObject(object.toString());if (jObject.get(key) != null) {
jObject.put(key, GetAesJToken(jObject.get(key), deepLevelNodes.get(key)));
}
object=jObject;
}if(JSON.isValidArray(object.toString())) {
var jArray=JSON.parseArray(object.toString());for (int i = 0; i < jArray.size(); i++) {
JSONObject curObject=jArray.getJSONObject(i);if (curObject != null && curObject.get(key) != null) {
jArray.set(i, GetAesJToken(curObject.get(key), deepLevelNodes.get(key)));
}
}
object=jArray;
}
}
}returnobject;
}/*** 将确定节点加密
*
*@paramobject 入参
*@paramnode 入参
*@return结果*/
privateObject JsonNodeToAes(Object object, String node) {if (object == null) returnobject;if(JSON.isValidObject(object.toString())) {
var jObject=JSON.parseObject(object.toString());if (jObject.get(node) != null) {if(JSON.isValidArray(jObject.get(node).toString())) {
var jArray=jObject.getJSONArray(node);for (int i = 0; i < jArray.size(); i++) {
jArray.set(i, stringEncrypt(jArray.get(i).toString()));
}
jObject.put(node, jArray);
}else if (!JSON.isValidObject(jObject.get(node).toString())) {
jObject.put(node, stringEncrypt(jObject.get(node).toString()));
}
}
object=jObject;
}else if(JSON.isValidArray(object.toString())) {
var jArray=JSON.parseArray(object.toString());for (int i = 0; i < jArray.size(); i++) {
Object curObject=jArray.getJSONObject(i);if (curObject != null) {
jArray.set(i, JsonNodeToAes(curObject, node));
}
}
object=jArray;
}else{
object=stringEncrypt(object.toString());
}returnobject;
}
}
最后
以上就是眼睛大微笑为你收集整理的java json 加密_对json中指定字段进行加密操作的全部内容,希望文章能够帮你解决java json 加密_对json中指定字段进行加密操作所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复