Curator 使用(三) ACL 權(quán)限控制
Zookeeper 五種操作權(quán)限
總體來說右遭,ZK的節(jié)點(diǎn)有5種操作權(quán)限:
CREATE做盅、READ、WRITE窘哈、DELETE吹榴、ADMIN 也就是 增、刪滚婉、改图筹、查、管理 權(quán)限让腹,這5種權(quán)限簡寫為 crwda (即:每個單詞的首字符縮寫)
注:這5種權(quán)限中远剩,delete是指對子節(jié)點(diǎn)的刪除權(quán)限,其它4種權(quán)限指對自身節(jié)點(diǎn)的操作權(quán)限
Zookeeper 身份認(rèn)證方式
- world:默認(rèn)方式骇窍,相當(dāng)于全世界都能訪問
- auth:代表已經(jīng)認(rèn)證通過的用戶(cli中可以通過addauth digest user:pwd 來添加當(dāng)前上下文中的授權(quán)用戶)
- digest:即用戶名:密碼這種方式認(rèn)證瓜晤,這也是業(yè)務(wù)系統(tǒng)中最常用的
- ip:使用ip地址認(rèn)證
digist 密碼加密規(guī)則
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 創(chuàng)建客戶端
client = CuratorFrameworkFactory.builder()
.authorization("digest", "imooc1:123456".getBytes())
.connectString(zkServerPath)
.sessionTimeoutMs(10000).retryPolicy(retryPolicy)
.namespace("workspace").build();
創(chuàng)建 ACL 節(jié)點(diǎn)
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));
// 創(chuàng)建節(jié)點(diǎn)
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 權(quán)限控制
client.setACL().withACL(acls).forPath("/curatorNode");