简体   繁体   中英

masking password input field from console in eclipse java

I have been using JDK 1.5 to run my code.
Does anyone know of a way to mask a password from console input?

I've tried using console.readPassword() , but it doesn't work in eclipse IDE. A full example might help me actually.

This article might be of use, from when there wasn't a command-line API to do password masking: http://java.sun.com/developer/technicalArticles/Security/pwordmask/

As a side note, wasn't the Console class only added in Java 1.6? Are you getting any errors in Eclipse?

Try using swing:

final String password, message = "Enter password";
if( System.console() == null ) 
{ // inside IDE like Eclipse or NetBeans
  final JPasswordField pf = new JPasswordField(); 
  password = JOptionPane.showConfirmDialog( null, pf, message,
    JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.QUESTION_MESSAGE ) == JOptionPane.OK_OPTION ? 
      new String( pf.getPassword() ) : "";
}
else 
  password = new String( System.console().readPassword( "%s> ", message ) );

readPassword() uses native code to turn echo off. Likely it will be platform dependent. So I don't think you will get an easy purely java solution. Why not run the program in real console instead?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM