[ZooKeeper]ZooKeeper Java客户端ACL API

The following constants are provided by the ZooKeeper Java library:

The following are the standard ACL IDs:

ZooKeeper client comes with three standard ACLs:

  • ZooDefs.Ids.OPEN_ACL_UNSAFE; //(ZooDefs.Perms.ALL, ZooDefs.Ids.ANYONE_ID_UNSAFE)
  • ZooDefs.Ids.READ_ACL_UNSAFE;// (ZooDefs.Perms.READ, ZooDefs.Ids.ANYONE_ID_UNSAFE)
  • ZooDefs.Ids.CREATOR_ALL_ACL; //(ZooDefs.Perms.ALL, ZooDefs.Ids.AUTH_IDS)
    The ZooDefs.Ids.OPEN_ACL_UNSAFE is completely open free for all ACL: any application can execute any operation on the node and can create, list and delete its children. The ZooDefs.Ids.READ_ACL_UNSAFE is read-only access for any application. ZooDefs.Ids.CREATOR_ALL_ACL grants all permissions to the creator of the node. The creator must have been authenticated by the server (for example, using “digest” scheme) before it can create nodes with this ACL.

The following ZooKeeper operations deal with ACLs:

Here is a sample code that makes use of the above APIs to authenticate itself using the “foo” scheme and create an ephemeral node “/xyz” with create-only permissions.

Note
This is a very simple example which is intended to show how to interact with ZooKeeper ACLs specifically.

package felix.zookeeper.examples.acl;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

public class ACLExample {

    /**
     * In this example this method gets the cert for your
     *   environment -- you must provide
     */
    public static String fooGetCertOnce(String id) {
        return null;
    }

    /** 
     * Watcher function -- empty for this example, not something you should
     * do in real code 
     * */
    private static Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
        }
    };

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        String appId = "example.foo_test";
        String cert = fooGetCertOnce(appId);
        if (cert != null) {
            System.out.printf("Certificate for appid [%s] is [%s]\n", appId, cert);
        } else {
            System.err.printf( "Certificate for appid [%s] not found\n", appId);
            cert = "dummy";
        }

        ZooKeeper client = new ZooKeeper("localhost:3181", 10000, watcher, false);



        try {
            client.addAuthInfo("foo", cert.getBytes());

            List<ACL> CREATE_ONLY = Arrays.asList(new ACL[] { new ACL(ZooDefs.Perms.CREATE, ZooDefs.Ids.AUTH_IDS) });
            client.create("/xyz", "value".getBytes(), CREATE_ONLY, CreateMode.EPHEMERAL);
            Stat stat = new Stat();

            // this operation will fail with a NoAuthException
            client.getData("/xyz", watcher, stat);
        } catch (KeeperException.NoAuthException e) {
            System.err.printf("Error %d for %s\n", e.getMessage());
        } finally {
            client.close();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值