To prevent hackers from exploiting your Java website:
NEVER do this (manually concatenate SQL):
String sql = "select 1 from user where userId='" + userId + "' and password='" + password + "'";
Instead do this (use prepared statements):
String sql = "select 1 from user where userId = ? and password = ?"; PreparedStatement prepStmt = con.prepareStatement(sql); prepStmt.setString(1, userId); prepStmt.setString(2, password);
When you use prepared statements it passes the parameters directly to the database independent of the SQL statement so that the hacker never has a chance to alter them.
PreparedStatement doesn't escape characters. It doesn't give attacker any chance to alternate your SQL statement.
ReplyDeleteRight you are. I looked it up and in most databases the parameters are passed in directly to the database unaltered, which never gives the hacker a chance to alter them. It seems that a few databases actually do escape the parameters but these are in the minority.
ReplyDeleteI've corrected my post.
ReplyDelete