[英]Hide input on command line
我知道像 Git 和其他命令行界面能夠隱藏用戶的輸入(對密碼有用)。 有沒有辦法在 Java 中以編程方式執行此操作? 我正在從用戶那里獲取密碼輸入,我希望他們的輸入被“隱藏”在該特定行上(但不是在所有行上)。 這是我的代碼(盡管我懷疑它會有所幫助......)
try (Scanner input = new Scanner(System.in)) {
//I'm guessing it'd probably be some property you set on the scanner or System.in right here...
System.out.print("Please input the password for " + name + ": ");
password = input.nextLine();
}
試試java.io.Console.readPassword
。 你必須至少運行Java 6。
/**
* Reads a password or passphrase from the console with echoing disabled
*
* @throws IOError
* If an I/O error occurs.
*
* @return A character array containing the password or passphrase read
* from the console, not including any line-termination characters,
* or <tt>null</tt> if an end of stream has been reached.
*/
public char[] readPassword() {
return readPassword("");
}
請注意,這不適用於Eclipse控制台。 您必須從真正的控制台/ shell /終端/提示符運行該程序才能測試它。
是的,可以做到。 這稱為命令行輸入掩蔽。 您可以輕松實現此功能。
您可以使用單獨的線程在輸入時擦除回顯的字符,並用星號替換它們。 這是使用下面顯示的EraserThread類完成的
import java.io.*;
class EraserThread implements Runnable {
private boolean stop;
/**
*@param The prompt displayed to the user
*/
public EraserThread(String prompt) {
System.out.print(prompt);
}
/**
* Begin masking...display asterisks (*)
*/
public void run () {
stop = true;
while (stop) {
System.out.print("\010*");
try {
Thread.currentThread().sleep(1);
} catch(InterruptedException ie) {
ie.printStackTrace();
}
}
}
/**
* Instruct the thread to stop masking
*/
public void stopMasking() {
this.stop = false;
}
}
使用這個線程
public class PasswordField {
/**
*@param prompt The prompt to display to the user
*@return The password as entered by the user
*/
public static String readPassword (String prompt) {
EraserThread et = new EraserThread(prompt);
Thread mask = new Thread(et);
mask.start();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String password = "";
try {
password = in.readLine();
} catch (IOException ioe) {
ioe.printStackTrace();
}
// stop masking
et.stopMasking();
// return the password entered by the user
return password;
}
}
本鏈接詳細討論。
JLine 2可能會引起關注。 除了字符屏蔽外,它還提供命令行完成,編輯和歷史記錄功能。 因此,它對基於CLI的Java工具非常有用。
掩蓋你的輸入:
String password = new jline.ConsoleReader().readLine(new Character('*'));
有 :
Console cons;
char[] passwd;
if ((cons = System.console()) != null &&
(passwd = cons.readPassword("[%s]", "Password:")) != null) {
...
java.util.Arrays.fill(passwd, ' ');
}
但我不認為這適用於像Eclipse這樣的IDE,因為程序是作為后台進程運行的,而不是帶有控制台窗口的頂級進程。
另一種方法是使用隨附的actionPerformed
方法使用JPasswordField
:
public void actionPerformed(ActionEvent e) {
...
char [] p = pwdField.getPassword();
}
Console類有一個方法readPassword()
可以解決您的問題。
這是一個使用console.readPassword(...);
的示例在 IDE 中。 我使用 Netbeans。 注意:在您的 IDE 中,將使用Scanner
,它會顯示密碼。,在控制台中,將使用console.readPassword(..)
,它不會顯示密碼..
public static void main(String[] args) {
//The jar needs to be run from the terminal for console to work.
Scanner input = new Scanner(System.in);
Console console = System.console();
String username = "";
String password = "";
if (console == null)
{
System.out.print("Enter username: ");
username = input.nextLine();
System.out.print("Enter password: ");
password = input.nextLine();
}
else
{
username = console.readLine("Enter username: ");
password = new String(console.readPassword("Enter password: "));
}
//I use the scanner for all other input in the code!
//I do not know if there are any pitfalls associated with using the Scanner and console in this manner!
}
注意:我不知道以這種方式使用掃描儀和控制台是否有任何陷阱!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.