目录

1.集成jsoneditor到React

2.修改代码

(1).添加Option

(2).修改Node.js文件

(3).修改util.js文件

3.扩展:


1.集成jsoneditor到React

关于怎样将jsoneditor集成到React中,请参考文章:https://blog.csdn.net/wdquan19851029/article/details/115869264,本文只讲怎样修改jsoneditor源码,使用password格式保护json敏感信息。

从github下载源代码:https://github.com/josdejong/jsoneditor

2.修改代码

我们得考虑,真正使用的时候,我们一般只是需要保护部分敏感信息,没有必要保护所有信息,那样根本什么信息都看不到,所以我们只考虑保护部分信息。

看我们的json数据,其中有需要保护的数据 "sensitive-data".

        const initialJson = {"Array": [1, 2, 3],"Boolean": true,"Null": null,"Number": 123,"Object": {"a": "b", "c": "d"},"MultiLevel":{"multi":{"e": "f","h": "h"}},"String": "Hello World","sensitive-data": {"sensitive1": "sensitive1","sensitive2": "sensitive2","sensitive3": "sensitive3"}}

(1).添加Option

需要保护的信息,我们设计以option的形式传进去。我们自己添加一个新的参数sensitiveDataLabel ,设置其值为"sensitive-data"

        const options = {mode: 'tree',sensitiveDataLabel: 'sensitive-data',onChangeJSON: (data)=>{console.log('JSONeditor:change:',data);}}

(2).修改Node.js文件

