Java Reference
In-Depth Information
System.exit(1);
}
String username = c.readLine("Enter your user name: ");
String password = c.readLine("Enter your password: ");
if (!verify(username, password)) {
throw new SecurityException("Invalid Credentials");
}
// ...
}
// Dummy verify method, always returns true
private static final boolean verify(String username,
String password) {
return true;
}
}
Compliant Solution
This compliant solution uses the Console.readPassword() method to obtain the pass-
word from the console.
Click here to view code image
class Password {
public static void main (String args[]) throws IOException {
Console c = System.console();
if (c == null) {
System.err.println("No console.");
System.exit(1);
}
String username = c.readLine("Enter your user name: ");
char[] password = c.readPassword("Enter your password: ");
if (!verify(username, password)) {
throw new SecurityException("Invalid Credentials");
}
// Clear the password
Arrays.fill(password, ' ');
}
Search WWH ::




Custom Search