Zookeeper provides authority authentication as zookeeper client access restrictions, there are two main ways, 1, IP mode 2, digest permission mode
You can define permission content by creating a node. Here is the Java implementation
Package com.aicong.test.helloZookeeper;
Import java.io.IOException;
Import java.security.NoSuchAlgorithmException;
Import java.util.ArrayList;
Import Org.apache.zookeeper.CreateMode;
Import org.apache.zookeeper.KeeperException;
Import org.apache.zookeeper.WatchedEvent;
Import Org.apache.zookeeper.Watcher;
Import Org.apache.zookeeper.Watcher.Event.EventType;
Import Org.apache.zookeeper.Watcher.Event.KeeperState;
Import org.apache.zookeeper.ZooDefs.Perms;
Import Org.apache.zookeeper.ZooKeeper;
Import Org.apache.zookeeper.data.ACL;
Import Org.apache.zookeeper.data.Id;
Import Org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
public class Createnodesyncauth implements watcher {private static Zookeeper zookeeper;
private static Boolean somethingdone = false; public static void Main (string[] args) throws IOException, interruptedexception {zookeeper = new zookeeper ("112.74.219.
174:2181 ", 5000,new Createnodesyncauth ());
System.out.println (Zookeeper.getstate ()); Thread.Sleep (Integer.max_value); /* * Permission mode (scheme): IP, Digest * Authorization Object (ID) * IP rights mode: specific IP address * Digest permission mode: Username:base64 (SHA-1 username: Password)) * Permission (permission): Create (C), DELETE (D), READ (R), WRITE (W), ADMIN (A) * Note: Individual permissions, full permissions, composite permissions * * Permission combination: Sch EME + ID + Permission * * * * * * * * */private void dosomething () {try {///Create node requires permission validation//ip-based ACL ACL
ip = new ACL (perms.read,new Id ("IP", "112.74.219.174")); Based on username password ACL acldigest = new ACL (perms.read|
Perms.write,new Id ("Digest", Digestauthenticationprovider.generatedigest ("jike:123456"));
Arraylist<acl> ACLS = new arraylist<acl> ();
Acls.add (acldigest);
Acls.add (Aclip);
Zookeeper.addauthinfo ("Digest", "jike:123456". GetBytes ());
String Path = zookeeper.create ("/node_10", "123". GetBytes (), ACLs, Createmode.persistent);
System.out.println ("Return path:" +path);
Somethingdone = true;
catch (Keeperexception e) {e.printstacktrace (); } CAtch (interruptedexception e) {e.printstacktrace ();
catch (NoSuchAlgorithmException e) {//TODO auto-generated catch block E.printstacktrace (); @Override public void Process (Watchedevent event) {//TODO auto-generated Method Stub System.out.println ("Received event
: "+event); if (Event.getstate () ==keeperstate.syncconnected) {if (!somethingdone && event.gettype () ==eventtype.none
&& Null==event.getpath ()) {dosomething ();
}
}
}
}
Callers can use code:
Package com.aicong.test.helloZookeeper;
Import java.io.IOException;
Import org.apache.zookeeper.KeeperException;
Import org.apache.zookeeper.WatchedEvent;
Import Org.apache.zookeeper.Watcher;
Import Org.apache.zookeeper.Watcher.Event.EventType;
Import Org.apache.zookeeper.Watcher.Event.KeeperState;
Import Org.apache.zookeeper.ZooKeeper;
Import Org.apache.zookeeper.data.Stat;
public class Getdatasyncauth implements watcher{private static zookeeper zookeeper;
private static Stat Stat = new Stat (); public static void Main (string[] args) throws IOException, Interruptedexception, keeperexception {zookeeper = new Zo
Okeeper ("112.74.219.174:2181", 5000,new Getdatasyncauth ());
System.out.println (Zookeeper.getstate (). toString ());
Thread.Sleep (Integer.max_value);
} private void DoSomething (Zookeeper zookeeper) {//permission validation zookeeper.addauthinfo ("Digest", "jike:1234". GetBytes ()); try {System.out.println (Zookeeper.getdata ("/node_4", trUE, STAT));
catch (Keeperexception e) {e.printstacktrace ();
catch (Interruptedexception e) {e.printstacktrace (); @Override public void Process (Watchedevent event) {//TODO auto-generated method stub if Event.getstate ( ==keeperstate.syncconnected) {if (Event.gettype () ==eventtype.none && Null==event.getpath ()) {doSomething (Z
Ookeeper); }else{if (Event.gettype () ==eventtype.nodedatachanged) {try {System.out.println (new String (ZOOKEEPER.G
Etdata (Event.getpath (), True, stat));
System.out.println ("stat:" +stat);
catch (Keeperexception e) {e.printstacktrace ();
catch (Interruptedexception e) {e.printstacktrace ();
}
}
}
}
}
}