您的位置:首页 > 编程语言 > Java开发

就有关json与struts整合时过滤字段的笔记

2016-09-15 20:24 543 查看
在很多的应用中,有些字段是不能带到前台显示的,例如密码字段,因此需要在传到前台显示的时候需要将此字段过滤。下面就是我关于json过滤字段的方法,使用了正则表达式。

通过浏览器调试界面查看前台显示的字段中包含密码字段,如图:

该json数据包含敏感字段密码选项,故需要过滤掉。<pre name="code" class="html"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">rows</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-array" style="color:inherit;min-width: 0px; min-height: 0px; box-sizing: border-box; position: relative; vertical-align: baseline; display: inline-block;">[,…]</span>
1:{id:
3, lastLoginTime: 1473939691479, userEmail: "121234313@qq.com", userGrade: 1, userName: "糖糖",…}
id:3
lastLoginTime:1473939691479
userEmail:"121234313@qq.com"
userGrade:1
userName:"糖糖"
userNtegration:8
<li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userPassword:"123456"</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;"></span></li>

userPhone:"18949344290"
userRegistdate:1471936892615
userSex:1
userStatus:0



通过观察分析可知,rows是一个数组的结构,利用正则表达式过滤时应该这样做

<result name="jsonMap" type="json">
<param name="root">jsonMap</param>
<!-- 过滤用户密码字段 -->
<param name="excludeProperties">
rows\[\d+\]\.userPassword,
</param>
</result>
如果不知道正则表达是的意思,请翻阅正则表达式api。这样就可以将rows下的userPassword字段过滤掉了。加上这个配置后,再次查询数据,结果显示如下:

<pre name="code" class="html"><ol class="children expanded" style="min-width: 0px; min-height: 0px; box-sizing: border-box; list-style-type: none; padding-left: 12px; display: block; color: rgb(33, 33, 33); font-family: Consolas, "Lucida Console", monospace; font-size: 12px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><li class="parent expanded" style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">rows</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-array" style="color:inherit;min-width: 0px; min-height: 0px; box-sizing: border-box; position: relative; vertical-align: baseline; display: inline-block;">[,…]</span></li><ol class="children expanded" style="min-width: 0px; min-height: 0px; box-sizing: border-box; list-style-type: none; padding-left: 12px; display: block;"><li class="parent expanded" style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">1</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-object" style="color:inherit;min-width: 0px; min-height: 0px; box-sizing: border-box; position: relative; vertical-align: baseline; display: inline-block;">{id: 3, lastLoginTime: 1473939691479, userEmail: "121234313@qq.com", userGrade: 1, userName: "糖糖",…}</span></li><ol class="children expanded" style="min-width: 0px; min-height: 0px; box-sizing: border-box; list-style-type: none; padding-left: 12px; display: block;"><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">id</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">3</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">lastLoginTime</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">1473939691479</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userEmail</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-string" style="color:#c41a16;min-width: 0px; min-height: 0px; box-sizing: border-box; white-space: pre; unicode-bidi: -webkit-isolate;">"121234313@qq.com"</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userGrade</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">1</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userName</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-string" style="color:#c41a16;min-width: 0px; min-height: 0px; box-sizing: border-box; white-space: pre; unicode-bidi: -webkit-isolate;">"糖糖"</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userNtegration</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">8</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userPhone</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-string" style="color:#c41a16;min-width: 0px; min-height: 0px; box-sizing: border-box; white-space: pre; unicode-bidi: -webkit-isolate;">"18949344290"</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userRegistdate</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">1471936892615</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userSex</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">1</span></li><li style="min-width: 0px; min-height: 16px; box-sizing: border-box; text-overflow: ellipsis; white-space: nowrap; position: relative; display: flex; align-items: center; -webkit-user-select: text;"><span class="name" style="color:#881391;min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0;">userStatus</span><span class="object-properties-section-separator" style="min-width: 0px; min-height: 0px; box-sizing: border-box; flex-shrink: 0; padding-right: 5px;">:</span><span class="value object-value-number" style="color:#1c0cf;min-width: 0px; min-height: 0px; box-sizing: border-box;">0</span></li></ol></ol></ol>


密码字段成功的过滤掉了。这也是我刚刚学过得,分享出来,有不足之处欢迎点出!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: