public interface CallbackHandler
一个应用程序实现了CallbackHandler
并把它传递给底层的安全服务,可以检索指定的认证数据的应用程序进行交互,如用户名和密码,或者显示特定的信息,如错误和警告消息。
callbackhandlers正在应用程序依赖的方式实现。例如,一个应用程序的图形用户界面(图形用户界面)的实现可能弹出窗口提示请求的信息或显示错误消息。一个实现也可以选择从一个备用源获取请求的信息,而不要求最终用户。
基本安全服务通过个人回调的CallbackHandler
使不同类型信息的请求。实施的CallbackHandler
决定如何检索和显示的信息取决于回调函数传递给它的。例如,如果基础服务需要用户名和密码的用户进行身份验证,它使用一个NameCallback
和PasswordCallback
。的CallbackHandler
可以选择提示用户名和密码串,或提示在一个单一的窗口。
默认CallbackHandler
类的实现可以通过设置的auth.login.defaultCallbackHandler
安全属性指定的值。
如果安全属性设置为一个CallbackHandler
实现类的完全限定名,然后LoginContext
将加载指定的CallbackHandler
并把它传递给潜在的loginmodules。的LoginContext
只加载默认处理程序如果它没有提供一个。
所有的默认处理程序实现必须提供一个公共的零参数构造函数。
security properties
void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
检索或显示在所提供的回调请求的信息。
的handle
方法实施检查实例(S)的Callback
对象(S)通过检索或显示要求的信息。下面的例子是提供帮助说明一个handle
实施方法可能看起来像。此示例代码仅用于指导。许多细节,包括适当的错误处理,都被排除在外。
public void handle(Callback[] callbacks)
throws IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof TextOutputCallback) {
// display the message according to the specified type
TextOutputCallback toc = (TextOutputCallback)callbacks[i];
switch (toc.getMessageType()) {
case TextOutputCallback.INFORMATION:
System.out.println(toc.getMessage());
break;
case TextOutputCallback.ERROR:
System.out.println("ERROR: " + toc.getMessage());
break;
case TextOutputCallback.WARNING:
System.out.println("WARNING: " + toc.getMessage());
break;
default:
throw new IOException("Unsupported message type: " +
toc.getMessageType());
}
} else if (callbacks[i] instanceof NameCallback) {
// prompt the user for a username
NameCallback nc = (NameCallback)callbacks[i];
// ignore the provided defaultName
System.err.print(nc.getPrompt());
System.err.flush();
nc.setName((new BufferedReader
(new InputStreamReader(System.in))).readLine());
} 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(readPassword(System.in));
} else {
throw new UnsupportedCallbackException
(callbacks[i], "Unrecognized Callback");
}
}
}
// Reads user password from given input stream.
private char[] readPassword(InputStream in) throws IOException {
// insert code to read a user password from the input stream
}
callbacks
-
Callback
对象由一个基本的安全服务,包含要检索或显示的信息提供了一个数组。
IOException
如果输入或输出错误发生。
UnsupportedCallbackException
-如果该方法的实现不支持一个或更多的
callbacks
参数中指定的回调函数。
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.