中間件 ZK分佈式專題與Dubbo微服務入門 6-11 acl - 默認匿名權限

0    課程地址

https://coding.imooc.com/lesson/201.html#mid=12727

 

1    重點關注

1.1    本節內容

acl設置 - 默認匿名權限

 

1.2    關鍵代碼

            /**
             * 同步或者異步創建節點,都不支持子節點的遞歸創建,異步有一個callback函數
             * 參數:
             * path:創建的路徑
             * data:存儲的數據的byte[]
             * acl:控制權限策略
             *             Ids.OPEN_ACL_UNSAFE --> world:anyone:cdrwa
             *             CREATOR_ALL_ACL --> auth:user:password:cdrwa
             * createMode:節點類型, 是一個枚舉
             *             PERSISTENT:持久節點
             *             PERSISTENT_SEQUENTIAL:持久順序節點
             *             EPHEMERAL:臨時節點
             *             EPHEMERAL_SEQUENTIAL:臨時順序節點
             */
            result = zookeeper.create(path, data, acls, CreateMode.PERSISTENT);

 

 

        zkServer.createZKNode("/aclimooc", "test".getBytes(), Ids.OPEN_ACL_UNSAFE);

 

 

 

2    課程內容

 

 

3    Coding

3.1    acl設置 - 默認匿名權限

  • 啓動服務端
    進入到
cd /usr/local/zookeeper/bin

 
    重啓zookeeper服務端
./zkServer.sh restart

 

  • 主類
package com.imooc.zk.demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

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.Ids;
import org.apache.zookeeper.ZooDefs.Perms;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;

import com.imooc.utils.AclUtils;

/**
 * 
 * @Description: zookeeper 操作節點acl演示
 */
public class ZKNodeAcl implements Watcher {

    private ZooKeeper zookeeper = null;
    
    public static final String zkServerPath = "172.26.139.4:2181";
    public static final Integer timeout = 5000;
    
    public ZKNodeAcl() {}
    
    public ZKNodeAcl(String connectString) {
        try {
            zookeeper = new ZooKeeper(connectString, timeout, new ZKNodeAcl());
        } catch (IOException e) {
            e.printStackTrace();
            if (zookeeper != null) {
                try {
                    zookeeper.close();
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
    
    public void createZKNode(String path, byte[] data, List<ACL> acls) {
        
        String result = "";
        try {
            /**
             * 同步或者異步創建節點,都不支持子節點的遞歸創建,異步有一個callback函數
             * 參數:
             * path:創建的路徑
             * data:存儲的數據的byte[]
             * acl:控制權限策略
             *             Ids.OPEN_ACL_UNSAFE --> world:anyone:cdrwa
             *             CREATOR_ALL_ACL --> auth:user:password:cdrwa
             * createMode:節點類型, 是一個枚舉
             *             PERSISTENT:持久節點
             *             PERSISTENT_SEQUENTIAL:持久順序節點
             *             EPHEMERAL:臨時節點
             *             EPHEMERAL_SEQUENTIAL:臨時順序節點
             */
            result = zookeeper.create(path, data, acls, CreateMode.PERSISTENT);
            System.out.println("創建節點:\t" + result + "\t成功...");
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } 
    }
    
    public static void main(String[] args) throws Exception {
    
        ZKNodeAcl zkServer = new ZKNodeAcl(zkServerPath);
        
        /**
         * ======================  創建node start  ======================  
         */
        // acl 任何人都可以訪問
        zkServer.createZKNode("/aclimooc", "test".getBytes(), Ids.OPEN_ACL_UNSAFE);
        
        // 自定義用戶認證訪問
//        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));
//        zkServer.createZKNode("/aclimooc/testdigest", "testdigest".getBytes(), acls);
        
        // 註冊過的用戶必須通過addAuthInfo才能操作節點,參考命令行 addauth
//        zkServer.getZookeeper().addAuthInfo("digest", "imooc1:123456".getBytes());
//        zkServer.createZKNode("/aclimooc/testdigest/childtest", "childtest".getBytes(), Ids.CREATOR_ALL_ACL);
//        Stat stat = new Stat();
//        byte[] data = zkServer.getZookeeper().getData("/aclimooc/testdigest", false, stat);
//        System.out.println(new String(data));
//        zkServer.getZookeeper().setData("/aclimooc/testdigest", "now".getBytes(), 1);
        
        // ip方式的acl
//        List<ACL> aclsIP = new ArrayList<ACL>();
//        Id ipId1 = new Id("ip", "192.168.1.6");
//        aclsIP.add(new ACL(Perms.ALL, ipId1));
//        zkServer.createZKNode("/aclimooc/iptest6", "iptest".getBytes(), aclsIP);

        // 驗證ip是否有權限
        /*
         * zkServer.getZookeeper().setData("/aclimooc/iptest6", "now".getBytes(), 1);
         * Stat stat = new Stat(); byte[] data =
         * zkServer.getZookeeper().getData("/aclimooc/iptest6", false, stat);
         * System.out.println(new String(data)); System.out.println(stat.getVersion());
         */
    }

    public ZooKeeper getZookeeper() {
        return zookeeper;
    }
    public void setZookeeper(ZooKeeper zookeeper) {
        this.zookeeper = zookeeper;
    }

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

 

  • 打印日誌1

創建節點: /aclimooc 成功...

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章