英文:
How To Mask a password input in java console
问题
我正在Java中构建一个登录系统,我试图遮蔽密码输入以提高安全性,但似乎找不到解决方法。这是我尝试做的:
用户名:
User1
密码:******
这是我的代码,以便让你了解一下:
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
System.out.println("用户名:");
String Username = Input.nextLine();
System.out.println("密码:");
String Password = Input.nextLine();
}
}
英文:
I'm building a login system in Java and I'm trying to mask the password input due to security measures but can't seem to find a way around it. This is what I'm Trying to do:
Username:
User1
Password:******
Here's my code to give you an idea
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner Input = new Scanner (System.in);
System.out.println("Username:");
String Username = Input.nextLine();
System.out.println("Password:");
String Password = Input.nextLine();
}
}
</details>
# 答案1
**得分**: 1
尝试使用Java的readPassword()方法:
它不会用*掩盖字符,但会隐藏我们在控制台上键入的输入。
```java
cnsl = System.console();
char[] pwd = cnsl.readPassword("Password: ");
System.out.println("Password is: " + pwd);
就像这样……
英文:
Try readPassword() method of java
It doesn't mask a * but it hide the input we type to console
cnsl = System.console();
char[] pwd = cnsl.readPassword("Password: ");
System.out.println("Password is: "+pwd);
like this.......
答案2
得分: 1
你可以使用来自 https://docs.oracle.com/javase/7/docs/api/java/io/Console.html 的 Console.readPassword()。
英文:
You can use Console.readPassword() from https://docs.oracle.com/javase/7/docs/api/java/io/Console.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论