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 = new StringBuffer("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("<A>dddd</A>"));   

输出结果为:

<a>dddd</a>

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

输出为:

<A>ddd</A>

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

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

输出为:

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

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

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

输出为:

用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)));
}
执行结果: &lt;html&gt;吃饭&lt;/html&gt; <html>吃饭</html> &lt;html&gt;吃饭&lt;/html&gt; <html>吃饭</html>

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

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

  1. commons-lang常用工具类StringEscapeUtils

    原文:https://my.oschina.net/mousai/blog/88832 在apache commons-lang(2.3以上版本)中为我们提供了一个方便做转义的工具类,主要是为了防止s ...

  2. Apache Commons 常用工具类整理

    其实一直都在使用常用工具类,只是从没去整理过,今天空了把一些常用的整理一下吧 怎么使用的一看就明白,另外还有注释,最后的使用pom引入的jar包 public class ApacheCommonsT ...

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

    /** * */ package com.dsj.gdbd.utils.web; import org.apache.commons.lang3.time.DateFormatUtils; impor ...

  4. commons-lang3-3.2.jar中的常用工具类的使用

    这个包中的很多工具类可以简化我们的操作,在这里简单的研究其中的几个工具类的使用. 1.StringUtils工具类 可以判断是否是空串,是否为null,默认值设置等操作: /** * StringUt ...

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

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口, ...

  6. Maven基础&&Spring框架阶段常用工具类整理

    常用工具类 1.密码加密工具类: package com.itheima.utils; import java.security.MessageDigest; import sun.misc.BASE ...

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

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

  8. Java_常用工具类收集

    一.日期工具类 package com.ebd.application.common.utils; import java.sql.Timestamp; import java.text.DateFo ...

  9. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

随机推荐

  1. 解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has not finished: run 'cleanup' if it was interrupted Please execute the 'Cleanup' command.

    解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has no ...

  2. ARGB,RGB颜色值表示

    转载请注明出处:http://blog.csdn.net/wei_chong_chong/article/details/50831493 今天自己定义一个控件.设置背景颜色时犯难了 如今就来总结一下 ...

  3. Oracle - 查询语句 - 分组函数

    /* 分组函数 不能再select子句中出现普通的列,除非这个列在group by中给出 所有的空值都会被分为一组 分组过滤 SELECT FROM WHERE GROUPBY HAVING ORDE ...

  4. c# 32位机和64位机 读取Excel内容到DataSet

    // ----------------------32位机 //注释说明 //ExclePath 为Excel路径 批号 是指Excel文件中某一列必填项 public static DataSet ...

  5. centos7和redhat7的比特币环境搭建

    比特币版本是 bitcoin-0.12 问题1: [root@localhost bitcoin-master]# ./autogen.sh  which: no autoreconf in (/us ...

  6. HTTP网络请求原理 (三) 简单模拟HTTP服务器

    HTTP实际上是基于TCP的应用层协议,它在更高的层次封装了TCP的使用细节,是网络请求操作更为易用. TCP连接是因特网上基于流的可靠连接,它为HTTP提供了一条可靠的比特传输管道. 从TCP连接一 ...

  7. [NOI 2012] 美食节

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2879 [算法] 首先 , 将每种食物建一个点 , 将每位厨师做的每一道菜建一个点 建 ...

  8. bzoj3668 [Noi2014]起床困难综合症——贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3668 一开始想着倒序推回去看看这一位能不能达到来着,因为这样好中途退出(以为不这样会T): ...

  9. char-rnn-tensorflow源码解析及结构流程分析

    char-rnn-tensorflow由飞飞弟子karpathy编写,展示了如何用tensorflow来搭建一个基本的RNN(LSTM)网络,并进行基于char的seq2seq进行训练. 数据读取部分 ...

  10. FB 对话框接口

    官方文档: https://developers.facebook.com/docs/dialogs FB的对话框主要是调用 FB.ui(); 方法. 有问题上官网看文档是最好的. 1. send D ...