java sasl例子_SaslClient

执行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()与空挑战。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Java框架中的ZooKeeper时,如果需要进行SASL身份验证,可以按照以下步骤进行操作: 1.在ZooKeeper服务器端配置JVM参数,启用SASL身份验证,例如: ``` -Dzookeeper.serverCnxnFactory=org.apache.zookeeper.server.NettyServerCnxnFactory -Djava.security.auth.login.config=/path/to/zookeeper_server_jaas.conf ``` 2.在ZooKeeper客户端端配置JVM参数,启用SASL身份验证,例如: ``` -Djava.security.auth.login.config=/path/to/zookeeper_client_jaas.conf ``` 3.在ZooKeeper服务器端和客户端端分别编写对应的JAAS配置文件,例如: 服务器端配置文件zookeeper_server_jaas.conf: ``` Server { org.apache.zookeeper.server.auth.DigestLoginModule required user_admin="password"; }; ``` 客户端配置文件zookeeper_client_jaas.conf: ``` Client { org.apache.zookeeper.server.auth.DigestLoginModule required username="admin" password="password"; }; ``` 其中,user_admin和password是服务器端的用户名和密码,username和password是客户端的用户名和密码。 4.在ZooKeeper客户端端编写对应的代码,例如: ``` import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import java.io.IOException; import java.util.concurrent.CountDownLatch; public class ZooKeeperClient { private static final String CONNECT_STRING = "localhost:2181"; private static final int SESSION_TIMEOUT = 5000; private static final String ZOOKEEPER_PATH = "/test"; private static CountDownLatch countDownLatch = new CountDownLatch(1); public static void main(String[] args) throws IOException, InterruptedException, KeeperException { ZooKeeper zooKeeper = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected) { countDownLatch.countDown(); } } }); countDownLatch.await(); String result = new String(zooKeeper.getData(ZOOKEEPER_PATH, false, new Stat())); System.out.println(result); zooKeeper.close(); } } ``` 以上代码中,我们使用了CountDownLatch来等待ZooKeeper客户端连接上ZooKeeper服务器。在连接成功之后,我们调用了zooKeeper.getData()方法来获取指定路径节点的数据。 以上就是在Java框架中使用ZooKeeper进行SASL身份验证的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值