Curator 使用(三) ACL 权限控制

Curator 使用(三) ACL 权限控制

Zookeeper 五种操作权限

总体来说,ZK的节点有5种操作权限:

CREATE、READ、WRITE、DELETE、ADMIN 也就是 增、删、改、查、管理 权限,这5种权限简写为 crwda (即:每个单词的首字符缩写)

注:这5种权限中,delete是指对子节点的删除权限,其它4种权限指对自身节点的操作权限

Zookeeper 身份认证方式

  1. world:默认方式,相当于全世界都能访问
  2. auth:代表已经认证通过的用户(cli中可以通过addauth digest user:pwd 来添加当前上下文中的授权用户)
  3. digest:即用户名:密码这种方式认证,这也是业务系统中最常用的
  4. ip:使用ip地址认证

digist 密码加密规则

static public String generateDigest(String idPassword)
        throws NoSuchAlgorithmException {
    String[] parts = idPassword.split(":", 2);
    byte[] digest = MessageDigest.getInstance("SHA1").digest(
            idPassword.getBytes());
    return parts[0] + ":" + base64Encode(digest);
}

先通过SHA1加密,然后base64编码

通过 ACL 创建客户端

client = CuratorFrameworkFactory.builder()
      .authorization("digest", "imooc1:123456".getBytes())
      .connectString(zkServerPath)
      .sessionTimeoutMs(10000).retryPolicy(retryPolicy)
      .namespace("workspace").build();

创建 ACL 节点

String nodePath = "/acl/father/child/sub";
List<ACL> acls = new ArrayList<ACL>();
Id imooc1 = new Id("digest", AclUtils.getDigestUserPwd("imooc1:123456"));
Id imooc2 = new Id("digest", AclUtils.getDigestUserPwd("imooc2:123456"));
acls.add(new ACL(Perms.ALL, imooc1));
acls.add(new ACL(Perms.READ, imooc2));
acls.add(new ACL(Perms.DELETE | Perms.CREATE, imooc2));

// 创建节点
byte[] data = "spiderman".getBytes();
cto.client.create().creatingParentsIfNeeded()
      .withMode(CreateMode.PERSISTENT)
      .withACL(acls, true) // applyToParents if true, then the aclList is applied to the created parents
      .forPath(nodePath, data);

为 ZNode 增加 ACL 权限控制

client.setACL().withACL(acls).forPath("/curatorNode");
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容