就有关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
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>
密码字段成功的过滤掉了。这也是我刚刚学过得,分享出来,有不足之处欢迎点出!
通过浏览器调试界面查看前台显示的字段中包含密码字段,如图:
该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>
密码字段成功的过滤掉了。这也是我刚刚学过得,分享出来,有不足之处欢迎点出!
相关文章推荐
- extjs和struts、json的整合 - taelons的专栏 - CSDNBlog
- struts与spring整合学习笔记
- Struts 2.1.6 精简实例系列教程(6):重写用户登录模块(整合Jquery+JSON)
- Extjs 3.3.0 Struts2.2.1 struts2-json-plugin-2.2.1.jar struts2-convention-plugin-2.2.1.jar 树结构 整合
- 通过配置JsonConfig过滤掉有可能引起循环的字段
- [导入]extjs和struts、json的整合
- Spring MVC中使用jackson的MixInAnnotations方法动态过滤JSON字段
- Struts 2 + Spring 2.0 + Hibernate 3.0整合笔记
- Struts 2 + Spring 2.0 + Hibernate 3.0整合笔记
- Struts 2 + Spring 2.0 + Hibernate 3.0整合笔记
- Struts 2 + Spring 2.0 + Hibernate 3.0整合笔记
- SSH学习笔记之关于struts-spring-hibernate整合配置问
- spring整合struts---个人笔记
- 有关Struts和Spring框架整合时出现的问题
- 强大的DataGrid组件[13]_字段过滤(Filter)——Silverlight学习笔记[21]
- [转]Struts+Spring+Hibernate整合笔记
- Struts+Spring+Hibernate整合笔记
- extjs和struts、json的整合
- struts2+json整合的各种问题
- 基于MyEclipse8.6开发Spring3.0+Hibernate3.3+Struts2.2整合笔记一