问题是这是一个JAAS登录应用程序。下面的方法使用Java的Callback类,它有点让我烦恼。因为它与Callback类(以及其他东西)标准连接(
) JTextfield username = Class.getText();
无效。欢迎您的想法:
public boolean login() throws LoginException {
// prompt for a user name and password
if (callbackHandler == null)
throw new LoginException("Error: no CallbackHandler available " +
"to garner authentication information from the user");
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("user name: ");
callbacks[1] = new PasswordCallback("password: ", false);
try {
callbackHandler.handle(callbacks);
username = ((NameCallback)callbacks[0]).getName();
char[] tmpPassword = ((PasswordCallback)callbacks[1]).getPassword();
if (tmpPassword == null) {
// treat a NULL password as an empty password
tmpPassword = new char[0];
}
password = new char[tmpPassword.length];
System.arraycopy(tmpPassword, 0,
password, 0, tmpPassword.length);
((PasswordCallback)callbacks[1]).clearPassword();
} catch (java.io.IOException ioe) {
throw new LoginException(ioe.toString());
} catch (UnsupportedCallbackException uce) {
throw new LoginException("Error: " + uce.getCallback().toString() +
" not available to garner authentication information " +
"from the user");
}
. . .
答案 0 :(得分:0)
感谢Hovercraft Full Of Eels指向我正确的方向,我能够弄明白。下面是我的Callback处理程序中的正确代码(不是我最初的登录模块):
public void handle(Callback[] callbacks)throws IOException, UnsupportedCallbackException
{
for (int i = 0; i < callbacks.length; i++)
{
if
{
. . . .
}
else if (callbacks[i] instanceof NameCallback)
{
// prompt the user for a username
NameCallback nc = (NameCallback)callbacks[i];
System.err.print(nc.getPrompt());
System.err.flush();
nc.setName(Video_Game_Store.usrName.getText());
}
else if (callbacks[i] instanceof PasswordCallback)
{
// prompt the user for sensitive information
PasswordCallback pc = (PasswordCallback)callbacks[i];
System.err.print(pc.getPrompt());
System.err.flush();
pc.setPassword(Video_Game_Store.psWord.getText().toCharArray());
}
else
{
throw new UnsupportedCallbackException
(callbacks[i], "Unrecognized Callback");
}
}
}