Stay organized with collections
Save and categorize content based on your preferences.
CallbackHandler
public
interface
CallbackHandler
javax.security.auth.callback.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
void
|
handle(Callback[] callbacks)
Retrieve or display the information requested in the
provided Callbacks.
|
Public methods
handle
public abstract void handle (Callback[] callbacks)
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.
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
}
Parameters |
callbacks |
Callback : an array of Callback objects provided
by an underlying security service which contains
the information requested to be retrieved or displayed. |
Throws |
IOException |
if an input or output error occurs. |
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](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\nCallbackHandler\n===============\n\n\n`\npublic\n\n\ninterface\nCallbackHandler\n`\n\n\n`\n\n\n`\n\n|----------------------------------------------|\n| javax.security.auth.callback.CallbackHandler |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\n\u003cbr /\u003e\n\nAn application implements a `CallbackHandler` and passes\nit to underlying security services so that they may interact with\nthe application to retrieve specific authentication data,\nsuch as usernames and passwords, or to display certain information,\nsuch as error and warning messages.\n\nCallbackHandlers are implemented in an application-dependent fashion.\nFor example, implementations for an application with a graphical user\ninterface (GUI) may pop up windows to prompt for requested information\nor to display error messages. An implementation may also choose to obtain\nrequested information from an alternate source without asking the end user.\n\nUnderlying security services make requests for different types\nof information by passing individual Callbacks to the\n`CallbackHandler`. The `CallbackHandler`\nimplementation decides how to retrieve and display information\ndepending on the Callbacks passed to it. For example,\nif the underlying service needs a username and password to\nauthenticate a user, it uses a `NameCallback` and\n`PasswordCallback`. The `CallbackHandler`\ncan then choose to prompt for a username and password serially,\nor to prompt for both in a single window.\n\nA default `CallbackHandler` class implementation\nmay be specified by setting the value of the\n`auth.login.defaultCallbackHandler` security property.\n\nIf the security property is set to the fully qualified name of a\n`CallbackHandler` implementation class,\nthen a `LoginContext` will load the specified\n`CallbackHandler` and pass it to the underlying LoginModules.\nThe `LoginContext` only loads the default handler\nif it was not provided one.\n\nAll default handler implementations must provide a public\nzero-argument constructor. \n**See also:**\n\n- [security properties](/reference/java/security/Security)\n\nSummary\n-------\n\n| ### Public methods ||\n|------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` abstract void` | ` `[handle](/reference/javax/security/auth/callback/CallbackHandler#handle(javax.security.auth.callback.Callback[]))`(`[Callback[]](/reference/javax/security/auth/callback/Callback)` callbacks) ` \u003cbr /\u003e Retrieve or display the information requested in the provided Callbacks. |\n\nPublic methods\n--------------\n\n### handle\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract void handle (Callback[] callbacks)\n```\n\n\u003cbr /\u003e\n\nRetrieve or display the information requested in the\nprovided Callbacks.\n\nThe `handle` method implementation checks the\ninstance(s) of the `Callback` object(s) passed in\nto retrieve or display the requested information.\nThe following example is provided to help demonstrate what an\n`handle` method implementation might look like.\nThis example code is for guidance only. Many details,\nincluding proper error handling, are left out for simplicity.\n\n public void handle(Callback[] callbacks)\n throws IOException, UnsupportedCallbackException {\n\n for (int i = 0; i \u003c 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 \n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Parameters ||\n|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `callbacks` | `Callback`: an array of `Callback` objects provided by an underlying security service which contains the information requested to be retrieved or displayed. \u003cbr /\u003e |\n\n| Throws ||\n|------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------|\n| [IOException](/reference/java/io/IOException) | if an input or output error occurs. \u003cbr /\u003e |\n| [UnsupportedCallbackException](/reference/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. |"]]