请教如何才能做到在控制台里输入密码显示特殊符号或者不显示

06-08-27 popufig

RT

如何才能做到在控制台里输入密码时显示特殊符号或者完全不显示呢?

记得在使用weblogic时提示输入密码就是不会显示用户的输入,看了它的代码应该是使用了PasswordCallback,也就是JAAS,PasswordCallback的构造函数的确有一个boolean的参数可以指定是否显示密码,然后有一个boolean isEchoOn()方法暴露这个参数,然后就写了一个最简单的,当然new PasswordCallback时使用了false,结果还是不行。

我的CallbackHandler是这么处理PasswordCallback的:

System.err.println("pwCallback.getPrompt()");

System.err.flush();

pwCallback.setPassword(new BufferedReader (new InputStreamReader(System.in))).readLine().toCharArray());

在网上搜到一篇代码:http://as400bks.rochester.ibm.com/html/as400/v4r5/ic2924/info/java/rzaha/SampleThreadSubjectLogin.java

其中是这么处理的:

/**

* Handle the given name callback.

*

* <p> First check to see if a password has been passed

* in on the constructor. If so, assign it to the

* callback and bypass the prompt.

*

* <p> If a value has not been preset, attempt to prompt

* for the password using standard input and output.

*

* @param c

* The PasswordCallback.

*

* @exception java.io.IOException

* If an input or output error occurs.

*

*/

private void handlePasswordCallback(PasswordCallback c) throws IOException {

// Check for cached value

if (password_ != null) {

c.setPassword(password_.toCharArray());

return;

}

// No preset value; attempt stdin/out

// Note - Not for production use.

// Password is not concealed by standard console I/O

if (c.isEchoOn())

c.setPassword(

stdIOReadName(c.getPrompt(), 10).toCharArray());

else

{

// Note - Password is not concealed by standard console I/O

c.setPassword(stdIOReadName(c.getPrompt(), 10).toCharArray());

}

}

/**

* Displays the given string using standard output,

* followed by a space to separate from subsequent

* input.

*

* @param prompt

* The text to display.

*

* @exception IOException

* If an input or output error occurs.

*

*/

private void stdIOPrompt(String prompt) throws IOException {

System.out.print(prompt + ' ');

System.out.flush();

}

/**

* Reads a String from standard input, terminated at

* <i>maxLength</i> or by a newline.

*

* @param prompt

* The text to display to standard output immediately

* prior to reading the requested value.

*

* @param maxLength

* Maximum length of the String to return.

*

* @return

* The entered string. The value returned will

* not contain leading or trailing whitespace

* and is converted to uppercase.

*

* @exception IOException

* If an input or output error occurs.

*

*/

private String stdIOReadName(String prompt, int maxLength) throws IOException {

stdIOPrompt(prompt);

String s =

(new BufferedReader

(new InputStreamReader(System.in))).readLine().trim();

if (s.length() > maxLength)

s = s.substring(0,maxLength);

return s.toUpperCase();

}

我看不出什么大的区别,哪位达人做过类似的东西指点一下,谢谢!!

banq
2006-08-28 10:41

使用JS吧?

popufig
2006-08-28 20:20

我是想屏蔽控制台的输入

console,命令控制台

目前找到一个做法是用一个线程不断地运行 System.out.print("\b*"); 感觉不怎么好。