防止SQL注入-字符串过滤法
防止SQL注入-字符串过滤法
例如
SELECT * FROM user_table WHERE username= ‘’ or 1 = 1 – —and password=’’
危害
通过SQL语句,实现无帐号登录,甚至篡改数据库。
方案一:通常预编译处理。setParem
sql注入只对sql语句的准备(编译)过程有破坏作用 而PreparedStatement已经准备好了,执行阶段只是把输入串作为数据处理, 而不再对sql语句进行解析,准备,因此也就避免了sql注入问题.
方案二:字符串过滤 js处理
public static boolean sql_inj(String str){ String inj_str = "|and|exec|insert|select|delete|update| count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+|,"; String inj_stra[] = split(inj_str,"|"); for (int i=0 ; i < inj_stra.length ; i++ ){ if (str.indexOf(inj_stra[i])>=0){ return true; } } return false; }
java处理
public static boolean sql_inj(String str){ String inj_str = "and,exec,insert,select,delete,update, count,*,%,chr,mid,master,truncate,char,declare,;,or,-,+,"; String inj_stra[] = inj_str.split(","); for (int i=0 ; i < inj_stra.length ; i++ ){ if (str.indexOf(inj_stra[i])>=0){ return true; } } return false; }
获取参数后对参数过滤处理,防止sql注入;
参考
https://www.cnblogs.com/pressur/p/11226392.html