Java API訪問ZK的許可權控制
阿新 • • 發佈:2019-02-17
- 無許可權訪問結點
/**
* 對於ZK的授權訪問
* Created by liuhuichao on 2017/7/27.
*/
public class AutoSample {
private static String path="/test-1";
public static void main(String[] args)throws Exception {
// ZooKeeper zk=new ZooKeeper("rc-zkp-datn-rse-nmg-ooz-woasis:2181",5000,null);
// zk.addAuthInfo("digest","lhc:true".getBytes());
// zk.create(path,"lhc".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
//zk.create(path+"/node1","lhc".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
/**無授權資訊訪問**/
// ZooKeeper zk2=new ZooKeeper("rc-zkp-datn-rse-nmg-ooz-woasis:2181",5000,null);
// byte[] result=zk2.getData(path,false,null);
/**使用錯誤資訊訪問結點**/
//ZooKeeper zk3=new ZooKeeper("rc-zkp-datn-rse-nmg-ooz-woasis:2181",5000,null);
//zk3.addAuthInfo("digest","lhc:false".getBytes());
// zk3.getData(path,false,null);// KeeperErrorCode = NoAuth for /test-1
/**測試刪除節點**/
ZooKeeper zk=new ZooKeeper("rc-zkp-datn-rse-nmg-ooz-woasis:2181",5000,null);
zk.addAuthInfo("digest","lhc:true".getBytes());
zk.delete(path+"/node1",-1);
ZooKeeper zk1=new ZooKeeper("rc-zkp-datn-rse-nmg-ooz-woasis:2181",5000,null);
zk1.delete(path,-1);
}
}