-
-11 28
-
PrepareStatement对象能够很好的防止Statement对象在数据库连接应用中被脚本注入,他的优势在于当SQL语句中要使用参数时,无需"拼接"SQL字符串这样就避免了被注入,当SQL语句中有多个字符串参数时,拼接这条 sql语句时很容易出现错误,,而采用PrepareStatment通过占位符控制,减少了出错的概率和编程的复杂度。
下面是利用SQL漏洞来入侵的实例代码:
String sql="select * from jdbc_test " + “ where jdbc_name=' " + username + " ' and jdbc_desc= ‘ ” +userpw + “ ’ ” ;
if(stmt.executeQuery(sql).next())
{
return true;
}
当用户输入:' or true or ' 时就达到了破解的作用。因为在拼接的时候SQL把这个TRUE当成直接量了。
下面是用 PrepareStatement对象来防止破解:
pstmt=conn.prepareStatement( ”select * from jdbc_test " + "where jdbc_name=? and jdbc_desc =? ");
pstmt.setString(1,username);
pstmt.setString(1,userpw);
if(stmt.executeQuery(sql).next())
{
return true;
}
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
- 评论(2)
发表评论 TrackBack