commons-lang常用工具类StringEscapeUtils使用--转
https://my.oschina.net/ydsakyclguozi/blog/341496 在apache commons-lang(2.3以上版本)中为我们提供了一个方便做转义的工具类,主要是为了防止sql注入,xss注入攻击的功能。 commons-lang常用工具类StringEscapeUtils使用: 1.escapeSql 提供sql转移功能,防止sql注入攻击,例如典型的万能密码攻击' ' or 1=1 ' ' StringBuffer sql = StringBuffer("select key_sn,remark,create_date from tb_selogon_key where 1=1 " <span style="color: #0000ff;">if(!<span style="color: #000000;">CommUtil.isEmpty(keyWord)){
sql.append(" and like '%" + StringEscapeUtils.escapeSql(keyWord) + "%'"); 2.escapeHtml /unescapeHtml ?转义/反转义html脚本 System.out.println(StringEscapeUtils.escapeHtml("dddd"输出结果为:
dddd<span style="color: #000000;">
System.out.println(StringEscapeUtils.unescapeHtml("dddd"<span style="color: #000000;">)); 输出为: ddd 3.escapeJavascript/unescapeJavascript 转义/反转义js脚本 System.out.println(StringEscapeUtils.escapeJavaScript(""输出为:
4.escapeJava/unescapeJava 把字符串转为unicode编码 System.out.println(StringEscapeUtils.escapeJava("中国"输出为:
用escapeJava方法转义之后的字符串为: /u4E2D/u56FD/u5171/u4EA7/u515A另一个例子: <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> EscapeString {
用unescapeJava方法反转义之后的字符串为:APEC召开时不让点柴火做饭 用escapeHtml方法转义之后的字符串为:APEC召开时不让点柴火做饭 用unescapeHtml方法反转义之后的字符串为:APEC召开时不让点柴火做饭 用escapeXml方法转义之后的字符串为:APEC召开时不让点柴火做饭 用unescapeXml方法反转义之后的字符串为:APEC召开时不让点柴火做饭 用escapeJavaScript方法转义之后的字符串为:APECu53ECu5F00u65F6u4E0Du8BA9u70B9u67F4u706Bu505Au996D 用unescapeJavaScript方法反转义之后的字符串为:APEC召开时不让点柴火做饭
} 表单富文本输入时,有html,需要转义,html+加中文时,用StringEscapeUtils.escapeHtml转义时,中文也转义了,经过查找,最终找到spring的org.springframework.web.util.HtmlUtils.htmlEscape
org.springframework
spring-webmvc
3.0.6.RELEASE
= "吃饭"
</span>&lt;html&gt;&#21507;&#39277;&lt;/html&<span style="color: #000000;">gt;
</span><html>吃饭</html>
&lt;html&gt;吃饭&lt;/html&<span style="color: #000000;">gt;
</span><html>吃饭</html></pre>
感觉还是spring好,一点一滴的比较贴心。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |