zookpeer use curator of ACL

1.zk的权限:
  • Zookeeper对权限的控制是节点级别的,而且不继承,即对父节点设置权限,其子节点不继承父节点的权限
2.zk 对于权限认证的几种方式:
  • world:有个单一的ID,anyone,表示任何人。
  • auth:不使用任何ID,表示任何通过验证的用户(是通过ZK验证的用户?连接到此ZK 服务器的用户?)。
  • digest:使用 用户名:密码 字符串生成MD5哈希值作为ACL标识符ID。权限的验证通过直接发送用户名密码字符串的方式完成,
  • ip:使用客户端主机ip地址作为一个ACL标识符,ACL表达式是以 addr/bits 这种格式表示的。ZK服务器会将addr的前bits位与客户端地址的前bits位来进行匹配验证权限。
3.Java curator of ACL
 public static void main(String[] args) throws Exception {
        String zkUsername="admin2";
        String zkPassword="123";
        CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
                .connectString("localhost:2181")
                .retryPolicy(new ExponentialBackoffRetry(2000, 5))
                .connectionTimeoutMs(2000)
                .sessionTimeoutMs(3000);

        if (zkUsername != null && zkPassword != null) {
            String authenticationString = zkUsername + ":" + zkPassword;
            builder.authorization("digest", authenticationString.getBytes())
                    .aclProvider(new ACLProvider() {
                        @Override
                        public List<ACL> getDefaultAcl() {
                            return ZooDefs.Ids.CREATOR_ALL_ACL;
                        }

                        @Override
                        public List<ACL> getAclForPath(String path) {
                            return ZooDefs.Ids.CREATOR_ALL_ACL;
                        }
                    });
        }

        CuratorFramework client = builder.build();
        client.start();
        System.out.println("client start .......");
//        byte[] bytes = client.getData().forPath("/bizseer/platform/atlas/ccb_1.1.1");
//        System.out.println(new String(bytes));
//        client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/ZNode/path");
//        client.setData().forPath("/ZNode/path","your data is path".getBytes());
//        byte[] bytes = client.getData().forPath("/ZNode/path");
//        String s = new String(bytes);
//        System.out.println(s);
        创建ACL权限
//        ArrayList<ACL> acls = new ArrayList<>();
//        Id id1=new Id("digest", DigestAuthenticationProvider.generateDigest("tom1:tom1"));
//        Id id2=new Id("digest", DigestAuthenticationProvider.generateDigest("tom3:tom3"));
//        acls.add(new ACL(ZooDefs.Perms.ADMIN,id1));
//        acls.add(new ACL(ZooDefs.Perms.CREATE,id2));
//        acls.add(new ACL(ZooDefs.Perms.ADMIN | ZooDefs.Perms.READ,id2));
//        
#######################################################################
withACL(acls,true)这个true表示是创建的父级也是这个权限,如果不写true的话,那么只有
001 末节点是acls权限,如果client也设置了权限,那么父节点和子节点是不同的权限,父节点会是client的设置的权限
client.create().creatingParentsIfNeeded().withACL(acls,true).forPath("/zhangyingjie001/001","zyj".getBytes());
#######################################################################
//  设置ACL权限
//        client.create().creatingParentsIfNeeded().forPath("/zhangyingjie004/004","zyj".getBytes());
//        ArrayList<ACL> acls = new ArrayList<ACL>();
//        Id id1 = new Id("digest", DigestAuthenticationProvider.generateDigest("admin1:123"));
//        Id id2 = new Id("digest", DigestAuthenticationProvider.generateDigest("admin2:123"));
//        acls.add(new ACL(ZooDefs.Perms.ADMIN, id1));
//        acls.add(new ACL(ZooDefs.Perms.CREATE, id2));
//        acls.add(new ACL(ZooDefs.Perms.ADMIN | ZooDefs.Perms.READ, id2));
//        client.setACL().withACL(acls).forPath("/zhangyingjie002/002");
//        Stat stat = new Stat();
//        System.out.println(new String(client.getData().storingStatIn(stat).forPath("/zhangyingjie002/002")));

#
//        client.create().creatingParentsIfNeeded().forPath("/zhangyingjie003/003","zyj".getBytes());
//        ArrayList<ACL> acls = new ArrayList<ACL>();
//        Id id1 = new Id("digest", DigestAuthenticationProvider.generateDigest("tim1:tim1"));
//        Id id2 = new Id("digest", DigestAuthenticationProvider.generateDigest("tim2:tim2"));
//        acls.add(new ACL(ZooDefs.Perms.ADMIN, id1));
//        acls.add(new ACL(ZooDefs.Perms.CREATE, id2));
//        acls.add(new ACL(ZooDefs.Perms.ADMIN | ZooDefs.Perms.READ, id2));
//        client.setACL().withACL(acls).forPath("/zhangyingjie003/003");

//        client.create().creatingParentsIfNeeded().forPath("/zhangyingjie004/004","zyj".getBytes());
//        System.out.println("*******************");

###########################################################
验证这种方式不能读取已经授权的节点的信息
###########################################################
        ZooKeeper zooKeeper = client.getZookeeperClient().getZooKeeper();
        zooKeeper.addAuthInfo("tim2","tim2".getBytes());
        zooKeeper.addAuthInfo("tim1","tim1".getBytes());
        zooKeeper.addAuthInfo("admin2","123".getBytes());
        Stat stat=new Stat();
        byte[] data = zooKeeper.getData("/zhangyingjie003/003", true, stat);
        System.out.println(new String(data));

//        System.out.println(new String(client.getData().forPath("/zhangyingjie003")));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值