ZooKeeper zk=new ZooKeeper("192.168.1.188,192.168.1.189,192.168.1.190",50000,w);
ArrayList<ACL> acls = new ArrayList<ACL>();
ACL acl1=new ACL(ZooDefs.Perms.ALL, new Id("digest", DigestAuthenticationProvider.generateDigest("qingxu:taobao123")));
//或者把以上两行换成如下三行
// zk.addAuthInfo("digest","qingxu:taobao123".getBytes());
//ArrayList<ACL> acls = new ArrayList<ACL>();
//ACL acl1=new ACL(ZooDefs.Perms.ALL, new Id("auth", ""));
acls.add(acl1);
zk.create("/qingxu/child",null, acls, CreateMode.PERSISTENT);
Thread.sleep(50000000);
zk.close();
child这个znode对于用户qingxu来说具有所有权限。它采用的digest认证方式。
其他客户端如果要访问这个znode或者创建子znode,需要首先进行认证。
如果这里qingxu:taobao123输入成了qingxu:taobao124,那么将会抛出NoAuthException.ZooKeeper zk=new ZooKeeper("192.168.1.188,192.168.1.189,192.168.1.190",50000,w); zk.addAuthInfo("digest","qingxu:taobao123".getBytes()); zk.create("/qingxu/child/sunzi2",null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.close();
原理请参考《ZK访问控制流程》