当前位置: 代码迷 >> Java Web开发 >> 求正则过滤sql中的要害
  详细解决方案

求正则过滤sql中的要害

热度:6248   发布时间:2013-02-25 21:15:43.0
求正则过滤sql中的关键
正则过滤sql中的关键字
sql语句中不能包含 update|drop|truncate|alter|set|delete|insert|exec

------解决方案--------------------------------------------------------
Java code
public static void main(String[] args) {        String reg="[update|drop|truncate|alter|set|delete|insert|exec]*";        String str="update xxx from fff";        String str2="drop xx from dd.";        Pattern p =Pattern.compile(reg);        //        Matcher m=p.matcher(str);        System.out.println(m.matches());        //        m=p.matcher(str2);        System.out.println(m.matches());            }
------解决方案--------------------------------------------------------
[^update]|[^drop]|[^truncate]|[^alter]|[^set]|[^delete]|[^insert]|[^exec]* 手写的没有试。。你试试吧
------解决方案--------------------------------------------------------
Java code
    public static void main(String[] args) throws Exception{        String sql = "update set tab s = 'a'";        Matcher m = Pattern.compile("[update|drop|truncate|alter|set|delete|insert|exec]").matcher(sql);        System.out.println(!m.find());    }
------解决方案--------------------------------------------------------
或者:.*?[update|drop|truncate|alter|set|delete|insert|exec].*?
------解决方案--------------------------------------------------------
你的意思是不是在客户端验证(JAVASCRIPT的正则?)
------解决方案--------------------------------------------------------
javascript校验:

JScript code
function checkSQL(sql){  var allowExt = "update|drop|truncate|alter|set|delete|insert|exec|"; if(allowExt != 0 && allowExt.indexOf(sql + "|") == -1){                  alert("【sql语句中不能包含 update|drop|truncate|alter|set|delete|insert|exec】!");                  return false;              } }
------解决方案--------------------------------------------------------
用jdbc的preparedStatement参数处理,就不用担心关键字的问题啦。。

  相关解决方案