如何在Java 5中屏蔽密码? [英] How to mask a password in Java 5?

查看:152
本文介绍了如何在Java 5中屏蔽密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Java掩盖密码。 Sun java建议了一种屏蔽密码的方法,如下所示。

I am trying to mask a password in Java. Sun java has suggested a way to mask a password as follows.

屏蔽密码

它使用一种简单的方法来做到这一点。

It uses a simple way to do that.

public void run () {
  stop = true;
  while (stop) {
     System.out.print("\010*");
 try {
    Thread.currentThread().sleep(1);
     } catch(InterruptedException ie) {
        ie.printStackTrace();
     }
  }
}

但这种方法有几个缺点。

But this approach has several drawbacks.


  1. 如果用户使用箭头键+删除键,则会显示密码。

  1. If the user uses the arrow keys + delete keys the password gets revealed.

如果用户意外同时按下2个键(极高的打字速度),某些字符不会被屏蔽。

If the user accidentally press 2 keys at the same time (Extremely high typing speed) some characters does not get masked.

你们有没有想过可以获得100%正确掩蔽的方法?

Do you guys think of any way that can get a 100% correct masking?

推荐答案

使用 Console.readPassword()

这篇关于如何在Java 5中屏蔽密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