Apache ZooKeeper - 使用原生的API操作ZK_ACL权限
生活随笔
收集整理的這篇文章主要介紹了
Apache ZooKeeper - 使用原生的API操作ZK_ACL权限
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- Pre
- Code
- 創建world模式的節點
- 使用授權模式創建節點
- 使用授權模式獲取節點數據
Pre
Apache ZooKeeper - ZK的ACL權限控制( Access Control List )
Apache ZooKeeper - 使用原生的API操作ZK_CRUD
Code
創建world模式的節點
package com.artisan.zk.originalClient;import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.ACL; import org.apache.zookeeper.data.Id; import org.apache.zookeeper.server.auth.DigestAuthenticationProvider; import org.junit.Test;import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2020/12/1 11:19* @mark: show me the code , change the world*/@Slf4j public class AclOperationStanAloneModeTest extends StandAloneBaseTest{private static final String NODE_NAME = "/artisan-acl-node";/*** 創建world模式的節點*/@SneakyThrows@Testpublic void testCreateNodeWithACL(){List<ACL>aclList = new ArrayList<>();ACL acl = new ACL();Id id = new Id();id.setId("anyone");id.setScheme("world");// 權限int perms = ZooDefs.Perms.ADMIN | ZooDefs.Perms.READ ;// 綁定acl.setId(id);acl.setPerms(perms);aclList.add(acl);String s = getZooKeeper().create(NODE_NAME, "artisan".getBytes(), aclList, CreateMode.PERSISTENT);log.info("path {} created " ,s);} }登錄ZK Client查看數據
使用授權模式創建節點
/**** 使用授權模式創建節點*/@SneakyThrows@Testpublic void createWithAclTest2() {String namePWD = "artisan:artisanPWD";// 對連接添加授權信息getZooKeeper().addAuthInfo("digest",namePWD.getBytes());List<ACL> acLList = new ArrayList<ACL>();ACL acl = new ACL();Id id = new Id();id.setId(namePWD);id.setScheme("auth");int perms = ZooDefs.Perms.ADMIN | ZooDefs.Perms.READ |ZooDefs.Perms.WRITE;acl.setId(id);acl.setPerms(perms);acLList.add(acl);String s = getZooKeeper().create("/artisanNode2", "artisan".getBytes(), acLList, CreateMode.PERSISTENT);log.info("create path: {}",s);}如果要手工查看 可以這兒樣
如果用代碼訪問 如下
使用授權模式獲取節點數據
@Testpublic void getDataWithAcl() throws KeeperException, InterruptedException {String namePWD = "artisan:artisanPWD";// 對連接添加授權信息getZooKeeper().addAuthInfo("digest",namePWD.getBytes());byte[] data = getZooKeeper().getData("/artisanNode2", false, null);log.info("GET_DATA : {}",new String(data));}public static void main(String[] args) throws NoSuchAlgorithmException {String sId = DigestAuthenticationProvider.generateDigest("artisan:artisanPWD");System.out.println(sId);// -Dzookeeper.DigestAuthenticationProvider.superDigest=artisan:d3gLrY2XgzvXZbJObw+wiWIQDko=}是不是明白了???
總結
以上是生活随笔為你收集整理的Apache ZooKeeper - 使用原生的API操作ZK_ACL权限的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache ZooKeeper - 高
- 下一篇: Apache ZooKeeper - Z