如何在java中检测sql注入

ru9i0ody  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(479)

我有一个用java编程的分类器来检测和阻止sql注入,这个分类器通过数据集的“两个双引号之间”的语句来判断请求是sql注入还是否,我需要把这个应用程序转换成基于web的,因此,当用户输入example 3或2=2时,应用程序必须从url而不是像我说的那样从我的应用程序中读取这个句子(句子“在两个双引号之间”)
例如:

String examplesql = "1' or 1=1";
String outputsql = nb.predict(examplesql);
System.out.format("The Traffic \"%s\" was classified as \"%s\".%n", examplesql, outputsql);
eqzww0vc

eqzww0vc1#

使用 PreparedStatement 而不是手动检查。这意味着驱动程序将自动转义您给它的字符串,sql注入尝试将失败,它们提供的任何数据都将传递到数据库字段。看到了吗https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html 以及https://docs.oracle.com/javase/8/docs/api/java/sql/preparedstatement.html 有关如何使用它的详细信息。
要获取已发送的参数,可以使用

// create connection
String passed = request.getParameter("data");
PreparedStatement statement = connection.prepareStatement("INSERT INTO table (field1) VALUES (?)");
statement.setString(1, passed);
statement.executeQuery();

相关问题