commons-lang常用工具类StringEscapeUtils使用

出处:http://my.oschina.net/ydsakyclguozi/blog/341496

在apache commons-lang(2.3以上版本)中为我们提供了一个方便做转义的工具类,主要是为了防止sql注入,xss注入攻击的功能。

commons-lang常用工具类StringEscapeUtils使用 - wjoygz - pauls private zone

1.escapeSql 提供sql转移功能,防止sql注入攻击,例如典型的万能密码攻击' ' or 1=1 ' '

1StringBuffer sql = new StringBuffer("select key_sn,remark,create_date from tb_selogon_key where 1=1 ");

2if(!CommUtil.isEmpty(keyWord)){

3sql.append(" and like '%" + StringEscapeUtils.escapeSql(keyWord) + "%'");

2.escapeHtml /unescapeHtml  转义/反转义html脚本

1System.out.println(StringEscapeUtils.escapeHtml("<A>dddd</A>"));   

2输出结果为:

<a>dddd</a>

1System.out.println(StringEscapeUtils.unescapeHtml("<a>dddd</a>"));   

2输出为:

<A>ddd</A>

3.escapeJavascript/unescapeJavascript 转义/反转义js脚本

1System.out.println(StringEscapeUtils.escapeJavaScript("<SCRIPT>alert('1111')</SCRIPT>

2"));   

3输出为:

<script>alert('111')</script>

4.escapeJava/unescapeJava 把字符串转为unicode编码

1System.out.println(StringEscapeUtils.escapeJava("中国"));   

2输出为:

用escapeJava方法转义之后的字符串为:/u4E2D/u56FD/u5171/u4EA7/u515A

另一个例子:

import org.apache.commons.lang.StringEscapeUtils;  

public class EscapeString {  

    public static void main(String[] args) throws Exception {  

        String str = "APEC召开时不让点柴火做饭";  

        System.out.println("用escapeJava方法转义之后的字符串为:"+StringEscapeUtils.escapeJava(str));  

        System.out.println("用unescapeJava方法反转义之后的字符串为:"+StringEscapeUtils.unescapeJava(StringEscapeUtils.escapeJava(str)));  

          

        System.out.println("用escapeHtml方法转义之后的字符串为:"+StringEscapeUtils.escapeHtml(str));  

        System.out.println("用unescapeHtml方法反转义之后的字符串为:"+StringEscapeUtils.unescapeHtml(StringEscapeUtils.escapeHtml(str)));  

          

        System.out.println("用escapeXml方法转义之后的字符串为:"+StringEscapeUtils.escapeXml(str));  

        System.out.println("用unescapeXml方法反转义之后的字符串为:"+StringEscapeUtils.unescapeXml(StringEscapeUtils.escapeXml(str)));  

          

        System.out.println("用escapeJavaScript方法转义之后的字符串为:"+StringEscapeUtils.escapeJavaScript(str));  

        System.out.println("用unescapeJavaScript方法反转义之后的字符串为:"+StringEscapeUtils.unescapeJavaScript(StringEscapeUtils.escapeJavaScript(str)));  

        /**输出结果如下: 

     用escapeJava方法转义之后的字符串为:APEC\u53EC\u5F00\u65F6\u4E0D\u8BA9\u70B9\u67F4\u706B\u505A\u996D

用unescapeJava方法反转义之后的字符串为:APEC召开时不让点柴火做饭

用escapeHtml方法转义之后的字符串为:APEC&#21484;&#24320;&#26102;&#19981;&#35753;&#28857;&#26612;&#28779;&#20570;&#39277;

用unescapeHtml方法反转义之后的字符串为:APEC召开时不让点柴火做饭

用escapeXml方法转义之后的字符串为:APEC&#21484;&#24320;&#26102;&#19981;&#35753;&#28857;&#26612;&#28779;&#20570;&#39277;

用unescapeXml方法反转义之后的字符串为:APEC召开时不让点柴火做饭

用escapeJavaScript方法转义之后的字符串为:APEC\u53EC\u5F00\u65F6\u4E0D\u8BA9\u70B9\u67F4\u706B\u505A\u996D

用unescapeJavaScript方法反转义之后的字符串为:APEC召开时不让点柴火做饭

    }  

}  


表单富文本输入时,有html,需要转义,html+加中文时,用StringEscapeUtils.escapeHtml转义时,中文也转义了,经过查找,最终找到spring的org.springframework.web.util.HtmlUtils.htmlEscape

?
1
2
3
4
5
<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version> 3.0 . 6 .RELEASE</version>
</dependency>


?
1
2
3
4
5
6
7
public  static  void  main(String[] args) {
     String a =  "<html>吃饭</html>" ;
     System.out.println(StringEscapeUtils.escapeHtml(a));
     System.out.println(StringEscapeUtils.unescapeHtml(StringEscapeUtils.escapeHtml(a)));
     System.out.println(HtmlUtils.htmlEscape(a));
     System.out.println(HtmlUtils.htmlUnescape(HtmlUtils.htmlEscape(a)));
}

执行结果:

&lt;html&gt;&#21507;&#39277;&lt;/html&gt;

<html>吃饭</html>

&lt;html&gt;吃饭&lt;/html&gt;

<html>吃饭</html>

感觉还是spring好,一点一滴的比较贴心。


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值