下面的Java小服務程序代碼,用於執行一個登錄功能,示出了通過接受用戶輸入的脆弱性,而不執行適當的輸入驗證或逸出元字符:
String sql = "select * from user where username='" + username +"' and password='" + password + "'";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
loggedIn = true;
out.println("Successfully logged in");
} else {
out.println("Username and/or password not recognized");
}
================
現在請告訴我怎麼才能修改此代碼,使其免受SQL注入自由
+1對於xkcd引用 –