gpt4 book ai didi

java - 如何创建zookeeper认证机制?

转载 作者:太空宇宙 更新时间:2023-11-04 14:15:21 25 4
gpt4 key购买 nike

我正在尝试为节点创建 acl:

    ZooKeeper client = new ZooKeeper("host:port/rootNode", 3000, null);
ACL acl = new ACL(Perms.CREATE,new Id("digest","user:pass"));
client.create("/testNode",new String("test").getBytes(), Arrays.asList(acl), CreateMode.PERSISTENT);
client.close();

然后我尝试访问该节点并在“testNode”下创建一个节点:

    ZooKeeper client = new ZooKeeper(" host:port/rootNode", 3000, null);
client.addAuthInfo("digest", new String("user:pass").getBytes());
Stat stat;
try {
stat = client.exists("/testNode", false);
if(stat!=null){
client.create("/testNode/clientTest", new String("clienttest").getBytes(),Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
}
} catch (KeeperException e) {
e.printStackTrace();
}
client.close();

但它给了我:

    org.apache.zookeeper.KeeperException$NoAuthException: KeeperErrorCode = NoAuth for /testNode/clientTest

我什么时候错了?

谢谢!

最佳答案

文件说:

digest uses a username:password string to generate MD5 hash which is then used as an ACL ID identity. Authentication is done by sending the username:password in clear text. When used in the ACL the expression will be the username:base64 encoded SHA1 password digest.

所以,创建节点时,ACL表达式不能只是user:pass,它必须经过编码。这样做:

String s = Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA1").digest("user:pass".getBytes()));
ACL acl = new ACL(ZooDefs.Perms.ALL, new Id("digest","user:" + s));

顺便说一句,该文档有点错误。因为根据这一点,你应该digest("pass")。但这是行不通的,您必须对整个字符串进行哈希处理 digest("user:pass")

关于java - 如何创建zookeeper认证机制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27820928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com