Sql注入由浅及深
有一张学生表
现在需要根据学生名称获取学生的期末考试分数。
1.public static void getStudent(String name) throws ClassNotFoundException {
2. Connection conn = null;
3. Statement stmt = null;
4. ResultSet rs = null;
5. try {
6. Class.forName(JDBC_DRIVER);
7. conn = DriverManager.getConnection(DB_URL, USER, PASS);
8. stmt = conn.createStatement();
9. rs = stmt.executeQuery("select name,score from student where name =' " + name +"'");
10. while (rs.next()) {
11. System.out.println(rs.getString("name") + ":" + rs.getInt("score"));
12. }
13. } catch (SQLException e) {
14. // ignore
15. } finally {
16. if (rs != null) {
17. try {
18. rs.close();
19. } catch (Exception e) {
20. // ignore
21. }
22. }
23. if (stmt != null) {
24. try {
25. stmt.close();
26. } catch (Exception e) {
27. // ignore
28. }
29. }
30. if (conn != null) {
31. try {
32. conn.close();
33. } catch (SQLException e) {
34. // ignore
35. }
36. }
37. }
38. }
1. 请指出上面这段程序存在什么安全风险?并给出具体的测试用例。
2. 请重新编写应用程序,解决上述风险。
用户在输入表单,通过恶意sql欺骗服务器。根源在于动态拼接sql;
此时可以使用preparedstatement。?占位符代表参数就可以简单防止sql注入了。
在一个就是注意封装数据库报错信息。
还有就是涉密信息一定加密处理。