Stay organized with collections
Save and categorize content based on your preferences.
CallbackHandler
interface CallbackHandler
An application implements a CallbackHandler
and passes it to underlying security services so that they may interact with the application to retrieve specific authentication data, such as usernames and passwords, or to display certain information, such as error and warning messages.
CallbackHandlers are implemented in an application-dependent fashion. For example, implementations for an application with a graphical user interface (GUI) may pop up windows to prompt for requested information or to display error messages. An implementation may also choose to obtain requested information from an alternate source without asking the end user.
Underlying security services make requests for different types of information by passing individual Callbacks to the CallbackHandler
. The CallbackHandler
implementation decides how to retrieve and display information depending on the Callbacks passed to it. For example, if the underlying service needs a username and password to authenticate a user, it uses a NameCallback
and PasswordCallback
. The CallbackHandler
can then choose to prompt for a username and password serially, or to prompt for both in a single window.
A default CallbackHandler
class implementation may be specified by setting the value of the auth.login.defaultCallbackHandler
security property.
If the security property is set to the fully qualified name of a CallbackHandler
implementation class, then a LoginContext
will load the specified CallbackHandler
and pass it to the underlying LoginModules. The LoginContext
only loads the default handler if it was not provided one.
All default handler implementations must provide a public zero-argument constructor.
Summary
Public methods |
abstract Unit |
Retrieve or display the information requested in the provided Callbacks.
|
Public methods
handle
abstract fun handle(callbacks: Array<Callback!>!): Unit
Retrieve or display the information requested in the provided Callbacks.
The handle
method implementation checks the instance(s) of the Callback
object(s) passed in to retrieve or display the requested information. The following example is provided to help demonstrate what an handle
method implementation might look like. This example code is for guidance only. Many details, including proper error handling, are left out for simplicity.
<code>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
}
</code>
Parameters |
callbacks |
Array<Callback!>!: an array of Callback objects provided by an underlying security service which contains the information requested to be retrieved or displayed. |
Exceptions |
java.io.IOException |
if an input or output error occurs.
|
javax.security.auth.callback.UnsupportedCallbackException |
if the implementation of this method does not support one or more of the Callbacks specified in the callbacks parameter. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# CallbackHandler\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nCallbackHandler\n===============\n\n```\ninterface CallbackHandler\n```\n\n|---------------------------------------------------|\n| [javax.security.auth.callback.CallbackHandler](#) |\n\nAn application implements a `CallbackHandler` and passes it to underlying security services so that they may interact with the application to retrieve specific authentication data, such as usernames and passwords, or to display certain information, such as error and warning messages.\n\nCallbackHandlers are implemented in an application-dependent fashion. For example, implementations for an application with a graphical user interface (GUI) may pop up windows to prompt for requested information or to display error messages. An implementation may also choose to obtain requested information from an alternate source without asking the end user.\n\nUnderlying security services make requests for different types of information by passing individual Callbacks to the `CallbackHandler`. The `CallbackHandler` implementation decides how to retrieve and display information depending on the Callbacks passed to it. For example, if the underlying service needs a username and password to authenticate a user, it uses a `NameCallback` and `PasswordCallback`. The `CallbackHandler` can then choose to prompt for a username and password serially, or to prompt for both in a single window.\n\nA default `CallbackHandler` class implementation may be specified by setting the value of the `auth.login.defaultCallbackHandler` security property.\n\nIf the security property is set to the fully qualified name of a `CallbackHandler` implementation class, then a `LoginContext` will load the specified `CallbackHandler` and pass it to the underlying LoginModules. The `LoginContext` only loads the default handler if it was not provided one.\n\nAll default handler implementations must provide a public zero-argument constructor.\n\nSummary\n-------\n\n| Public methods ||\n|---------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [handle](#handle(kotlin.Array))`(`callbacks:` `[Array](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/index.html)\u003c[Callback](/reference/kotlin/javax/security/auth/callback/Callback)!\u003e!`)` Retrieve or display the information requested in the provided Callbacks. |\n\nPublic methods\n--------------\n\n### handle\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun handle(callbacks: Array\u003cCallback!\u003e!): Unit\n```\n\nRetrieve or display the information requested in the provided Callbacks.\n\nThe `handle` method implementation checks the instance(s) of the `Callback` object(s) passed in to retrieve or display the requested information. The following example is provided to help demonstrate what an `handle` method implementation might look like. This example code is for guidance only. Many details, including proper error handling, are left out for simplicity. \n\n```kotlin\n\u003ccode\u003epublic void handle(Callback[] callbacks)\n throws IOException, UnsupportedCallbackException {\n \n for (int i = 0; i < callbacks.length; i++) {\n if (callbacks[i] instanceof TextOutputCallback) {\n \n // display the message according to the specified type\n TextOutputCallback toc = (TextOutputCallback)callbacks[i];\n switch (toc.getMessageType()) {\n case TextOutputCallback.INFORMATION:\n System.out.println(toc.getMessage());\n break;\n case TextOutputCallback.ERROR:\n System.out.println(\"ERROR: \" + toc.getMessage());\n break;\n case TextOutputCallback.WARNING:\n System.out.println(\"WARNING: \" + toc.getMessage());\n break;\n default:\n throw new IOException(\"Unsupported message type: \" +\n toc.getMessageType());\n }\n \n } else if (callbacks[i] instanceof NameCallback) {\n \n // prompt the user for a username\n NameCallback nc = (NameCallback)callbacks[i];\n \n // ignore the provided defaultName\n System.err.print(nc.getPrompt());\n System.err.flush();\n nc.setName((new BufferedReader\n (new InputStreamReader(System.in))).readLine());\n \n } else if (callbacks[i] instanceof PasswordCallback) {\n \n // prompt the user for sensitive information\n PasswordCallback pc = (PasswordCallback)callbacks[i];\n System.err.print(pc.getPrompt());\n System.err.flush();\n pc.setPassword(readPassword(System.in));\n \n } else {\n throw new UnsupportedCallbackException\n (callbacks[i], \"Unrecognized Callback\");\n }\n }\n }\n \n // Reads user password from given input stream.\n private char[] readPassword(InputStream in) throws IOException {\n // insert code to read a user password from the input stream\n }\n \u003c/code\u003e\n```\n\n| Parameters ||\n|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `callbacks` | [Array](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/index.html)\\\u003c[Callback](/reference/kotlin/javax/security/auth/callback/Callback)!\\\u003e!: an array of `Callback` objects provided by an underlying security service which contains the information requested to be retrieved or displayed. |\n\n| Exceptions ||\n|-------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------|\n| `java.io.IOException` | if an input or output error occurs. \u003cbr /\u003e |\n| `javax.security.auth.callback.UnsupportedCallbackException` | if the implementation of this method does not support one or more of the Callbacks specified in the `callbacks` parameter. |"]]