在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召开时不让点柴火做饭
用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
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)));
} |
相关推荐
lang中有一个很有用的处理字符串的工具类,其中之一就是StringEscapeUtils,这个工具类是在2.3版本以上加上的去的,利用它能很方便的进行html,xml,java等的转义与反转义,而且还能对关键字符串进行处理预防SQL注入,...
StringEscapeUtils方法消除json反斜杠
解决json数据中,返回的数据格式中带有反斜杠 如下所示{\"Count\":\"3\",\"ErrorString\":\"\",\"Success\":true,\"URL\":\"http:\\\/\\\/172.16.80.65:8080\\\/LoginSSO.aspx?UserCode=wubg&Type=WorkItem\",\...
第一步:引入Jsoup和lang和lang3的依赖: ...lang和lang3这两个包里有转换所需的工具类 ...import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.jsoup.Jsoup; im
解决脚本攻击xss可用到此资源,利用org.apache.commons.lang3.StringEscapeUtils这个类对输入的参数进行html转义
StringEscapeUtils – 用于正确处理转义字符,产生正确的Java、JavaScript、HTML、XML和SQL代码; StringUtils – 处理String的核心类,提供了相当多的功能; SystemUtils – 在java.lang.System基础上提供更方便的...
escape.sql
commons-lang3-3.4jar 包括org.apache.commons.lang.StringEscapeUtils类。
org.apache.commons 的 jar 包 12313213215646546521大夫撒旦法的得分的斯蒂芬斯蒂芬
commons-lang可以将html转以后的字符转化成正常显示的字符,具体如下: String str = "“!@#¥%……&——+”";//“!@#¥%……&——+” 中文状态下的标点符号,进过html转换了...
commons-lang3-3.1_jar jar包官方下载的,亲测可用。import org.apache.commons.lang3.StringEscapeUtils;
org.apache.commons.lang.StringEscapeUtils.class org.apache.commons.lang.StringUtils.class org.apache.commons.lang.SystemUtils.class org.apache.commons.lang.UnhandledException.class org.apache....
Apache Commons Lang资源包,下载解压缩后,可获得api文档,源码,jar包,用于开发
commons-lang3-3.4.rar,包括org.apache.commons.lang.StringEscapeUtils类。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。...
15.json数据请求时,返回的error信息使用json字符串 public class JsonErrorRenderFactory implements IErrorRenderFactory { public Render getRender(int errorCode, String view) { if ...
什么是格拉菲亚? Graffias是受和Sinatra启发的Groovy轻量级微型网络框架。...import org.apache.commons.lang.StringEscapeUtils get( ' / ' ) { uri ' index.html ' // public/index.html } post( ' / ' )
<a href="@{ua.pageURI}">@{org.apache.commons.lang.StringEscapeUtils.escapeHtml(ua.name)} Inline Ternary Operator <li>@{ua.hitsTotal} total @{ua.hitsTotal == 1 ? "Hit" : "Hits"}. MVEL Integration The ...
org.apache.commons.lang.StringEscapeUtils.class org.apache.commons.lang.StringUtils.class org.apache.commons.lang.SystemUtils.class org.apache.commons.lang.UnhandledException.class org.apache.commons....
org.apache.commons.lang.StringEscapeUtils.class org.apache.commons.lang.StringUtils.class org.apache.commons.lang.SystemUtils.class org.apache.commons.lang.UnhandledException.class org.apache....