ITPub博客

首页 > > > commons-lang常用工具类StringEscapeUtils使用--转

commons-lang常用工具类StringEscapeUtils使用--转

作者:longmanma 时间:2019-12-31 09:31:27 0 删除 编辑

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 =newStringBuffer("select key_sn,remark,create_date from tb_selogon_key where 1=1 ");

  if(!CommUtil.isEmpty(keyWord)){

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

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

System.out.println(StringEscapeUtils.escapeHtml("dddd"));

输出结果为:ddddSystem.out.println(StringEscapeUtils.unescapeHtml("dddd"));

输出为: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

另一个例子:

import org.apache.commons.lang.StringEscapeUtils; publicclass EscapeString {

publicstaticvoidmain(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召开时不让点柴火做饭

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

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

  用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

<dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>3.0.6.RELEASE</version></dependency>

publicstaticvoid main(String[] args) {

String a = "吃饭";

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)));

}

执行结果:

<html>吃饭</html>

吃饭 <html>吃饭</html>

吃饭

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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2730/viewspace-3171230/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-12-18

  • 博文量
    492
  • 访问量
    11986