【Javaweb】后台的字符串转义,入库之前记得先对字符串转义防止sql注入问题

来源:互联网 发布:淘宝智能版全屏店招 编辑:程序博客网 时间:2024/05/16 06:19
  1. public class EncodeDecodeUtil {  
  2.     /* 
  3.      * 所有文本框,都必须先经过htmlEncode再入库(防sql注入) 
  4.      * 所有显示,如果是单行文本框无须经过htmlDecode 
  5.      * 定义:单行文本框输入的内容都是没有格式的内容 推定:所有在单行文本框输入html标签的都是不良行为 
  6.      * 带编辑器的多行文本框的内容应该经过htmlDecode,保持其固有格式 
  7.      * 注意到script已经被强力转义不再decode还原,因此给你有格式又怎么样? 
  8.      */  
  9.     public static String htmlEncode(String html) {  
  10.         if (html != null) {  
  11.             // 必要  
  12.             return html  
  13.                     .replaceAll("&""&")  
  14.                     .replaceAll("<""&lt;")  
  15.                     .replaceAll(">""&gt;")  
  16.                     .replaceAll("\"""&quot;")  
  17.                     .replaceAll(" ""&nbsp;")  
  18.                     // 一些可能出现问题的半角符号,还是要转义的。  
  19.                     // ,+、*、|、\、?等符号在正则表达示中有相应的不同意义。要写成\\,\\+之类  
  20.                     .replaceAll("[+]""+")  
  21.                     .replaceAll("-""-")  
  22.                     // \最为特殊,对于正则表达式敏感也对于java字符串敏感,其在replace的写法为\\\\  
  23.                     .replaceAll("\\\\", "").replaceAll("/", "/")  
  24.                     .replaceAll("[*]""*")  
  25.                     .replaceAll(":"":")  
  26.                     .replaceAll("[?]""?")  
  27.                     .replaceAll("[|]""|")  
  28.                     // 非必要,但是还是转义吧  
  29.                     .replaceAll("¢""&cent;").replaceAll("£""&pound;")  
  30.                     .replaceAll("¥""&yen;").replaceAll("§""&sect;")  
  31.                     .replaceAll("©""&copy;").replaceAll("®""&reg;")  
  32.                     .replaceAll("×""&times;").replaceAll("÷""&divide;");  
  33.         } else {  
  34.             //避免控制入库导致报错,如果遇到空值,先替换成一个空格  
  35.             //主要是防止表单验证时,某些浏览器因为兼容性问题,取不到表单某些项的值  
  36.             return " ";  
  37.         }  
  38.     }  
  39.     // 这里与htmlEncode成轴对称!正如汇编语言,变量的出栈入栈互成对子才能保护现场一样  
  40.     // 写得我很蛋疼,手工把它反过来了  
  41.     public static String htmlDecode(String html) {  
  42.         if (html != null) {  
  43.             return html.replaceAll("&divide;""÷")  
  44.                     .replaceAll("&times;""×")  
  45.                     .replaceAll("&reg;""®")  
  46.                     .replaceAll("&copy;""©")  
  47.                     .replaceAll("&sect;""§")  
  48.                     .replaceAll("&yen;""¥")  
  49.                     .replaceAll("&pound;""£")  
  50.                     .replaceAll("&cent;""¢")  
  51.                     // “非必要,但是还是转义吧”部分  
  52.                     .replaceAll("|""|").replaceAll("?""?")  
  53.                     // 在replace中,在第一个位置要写成[*],在第二个位置就不用这样写  
  54.                     // 因为这里不是正则表达式  
  55.                     .replaceAll(":"":").replaceAll("*""*")  
  56.                     .replaceAll("/""/").replaceAll("\""\\\\")  
  57.                     .replaceAll("-""-")  
  58.                     .replaceAll("+""+")  
  59.                     // “一些可能出现问题的半角符号,还是要转义的。”部分  
  60.                     .replaceAll("&nbsp;"" ").replaceAll("&quot;""\"")  
  61.                     .replaceAll("&gt;"">").replaceAll("&lt;""<")  
  62.                     .replaceAll("&amp;""&");  
  63.                     .replaceAll("<script""&lt;script")  
  64.             // “必要”部分  
  65.   
  66.         } else {  
  67.             return null;  
  68.         }  
  69.     }  

0 0
原创粉丝点击