103 lines
7.4 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="header">
<div class="subTitle">
compact1, compact2, compact3
</div>
<div class="subTitle">
javax.security.auth.callback
</div>
<h2 class="title" title="Interface CallbackHandler">Interface CallbackHandler</h2>
</div><div class="contentContainer">
<div class="description">
<ul class="blockList">
<li class="blockList">
<hr/> <br/> <pre>public interface <span class="typeNameLabel">CallbackHandler</span></pre>
<div class="block">
<p> <span>应用程序实现<code>CallbackHandler</code>并将其传递给底层安全服务,以便它们可以与应用程序交互以检索特定的身份验证数据,例如用户名和密码,或显示某些信息,如错误和警告消息。</span> </p>
<p> <span>CallbackHandler以应用程序相关的方式实现。</span> <span>例如具有图形用户界面GUI的应用的实现可以弹出窗口以提示所请求的信息或显示错误消息。</span> <span>实现还可以选择从备用源获得所请求的信息,而不要求最终用户。</span> </p>
<p> <span>基础安全服务通过将个人回调传递给CallbackHandler来请求不同类型的<code>CallbackHandler</code></span> <span><code>CallbackHandler</code>实现决定如何根据传递给它的回调来检索和显示信息。</span> <span>例如,如果底层服务需要用户名和密码来认证用户,则它使用<code>NameCallback</code><code>PasswordCallback</code></span> <span>然后, <code>CallbackHandler</code>可以选择提示输入用户名和密码,或在单个窗口中提示输入。</span> </p>
<p> <span>可以通过设置<code>auth.login.defaultCallbackHandler</code>安全属性的值来指定默认的<code>CallbackHandler</code>类实现。</span> </p>
<p> <span>如果security属性设置为<code>CallbackHandler</code>实施类的完全限定名称,那么<code>LoginContext</code>将加载指定的CallbackHandler <code>CallbackHandler</code>其传递给基础的LoginModules。</span> <span><code>LoginContext</code>仅加载默认处理程序(如果未提供)。</span> </p>
<p> <span>所有默认处理程序实现必须提供公共零参数构造函数。</span> </p>
</div>
<dl>
<dt>
<span class="seeLabel">另请参见:</span>
</dt>
<dd>
<span><a href="../../../../java/security/Security.html" title="java.security中的类"><code>security properties</code></a></span>
</dd>
</dl> </li>
</ul>
</div>
<div class="summary">
<ul class="blockList">
<li class="blockList">
<!-- ========== METHOD SUMMARY =========== -->
<ul class="blockList">
<li class="blockList"><a name="method.summary">
<!-- --> </a> <h3>方法摘要</h3>
<table border="0" cellpadding="3" cellspacing="0" class="memberSummary" summary="Method Summary table, listing methods, and an explanation">
<caption>
<span class="activeTableTab" id="t0"><span>所有方法</span><span class="tabEnd"> </span></span>
<span class="tableTab" id="t2"><span><a href="javascript:show(2);">接口方法</a></span><span class="tabEnd"> </span></span>
<span class="tableTab" id="t3"><span><a href="javascript:show(4);">抽象方法</a></span><span class="tabEnd"> </span></span>
</caption>
<tbody>
<tr>
<th class="colFirst" scope="col">Modifier and Type</th>
<th class="colLast" scope="col">Method and Description</th>
</tr>
<tr class="altColor" id="i0">
<td class="colFirst"><code>void</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../../../javax/security/auth/callback/CallbackHandler.html#handle-javax.security.auth.callback.Callback:A-">handle</a></span>(<a href="../../../../javax/security/auth/callback/Callback.html" title="interface in javax.security.auth.callback">Callback</a>[] callbacks)</code>
<div class="block">
检索或显示提供的回调中请求的信息。
</div> </td>
</tr>
</tbody>
</table> </li>
</ul> </li>
</ul>
</div>
<div class="details">
<ul class="blockList">
<li class="blockList">
<!-- ============ METHOD DETAIL ========== -->
<ul class="blockList">
<li class="blockList"><a name="method.detail">
<!-- --> </a> <h3>方法详细信息</h3> <a name="handle-javax.security.auth.callback.Callback:A-">
<!-- --> </a>
<ul class="blockListLast">
<li class="blockList"> <h4>handle</h4> <pre>void handle(<a href="../../../../javax/security/auth/callback/Callback.html" title="interface in javax.security.auth.callback">Callback</a>[] callbacks)
throws <a href="../../../../java/io/IOException.html" title="class in java.io">IOException</a>,
<a href="../../../../javax/security/auth/callback/UnsupportedCallbackException.html" title="class in javax.security.auth.callback">UnsupportedCallbackException</a></pre>
<div class="block">
<p> <span>检索或显示提供的回调中请求的信息。</span> </p>
<p> <span><code>handle</code>方法实现检查传入的<code>Callback</code>对象的实例以检索或显示所请求的信息。</span> <span>提供以下示例来帮助演示什么样的<code>handle</code>方法实现。</span> <span>此示例代码仅供参考。</span> <span>为了简单起见,省略了许多细节,包括适当的错误处理。</span> </p>
<pre> <span><code> public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (int i = 0; i &lt; 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></span> </pre>
</div>
<dl>
<dt>
<span class="paramLabel">参数</span>
</dt>
<dd>
<code>callbacks</code> - 由底层安全服务提供的
<code>Callback</code>对象数组,其中包含请求检索或显示的信息。
</dd>
<dt>
<span class="throwsLabel">异常</span>
</dt>
<dd>
<code><a href="../../../../java/io/IOException.html" title="class in java.io">IOException</a></code> - 如果发生输入或输出错误。
<p></p>
</dd>
<dd>
<code><a href="../../../../javax/security/auth/callback/UnsupportedCallbackException.html" title="class in javax.security.auth.callback">UnsupportedCallbackException</a></code> - if the implementation of this method does not support one or more of the Callbacks specified in the
<code>callbacks</code> parameter.
</dd>
</dl> </li>
</ul> </li>
</ul> </li>
</ul>
</div>
</div>