然后修改Node.js的_createDomValue ()函数:其中注释为"新增的代码",是我做的修改,主要是判断parent node的name是不是与我们传进来的option sensitiveDataLabel 相等,如果相等,那么就创建一个input标签,type为password,用来保护敏感信息。

  /*** Create an editable value* @private*/_createDomValue () {let domValueif (this.type === 'array') {domValue = document.createElement('div')domValue.textContent = '[...]'} else if (this.type === 'object') {domValue = document.createElement('div')domValue.textContent = '{...}'} else {if (!this.editable.value && isUrl(this.value)) {// create a link in case of read-only editor and value containing an urldomValue = document.createElement('a')domValue.href = this.valuedomValue.innerHTML = this._escapeHTML(this.value)} else {// create an editable or read-only div//新增的代码let isSensitiveData = false//判断是否敏感数据方式1,只看当前node的parent,这是不合适的,因为可能有嵌套//if(this.parent) {//let sensitiveDataLabel = ""//if(this.editor.options.sensitiveDataLabel) sensitiveDataLabel = this.editor.options.sensitiveDataLabel//if(this.parent.getName() && this.parent.getName() == sensitiveDataLabel){//isSensitiveData = true//}//}//判断是否敏感数据方式2,因为敏感数据也有可能嵌套,所以不能只看当前node的parent,而要看其所有的json路径let nodePaths = this.getNodePath()let sensitiveDataLabel = ""if(this.editor.options.sensitiveDataLabel) sensitiveDataLabel = this.editor.options.sensitiveDataLabelfor(var i = 0; i < nodePaths.length; i++) {if(nodePaths[i].field && nodePaths[i].field == sensitiveDataLabel){isSensitiveData = truebreak;}}if(isSensitiveData){//新增的代码domValue = document.createElement('input')domValue.type = "password"domValue.placeholder=""domValue.autocomplete = "new-password" //防止自动填充保存过的登录密码domValue.style.border = "none" //去掉输入框的边框domValue.contentEditable = this.editable.valuedomValue.spellcheck = falsedomValue.value = this._escapeHTML(this.value)}else{domValue = document.createElement('div')domValue.contentEditable = this.editable.valuedomValue.spellcheck = falsedomValue.innerHTML = this._escapeHTML(this.value)}}}return domValue}

重新编译源代码,并导入React工程之后,可以看到效果:

(3).修改util.js文件

但是当进行修改的时候,发现修改无效,头疼,继续修改代码,修改util.js文件中的getInnerText (element, buffer)函数。

注释为"新增的代码"部分,就是我做的修改,将用户在上面界面的修改,赋给child这个Node,因为以前都是用<div>标签去显示value,现在改成了<input>,按照以前的方式,是用innerHTML属性获取值,但是现在需要使用value属性去获取值。

export function getInnerText (element, buffer) {const first = (buffer === undefined)if (first) {buffer = {_text: '',flush: function () {const text = this._textthis._text = ''return text},set: function (text) {this._text = text}}}// text node//console.log("Debug: getInnerText() -> element.nodeValue : " + element.nodeValue)if (element.nodeValue) {// remove return characters and the whitespace surrounding return charactersconst trimmedValue = element.nodeValue.replace(/\s*\n\s*/g, '')if (trimmedValue !== '') {return buffer.flush() + trimmedValue} else {// ignore empty textreturn ''}}// divs or other HTML elementsif (element.hasChildNodes()) {const childNodes = element.childNodeslet innerText = ''for (let i = 0, iMax = childNodes.length; i < iMax; i++) {const child = childNodes[i]//新增的代码if(element.type == "password" && element.value){child.nodeValue = element.value;}if (child.nodeName === 'DIV' || child.nodeName === 'P') {const prevChild = childNodes[i - 1]const prevName = prevChild ? prevChild.nodeName : undefinedif (prevName && prevName !== 'DIV' && prevName !== 'P' && prevName !== 'BR') {if (innerText !== '') {innerText += '\n'}buffer.flush()}innerText += getInnerText(child, buffer)buffer.set('\n')} else if (child.nodeName === 'BR') {innerText += buffer.flush()buffer.set('\n')} else {innerText += getInnerText(child, buffer)}}return innerText}// br or unknownreturn ''
}

3.扩展:

如果除了"sensitive-data"数据,还有别的数据需要保护,怎么办? 其实非常简单,传参数的时候,传入一个数组 sensitiveDataLabel: ['sensitive-data', 'sensitive-data1', 'sensitive-data2'],然后在Node.js中判断的时候,将if(nodePaths[i].field && nodePaths[i].field == sensitiveDataLabel) 改为if(nodePaths[i].field && sensitiveDataLabel.indexOf(nodePaths[i].field) > -1),大体就是这样子,有需要的话,请自己进行修改。

React 集成jsoneditor后,password格式保护json数据的敏感信息相关推荐

  1. 如何保护个人隐私和敏感信息不被黑客窃取?

    网络安全是当前世界面临的最大挑战之一.黑客们不断进化和发展出更加高级的攻击技术,导致我们的个人隐私和敏感信息遭受威胁.在这篇文章中,我将会分享一些关于如何保护我们的个人隐私和敏感信息不被黑客窃取的重要 ...

  2. 怎样使用fastJson发送数组格式的Json数据

    场景 之前要发送的json数据: {"PrintContent":"CAB1DM1152CJ@2097812420006@20181010,CAB1DM1152CJ@20 ...

  3. 【232期】面试官:如何保护 Spring Boot 配置文件敏感信息?

    点击上方"Java精选",选择"设为星标" 别问别人为什么,多问自己凭什么! 下方有惊喜,留言必回,有问必答! 每天 08:15 更新文章,每天进步一点点... ...

  4. SpringBoot处理JSON数据

    SpringBoot内置了JSON解析功能,默认使用Jackson来自动完成 当Controller返回的是一个Java对象或者是List集合时,SpringBoot自动将其转换成JSON数据 一.新 ...

  5. pg高性能服务器,如何充分利用单台服务器的性能将10亿级的json数据尽可能高效的插入postgresql?...

    问题说明: 目前有一个文本格式的json数据,里面数据量约15亿,每一行数据的格式都是固定的,插入前先要将json反序列化.运行环境为:windows server 2016,postgresql 1 ...

  6. SpringMVC响应的方式,无数据跳转页面,带数据跳转页面.Json数据返回

    SpringMVC响应的方式,无数据跳转页面,带数据跳转页面.Json数据返回 页面跳转设定 //转发会把后端携带的request和Response发送到前端jsp,在jsp中可以直接使用其中的数据/ ...

  7. Swift 与 JSON 数据

    转载自: http://www.cnblogs.com/theswiftworld/p/4660177.html 我们大家平时在开发 App 的时候,相信接触最多的就是 JSON 数据了.只要你的 A ...

  8. js实现格式化JSON数据方法

    前言: 最近做的项目中遇到个需要在前端页面中将某个设备需要的数据格式展示出来,方便用户配置.一开始单纯的将数据格式写入到pre标签中, 但是通过pre标签写入的数据格式在代码可视化上不是很优雅.由于上 ...

  9. 【PI应用】用Java查询雅虎天气并解析Json数据

    这篇文章是"树莓派查询天气,通过串口彩屏/7219点阵实时显示"的一部分,主要介绍使用Java查询雅虎天气并解析Json返回数据,这里只是将获得的实时天气.未来十天的天气等信息,解 ...

  10. 在php中怎么解析json数据,php解析json数据

    在我们使用编程语言的时候,对于数据的格式会出现不能直接使用的情况,所以就会有解析的操作.在php中有专门解析json的函数,那就是json_decode().想要进一步的运用这个函数,我们还需要对它的 ...

最新文章

  1. Quill编辑器IOS下无法获取焦点的解决方法
  2. UpTime:供电、系统、网络、制冷——2020年数据中心宕机四大主因
  3. 亚马逊查询关键词排名的工具_查询关键词排名收录的作用与操作
  4. Python基础教程:新式类与经典类
  5. 目标检测系列(八)——CenterNet:Objects as points
  6. sql 两个表列包含_Oracle数据库扩展语言PL/SQL之运算符
  7. httpd Server not started: (13)Permission denied: make_sock: could not bind to address [::]:88
  8. Sqlserver2008相关配置问题
  9. 计算机维修和维护实训报告,计算机维护与维修实训报告书.docx
  10. AngularJ控制器
  11. java图表分析_怎样用图表分析数据(一)
  12. bsod错误代码。_BSOD的完整形式是什么?
  13. 【Mysql】Error 1826: Duplicate foreign key constraint 错误
  14. DellR720服务器上安装EXSI6.5.0全教程
  15. 总结:云原生架构理解
  16. 腾讯在线教育互动课堂——Demo调试过程记录
  17. 苹果、微软合作推新版iCloud for Windows app
  18. 个人中心修改用户头像
  19. 4 frontpage+Access数据库信息的删除
  20. NC17134 Symmetric Matrix(dp+数学)

热门文章

  1. Zmap详细用户手册及DDOS的可行性
  2. ffplay整体框架
  3. OFD文件是什么?如何将ofd转成PDF格式?
  4. 华为HCNA之PPP认证实验
  5. 药品信息管理系统php,医药行业信息化管理系统
  6. python视频教程马哥_【进阶】51CTO马哥Python从入门到精通视频 60课
  7. 微信小程序——云开发入门
  8. PPM 金字塔池化模块 - PSPNet
  9. Java基础语法(详细版)
  10. 运筹优化(五)--线性规划之内点法