[英]Password Masking in Java with Scanner
I'm a high school student learning Java and I want to know how to change input text automatically into an asterisk in Scanner.我是一名正在学习 Java 的高中生,我想知道如何在 Scanner 中将输入文本自动更改为星号。 This is for a simple log-in system I have made for a project.
这是我为一个项目制作的一个简单的登录系统。 My code is
我的代码是
Scanner scan = new Scanner(System.in);
boolean correctLogin = false;
String username;
String password;
String enteredUsername;
String enteredPassword;
while(correctLogin != true){
System.out.println("Enter Username: ");
enteredUsername = scan.nextLine();
System.out.println("Enter Password: ");
enteredPassword = scan.nextLine();
if(enteredUsername.equals("username") && enteredPassword.equals("passw00rd")){
System.out.println("You have entered the correct login info");
correctLogin = true;
break;
}
else{
System.out.println("Your login info was incorrect, please try again");
}
}
System.out.println("You are now logged in, good job!");
I want it so that when I type the password, it will automatically change into an asterisk.我想要它,这样当我输入密码时,它会自动变成星号。
try with this for password read: 尝试使用此密码读取:
Console console = System.console();
if(console != null){
console.readPassword("Enter Password: ");
}
I also had the same issue with my console java application and I also do not want to display password in my IDE for security reasons.我的控制台 java 应用程序也有同样的问题,出于安全原因,我也不想在我的 IDE 中显示密码。 So, to find the bug I had to debug against the productive environment.
因此,为了找到我必须针对生产环境进行调试的错误。 Here is my solution that works for me in IntelliJ IDEA:
这是我在 IntelliJ IDEA 中适用的解决方案:
public static String getPassword() {
String password;
Console console = System.console();
if (console == null) {
password = getPasswordWithoutConsole("Enter password: ");
} else {
password = String.valueOf(console.readPassword("Enter password: "));
}
return password;
}
public static String getPasswordWithoutConsole(String prompt) {
final JPasswordField passwordField = new JPasswordField();
return JOptionPane.showConfirmDialog(
null,
passwordField,
prompt,
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION ? new String(passwordField.getPassword()) : "";
}
I am not sure if i get your question correctly, but still I will try to explain of what I understood. 我不确定我是否能正确回答您的问题,但是我仍然会尝试解释我的理解。
To ensure that you see *** on the user screen you need to have some kind of a User interface written in HTML. 为了确保在用户屏幕上看到***,您需要使用HTML编写某种用户界面。 I think in this case you are running your code in eclipse and via some kind of a main method.
我认为在这种情况下,您正在Eclipse中通过某种主要方法运行代码。 If that is the case then as Vince mentioned there is no benefit of ** since the letters would appear in the console.
如果是这种情况,那么正如文斯提到的那样,**不会带来任何好处,因为字母会出现在控制台中。
What I would recommend is look for something basic web application tutorial and you would have more idea on how it works then. 我建议您寻找一些基本的Web应用程序教程,然后您将对它的工作方式有更多的了解。
HTH 高温超导
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.