前言
在每个zk版本的中都有提供这个工具,以zookeeper-3.7.0.jar(解压后在lib里)为例。
方法
import java.security.NoSuchAlgorithmException;
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
public class AclUtils {
public static String getDigestUserPwd(String idPassword) throws NoSuchAlgorithmException {
return DigestAuthenticationProvider.generateDigest(idPassword);
}
public static void main(String[] args) throws NoSuchAlgorithmException {
String idPassword="root:root";//该用户通过后台的#addauth digested root:root 命令来创建的
String idDigested=getDigestUserPwd(idPassword);
System.out.println(idDigested);
//root:qiTlqPLK7XM2ht3HMn02qRpkKIE=
}
}
使用
java前台Node节点时,需要赋予节点ACL权限,scheme是digest的时候,就需要使用上面的AclUtils工具把明文密码转换为密文。上面的AclUtils工具类主要是在下面的getAcls()方法中使用,贴出整个代码,便于验证消化吸收。
package com.xp.zk.node.data.opera;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.acl.Acl;
import java.util.ArrayList;
import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
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.data.Stat;
public class ZKNodeAcl implements Watcher{
private ZooKeeper zookeeper = null;
public static final String zkServerPath = "192.168.31.216:2181";
public static final Integer timeout = 5000;
public ZKNodeAcl() {
super();
}
public ZKNodeAcl(String connectStrPath) {
try {
zookeeper = new ZooKeeper(connectStrPath, timeout, new ZKNodeAcl());
} catch (IOException e) {
e.printStackTrace();
if (null != zookeeper) {
try {
zookeeper.close();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
public static void createNode(String nodePath,byte[]data,List<ACL> acls) throws Exception{
ZooKeeper zookeeper=new ZKNodeAcl(zkServerPath).getZookeeper();
Stat stat=zookeeper.exists(nodePath, false);//先判断节点是否已经存在(false意思是不再回调process)
if(null!=stat) {
System.out.println("节点已存在,版本号:"+stat.getVersion());
}else {
System.out.println("节点不存在,正在创建…");
zookeeper.addAuthInfo("digest", "lilei:lilei".getBytes());//登录lilei用户
String result=zookeeper.create(nodePath, data, acls,CreateMode.PERSISTENT);
System.out.println("创建节点:\t"+result+"\t成功");
}
}
//一次性给两个用户赋值不同的操作权限
public static List<ACL> getAcls() throws NoSuchAlgorithmException{
List<ACL> acls=new ArrayList<ACL>();
Id mayun =new Id("digest", AclUtils.getDigestUserPwd("mayun:mayun"));
Id lilei =new Id("digest", AclUtils.getDigestUserPwd("lilei:lilei"));
acls.add(new ACL(Perms.ALL, mayun));//给mayun一次性赋值所有权限
acls.add(new ACL(Perms.READ, lilei));
acls.add(new ACL(Perms.DELETE | Perms.CREATE, lilei));//给lilei分两次赋权限(目的:看不同的赋权方式)
return acls;
}
public static void main(String[] args) throws Exception {
String fatherPath="/succ/testdigest";
ZKNodeAcl.createNode(fatherPath, "testdigest".getBytes(), getAcls());//分配自定义权限
}
@Override
public void process(WatchedEvent event) {
}
public ZooKeeper getZookeeper() {
return zookeeper;
}
public void setZookeeper(ZooKeeper zookeeper) {
this.zookeeper = zookeeper;
}
}
尾言
我们在java前台,添加一个node节点时,往往需要给节点赋予Acls权限,有时候直接使用Ids.CREATOR_ALL_ACL(赋予节点所有开放权限,谁都能访问),它的scheme是word。
有时需要控制一下节点的访问权限,此时的scheme就应该是digest而不是?word,此时就需要使用密文,这个时候就需要使用上面的AclUtils工具转换一下。
也就是下面这段代码,用该方法的返回值代替Ids.CREATOR_ALL_ACL。
?public static List<ACL> getAcls() throws NoSuchAlgorithmException{
?? ??? ?List<ACL> acls=new ArrayList<ACL>();
?? ??? ?Id mayun =new Id("digest", AclUtils.getDigestUserPwd("mayun:mayun"));
?? ??? ?Id lilei =new Id("digest", AclUtils.getDigestUserPwd("lilei:lilei"));
?? ??? ?acls.add(new ACL(Perms.ALL, mayun));//给mayun一次性赋值所有权限
?? ??? ?acls.add(new ACL(Perms.READ, lilei));
?? ??? ?acls.add(new ACL(Perms.DELETE | Perms.CREATE, lilei));//给lilei分两次赋权限(目的:看不同的赋权方式)
?? ??? ?return acls;
?? ?}
|