执行SASL身份验证作为客户端。
诸如LDAP之类的协议库获取此类的实例,以执行由特定SASL机制定义的认证。 调用SaslClient实例过程的方法,并根据SaslClient实现的SASL机制创建响应。 当认证进行时,该实例封装了SASL客户端认证交换的状态。
以下是LDAP库如何使用SaslClient 。 它首先得到一个SaslClient的实例:
SaslClient sc = Sasl.createSaslClient(mechanisms,
authorizationId, protocol, serverName, props, callbackHandler);
然后可以继续使用客户端进行身份验证。
例如,LDAP库可能会使用客户端,如下所示:
// Get initial response and send to server
byte[] response = (sc.hasInitialResponse() ? sc.evaluateChallenge(new byte[0]) :
null);
LdapResult res = ldap.sendBindRequest(dn, sc.getName(), response);
while (!sc.isComplete() &&
(res.status == SASL_BIND_IN_PROGRESS || res.status == SUCCESS)) {
response = sc.evaluateChallenge(res.getBytes());
if (res.status == SUCCESS) {
// we're done; don't expect to send another BIND
if (response != null) {
throw new SaslException(
"Protocol error: attempting to send response after completion");
}
break;
}
res = ldap.sendBindRequest(dn, sc.getName(), response);
}
if (sc.isComplete() && res.status == SUCCESS) {
String qop = (String) sc.getNegotiatedProperty(Sasl.QOP);
if (qop != null
&& (qop.equalsIgnoreCase("auth-int")
|| qop.equalsIgnoreCase("auth-conf"))) {
// Use SaslClient.wrap() and SaslClient.unwrap() for future
// communication with server
ldap.in = new SecureInputStream(sc, ldap.in);
ldap.out = new SecureOutputStream(sc, ldap.out);
}
}
如果机制有一个初始响应,库将调用evaluateChallenge()一个空挑战并得到初始响应。
诸如IMAP4之类的协议,不包括初始响应及其对服务器的第一个认证命令,在不首先调用hasInitialResponse()或evaluateChallenge()情况下启动认证。
当服务器响应该命令时,它发送初始挑战。
对于客户端首先发送数据的SASL机制,服务器应该发出无数据的挑战。
这将导致呼叫(在客户端)到evaluateChallenge()与空挑战。