简体   繁体   English

在eclipse java中从控制台屏蔽密码输入字段

[英]masking password input field from console in eclipse java

I have been using JDK 1.5 to run my code. 我一直在使用JDK 1.5来运行我的代码。
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. 我尝试过使用console.readPassword() ,但它在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/ 从没有命令行API进行密码屏蔽时,本文可能会有用: http//java.sun.com/developer/technicalArticles/Security/pwordmask/

As a side note, wasn't the Console class only added in Java 1.6? 作为旁注,是不是只在Java 1.6中添加了Console类? Are you getting any errors in Eclipse? 你在Eclipse中遇到任何错误吗?

Try using swing: 尝试使用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. readPassword()使用本机代码关闭echo。 Likely it will be platform dependent. 可能它将取决于平台。 So I don't think you will get an easy purely java solution. 所以我认为你不会得到一个简单的纯Java解决方案。 Why not run the program in real console instead? 为什么不在真正的控制台中运行该程序呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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