防止SQL注入,字符串过滤关键字符
public class SQLFilterTest {
public static void main(String[] args) {
String temp = "asdfasd哈哈哈哈哈.sdfjalsd.";
System.out.println(doSQLFilter(temp));
}
//比较笨的过滤sql字符串
public static String doSQLFilter(String str){
str=str.replaceAll("\\.","。");
str=str.replaceAll(":",":");
str=str.replaceAll(";",";");
str=str.replaceAll("&","&");
str=str.replaceAll("<","<");
str=str.replaceAll(">",">");
str=str.replaceAll("'","'");
str=str.replaceAll("\"","“");
str=str.replaceAll("--","--");
str=str.replaceAll("/","/");
str=str.replaceAll("%","%");
str=str.replaceAll("\\+", "+");
str=str.replaceAll("\\(", "(");
str=str.replaceAll("\\)", ")");
return str;
}
}
本文介绍了一种简单的SQL注入防护方法,通过过滤特定字符来避免恶意输入。这种方法虽然不够完善,但对于初学者理解如何保护应用程序免受SQL注入攻击有一定的帮助。
7131

被折叠的 条评论
为什么被折叠?



