zoukankan      html  css  js  c++  java
  • StringEscapeUtils的常用使用,防止SQL注入及XSS注入

    StringEscapeUtils的常用使用,防止SQL注入及XSS注入

    版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/wanghaoqian/article/details/78293631

    引入common-lang-2.4.jar中
    一个方便做转义的工具类,主要是为了防止sql注入,xss注入攻击的功能
    官方参考文档
    StringEscapeUtils.unescapeHtml(sname)
    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) + "%'");
            }
    • 1
    • 2
    • 3
    • 4

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

    System.out.println(StringEscapeUtils.escapeHtml("<a>dddd</a>"));   
    • 1

    输出结果为:<a>dddd</a>

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

    输出为:

    <a>ddd</a>
    • 1

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

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

    输出为:<script>alert(‘111’)</script>
    4.escapeJava/unescapeJava 把字符串转为unicode编码

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

    输出为:用escapeJava方法转义之后的字符串为:/u4E2D/u56FD/u5171/u4EA7/u515A

  • 相关阅读:
    相关书籍下载2
    神奇的null和undefined
    相关书籍下载1
    微信小程序之for循环
    渐变(Gradients)
    模拟今日头条顶部导航菜单
    网格布局之相关特性
    网格布局之合并单元格
    网格布局
    Linux常用命令
  • 原文地址:https://www.cnblogs.com/libin6505/p/10443956.html
Copyright © 2011-2022 走看看