commons-lang常用工具类StringEscapeUtils使用

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

}

执行结果:

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

<html>吃饭</html>

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

<html>吃饭</html>

时间: 2024-08-02 01:12:02

commons-lang常用工具类StringEscapeUtils使用的相关文章

commons-lang常用工具类StringEscapeUtils

在apache commons-lang(2.3以上版本)中为我们提供了一个方便做转义的工具类,主要是为了防止sql注入,xss注入攻击的功能.总共提供了以下几个方法: 1.escapeSql 提供sql转移功能,防止sql注入攻击,例如典型的万能密码攻击' ' or 1=1 ' ' StringBuffer sql = new StringBuffer("select key_sn,remark,create_date from tb_selogon_key where 1=1 ")

java开发_org.apache.commons.lang.StringUtils工具类源码

package org.apache.commons.lang; 18 19 import java.util.ArrayList; 20 import java.util.Collection; 21 import java.util.Iterator; 22 import java.util.List; 23 import java.util.Locale; 24 25 import org.apache.commons.lang.text.StrBuilder; 26 27 /** 28

org.apache.commons.lang.StringUtils 工具类的使用

日期工具类 DateUtils(继承org.apache.commons.lang.time.DateUtils类)

/** * */ package com.dsj.gdbd.utils.web; import org.apache.commons.lang3.time.DateFormatUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Matcher; /** * 日期工具类, 继承org.apache.commons.l

简单了解Spring中常用工具类_java - JAVA

文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口,Resource 接口是为了统一各种类型不同的资源而定义的,Spring 提供了若干 Resource 接口的实现类,这些实现类可以轻松地加载不同类型的底层资源,并提供了获取文件名.URL 地址以及资源内容的操作方法 访问文件资源 * 通过 FileSystemResource 以文件系统绝对路径的

Android 常用工具类之SPUtil,可以修改默认sp文件的路径

参考: 1. 利用Java反射机制改变SharedPreferences存储路径    Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import android.app.Activity; import android.content.Context; import android.content.ContextWrapper; import android.content.SharedPreferences; import java.io.

第03章(常用工具类)

1 /***************** 2 ***第三章常用工具类 3 *******知识点: 4 **************1.系统相关 5 ******************1.1 System类 6 ******************1.2 Runtime类 7 ******************1.3 输入类 8 **************************1.3.1 Scanner类 9 **************************1.3.2 Buffered

Java常用工具类集合

数据库连接工具类 仅仅获得连接对象 ConnDB.java package com.util; import java.sql.Connection; import java.sql.DriverManager; /** * 数据库连接工具类——仅仅获得连接对象 * */ public class ConnDB { private static Connection conn = null; private static final String DRIVER_NAME = "com.mysql

Android常用工具类(收藏)

Android常用工具类 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括(HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.PreferencesUtils.JSONUtils.FileUtils.ResourceUtils.StringUtils.ParcelUtils.RandomUtils.ArrayUtils.ImageUtils.ListUtils.MapUtils.ObjectUtils.S