在apache commons-lang(2.3以上版本)中爲咱們提供了一個方便作轉義的工具類,主要是爲了防止sql注入,xss注入攻擊的功能。html
commons-lang經常使用工具類StringEscapeUtils使用 - wjoygz - pauls private zonejava
1.escapeSql 提供sql轉移功能,防止sql注入攻擊,例如典型的萬能密碼攻擊' ' or 1=1 ' '
web
1StringBuffer sql = new StringBuffer("select key_sn,remark,create_date from tb_selogon_key where 1=1 ");
spring
2if(!CommUtil.isEmpty(keyWord)){sql
3sql.append(" and like '%" + StringEscapeUtils.escapeSql(keyWord) + "%'");apache
2.escapeHtml /unescapeHtml 轉義/反轉義html腳本mvc
1System.out.println(StringEscapeUtils.escapeHtml("<A>dddd</A>"));
app
2輸出結果爲:xss
<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召开时不让点柴火做饭
用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>
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))); }
執行結果:
<html>吃饭</html>
<html>吃飯</html>
<html>吃飯</html>
<html>吃飯</html>
感受仍是spring好,一點一滴的比較貼心。