-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecureLogin.java
More file actions
40 lines (33 loc) · 1.02 KB
/
Copy pathSecureLogin.java
File metadata and controls
40 lines (33 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* SQL Injection Remediation
*
* SECURE VERSION
*
* Uses a PreparedStatement so user-controlled values are
* handled as parameters rather than executable SQL syntax.
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SecureLogin {
private static final String AUTH_QUERY =
"SELECT 1 FROM users "
+ "WHERE username = ? AND password = ?";
public static boolean authenticate(
Connection connection,
String username,
String password) throws SQLException {
try (PreparedStatement statement =
connection.prepareStatement(AUTH_QUERY)) {
/*
* User-controlled values are bound as data.
*/
statement.setString(1, username);
statement.setString(2, password);
try (ResultSet result = statement.executeQuery()) {
return result.next();
}
}
}
}