【问题标题】:How to create zookeeper auth mechanism?如何创建zookeeper认证机制?
【发布时间】:2015-01-07 13:48:49
【问题描述】:

我正在尝试为节点创建 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

我什么时候错了?

谢谢!

【问题讨论】:

  • 面临同样的问题。

标签: java authentication apache-zookeeper digest-authentication


【解决方案1】:

文件说:

digest 使用 username:password 字符串生成 MD5 哈希,即 然后用作 ACL ID 身份。身份验证是通过发送 用户名:明文密码。在 ACL 中使用时,表达式 将是用户名:base64 编码的 SHA1 密码摘要。

所以,在创建节点时,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")

【讨论】:

    猜你喜欢
    • 2014-10-03
    • 2015-04-19
    • 2016-01-04
    • 2018-01-16
    • 2015-10-22
    • 2013-09-28
    • 1970-01-01
    • 2018-06-17
    • 2011-07-09
    相关资源
    最近更新 更多