ACL 可以指 Access Control List(访问控制列表),它是一种网络安全技术,用于控制数据包在网络设备上的流动。ACL 可以根据预先设定的规则,允许或者拒绝数据包通过网络设备,从而实现对网络流量的精细控制。
在 ZooKeeper 中,ACL 是 Access Control List(访问控制列表)的缩写,它用于控制对 ZooKeeper 节点的访问权限。ACL 可以通过预定义的规则来限制对 ZooKeeper 节点的读取、写入和管理权限。这样可以确保 ZooKeeper 中的数据得到安全地存储和访问。ACL 在 ZooKeeper 中扮演着重要的安全角色,可以帮助管理者控制对 ZooKeeper 节点的访问权限。
本文将介绍怎样通过 ZooKeeper 类中的 getACL 和 seACL 方法来控制节点的 ACL。
Stat setACL(String path, List<ACL> acl, int version) 如果存在给定路径的节点,且给定版本与该节点的版本一致,则为该节点设置 ACL。如果不存在具有给定路径的节点,则会抛出错误代码为 KeeperException.NoNode 的 KeeperException。如果给定的版本与节点的版本不匹配,将抛出错误代码为 KeeperException.BadVersion 的 KeeperException。
void setACL(String path, List<ACL> acl, int version, AsyncCallback.StatCallback cb, Object ctx) setACL 方法的异步版本
参数说明:
path - 节点路径
acl - ACL 列表
versin - 节点版本
cb - 回调接口
ctx - 上下文,用于向回调传递扩展数据
List<ACL> getACL(String path, Stat stat) 返回给定路径节点的 ACL 和状态。如果不存在具有给定路径的节点,则会抛出错误代码为 KeeperException.NoNode 的 KeeperException。
void getACL(String path, Stat stat, AsyncCallback.ACLCallback cb, Object ctx) getACL 方法的异步版本
参数说明:
path - 节点路径
stat - 如果不为空,节点的状态将被复制到此参数中。
cb - 回调接口
ctx - 上下文,用于向回调传递扩展数据
下面通过实例演示同步设置/获取ACL以及异步设置/获取ACL的用法,代码如下:
package com.hxstrive.zookeeper; import com.alibaba.fastjson.JSONObject; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; import org.apache.zookeeper.data.Stat; import org.junit.Before; import org.junit.Test; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; /** * 设置节点ACL * @author hxstrive.com */ public class SetACLNode { private static ZooKeeper zooKeeper; @Before public void init() throws Exception { zooKeeper = new ZooKeeper("127.0.0.1:2181", 2000, new Watcher() { public void process(WatchedEvent watchedEvent) { System.out.println("触发了 " + watchedEvent.getType() + " 事件"); } }); // 如果节点不存在,则创建节点 Stat stat = zooKeeper.exists("/setACL_node", false); if(null == stat) { String nodeName = zooKeeper.create("/setACL_node", "init value".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println("nodeName = " + nodeName); } } /** * 同步判断节点是否存在 */ @Test public void syncDemo() throws Exception { Stat stat = zooKeeper.setACL("/setACL_node", ZooDefs.Ids.OPEN_ACL_UNSAFE, -1); System.out.println(JSONObject.toJSONString(stat)); // 获取 ACL Stat newStat = new Stat(); List<ACL> aclList = zooKeeper.getACL("/setACL_node", newStat); System.out.println("newStat = " + JSONObject.toJSONString(newStat)); for(ACL acl : aclList) { System.out.println(JSONObject.toJSONString(acl)); } //输出: //触发了 None 事件 //nodeName = /setACL_node //{"aversion":1,"ctime":1703939758594,"cversion":0,"czxid":220,"dataLength":10, // "ephemeralOwner":0,"mtime":1703939758594,"mzxid":220,"numChildren":0,"pzxid":220,"version":0} //newStat = {"aversion":2,"ctime":1703939758594,"cversion":0,"czxid":220,"dataLength":10, // "ephemeralOwner":0,"mtime":1703939758594,"mzxid":220,"numChildren":0,"pzxid":220,"version":0} //{"id":{"id":"anyone","scheme":"world"},"perms":31} } /** * 异步判断节点是否存在 */ @Test public void asyncDemo() throws Exception { Map<String,String> extData = new HashMap<>(); extData.put("code", "C100"); extData.put("title", "这是标题"); CountDownLatch countDownLatch = new CountDownLatch(1); zooKeeper.setACL("/setACL_node", ZooDefs.Ids.OPEN_ACL_UNSAFE, -1, new AsyncCallback.StatCallback() { @Override public void processResult(int i, String s, Object o, Stat stat) { System.out.println("i = " + i); System.out.println("s = " + s); System.out.println("o = " + o); System.out.println("stat = " + JSONObject.toJSONString(stat)); countDownLatch.countDown(); } }, extData); countDownLatch.await(); // 获取ACL CountDownLatch countDownLatch2 = new CountDownLatch(1); Stat newStat = new Stat(); zooKeeper.getACL("/setACL_node", newStat, new AsyncCallback.ACLCallback() { @Override public void processResult(int i, String s, Object o, List<ACL> list, Stat stat) { System.out.println("i = " + i); System.out.println("s = " + s); System.out.println("o = " + o); System.out.println("list = " + JSONObject.toJSONString(list)); System.out.println("stat = " + JSONObject.toJSONString(stat)); countDownLatch2.countDown(); } }, extData); countDownLatch2.await(); //输出: //触发了 None 事件 //i = 0 //s = /setACL_node //o = {code=C100, title=这是标题} //stat = {"aversion":3,"ctime":1703939758594,"cversion":0,"czxid":220,"dataLength":10,"ephemeralOwner":0, // "mtime":1703939758594,"mzxid":220,"numChildren":0,"pzxid":220,"version":0} //i = 0 //s = /setACL_node //o = {code=C100, title=这是标题} //list = [{"id":{"id":"anyone","scheme":"world"},"perms":31}] //stat = {"aversion":3,"ctime":1703939758594,"cversion":0,"czxid":220,"dataLength":10,"ephemeralOwner":0, // "mtime":1703939758594,"mzxid":220,"numChildren":0,"pzxid":220,"version":0} } }