隐藏命令行上的input

我知道像Git这样的命令行界面能够隐藏用户的input(对密码有用)。 有没有一种方法可以在Java中进行编程? 我正在从用户那里input密码,我希望他们的input被隐藏在特定的行上(但不是全部)。 这是我的代码(尽pipe我怀疑这会有帮助…)

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 。 不过至less要运行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/terminal/提示符运行程序才能够testing它。

可以做。 这被称为命令行input掩码。 您可以轻松实现这一点。

您可以使用一个单独的线程来擦除正在input的回显字符,并用星号replace它们。 这是使用下面显示的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可能是有趣的。 除了字符掩码之外,它还提供命令行完成,编辑和历史loggingfunction。 因此它对于基于CLI的Java工具非常有用。

要掩盖您的input:

 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方法在swing中使用JPasswordField

 public void actionPerformed(ActionEvent e) { ... char [] p = pwdField.getPassword(); } 

资源

类控制台有一个方法readPassword()可以解决您的问题。

如果您正在处理Java字符数组(例如您从控制台读取的密码字符),则可以使用以下Ruby代码将其转换为JRubystring:

 # GIST: "pw_from_console.rb" under "https://gist.github.com/drhuffman12" jconsole = Java::java.lang.System.console() password = jconsole.readPassword() ruby_string = '' password.to_a.each {|c| ruby_string << c.chr} # .. do something with 'password' variable .. puts "password_chars: #{password_chars.inspect}" puts "password_string: #{password_string}" 

另见“ https://stackoverflow.com/a/27628738/4390019 ”和“ https://stackoverflow.com/a/27628756/4390019