首页 > 其他分享 >中间件 ZK分布式专题与Dubbo微服务入门 6-13 acl - ip权限

中间件 ZK分布式专题与Dubbo微服务入门 6-13 acl - ip权限

时间:2024-04-04 09:22:39浏览次数:24  
标签:Dubbo 13 org zookeeper 中间件 zkServer new apache import

0    课程地址

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

 

1    重点关注

1.1    本节内容

通过schema为ip的方式设置权限,只有指定ip才能操作

 

1.2    关键代码

    // ip方式的acl
        List<ACL> aclsIP = new ArrayList<ACL>();
        Id ipId1 = new Id("ip", "172.26.128.1");
        aclsIP.add(new ACL(Perms.ALL, ipId1));
        zkServer.createZKNode("/aclimooc/iptest9", "iptest".getBytes(), aclsIP);

        // 验证ip是否有权限
       zkServer.getZookeeper().setData("/aclimooc/iptest9", "now".getBytes(), 0);
       Stat stat = new Stat(); 
       byte[] data =  zkServer.getZookeeper().getData("/aclimooc/iptest9", false, stat);
       System.out.println(new String(data)); 
       System.out.println(stat.getVersion());

 

 

2    课程内容

 

 

3    Coding

3.1    用一个非本机的ip创建节点,然后访问,会发现没有权限

  • 启动服务端
    进入到
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.43.206");
        aclsIP.add(new ACL(Perms.ALL, ipId1));
        zkServer.createZKNode("/aclimooc/iptest6", "iptest".getBytes(), aclsIP);

        // 验证ip是否有权限
       zkServer.getZookeeper().setData("/aclimooc/iptest6", "now".getBytes(), 0);
       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) {
        
    }
}

 

  • 加密工具类:
package com.imooc.utils;

import java.io.IOException;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;

public class AclUtils {
    
    public static String getDigestUserPwd(String id) throws Exception {
        return DigestAuthenticationProvider.generateDigest(id);
    }
    
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException, Exception {
        String id = "imooc:imooc";
        String idDigested = getDigestUserPwd(id);
        System.out.println(idDigested);
    }
}

 

 

  • 打印日志1(报错原因为创建节点没有用设置创建权限的用户操作)
创建节点: /aclimooc/iptest6 成功...

Exception in thread "main" org.apache.zookeeper.KeeperException$NoAuthException: KeeperErrorCode = NoAuth for /aclimooc/iptest6

at org.apache.zookeeper.KeeperException.create(KeeperException.java:116)

at org.apache.zookeeper.KeeperException.create(KeeperException.java:54)

at org.apache.zookeeper.ZooKeeper.setData(ZooKeeper.java:1330)

at com.imooc.zk.demo.ZKNodeAcl.main(ZKNodeAcl.java:110)

 

 

 

3.2    用本机的ip创建节点,然后访问,会发现有权限

  • 主类:
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", "172.26.128.1");
        aclsIP.add(new ACL(Perms.ALL, ipId1));
        zkServer.createZKNode("/aclimooc/iptest9", "iptest".getBytes(), aclsIP);

        // 验证ip是否有权限
       zkServer.getZookeeper().setData("/aclimooc/iptest9", "now".getBytes(), 0);
       Stat stat = new Stat(); 
       byte[] data =  zkServer.getZookeeper().getData("/aclimooc/iptest9", 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) {
        
    }
}

 

  • 打印日志
创建节点:    /aclimooc/iptest9    成功...
now
1

 

 

标签:Dubbo,13,org,zookeeper,中间件,zkServer,new,apache,import
From: https://www.cnblogs.com/1446358788-qq/p/18113915

相关文章

  • 中间件 ZK分布式专题与Dubbo微服务入门 6-12 acl -自定义用户权限
    0课程地址https://coding.imooc.com/lesson/201.html#mid=12728 1重点关注1.1本节内容通过schema为digest密文的形式设置用户权限,调用权限 1.2关键代码//自定义用户认证访问List<ACL>acls=newArrayList<ACL>();......
  • dubbo 统一异常处理
    依赖包pom.xml如下:<dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-nacos</artifactId><version>3.0.4</version></dependency>dubbo服务:示例:@DubboService(interfaceClass=......
  • Nginx 配置反向代理时出现 502 Bad Gateway (13_ Permission denied) while connectin
    我们在Nginx配置反向代理后,可能会出现如下报错:502BadGateway(13:Permissiondenied)whileconnectingtoupstreamSorry,thepageyouarelookingforiscurrentlyunavailable.<br/>Pleasetryagainlater.这些错误,一般是由SELinux引起的(https://stackoverf......
  • leetcode每日一题 1379. 找出克隆二叉树中的相同节点
    问题描述给你两棵二叉树,原始树original和克隆树cloned,以及一个位于原始树original中的目标节点target。其中,克隆树cloned是原始树original的一个副本。请找出在树cloned中,与target相同的节点,并返回对该节点的引用(在C/C++等有指针的语言中返回节点指针,其他......
  • 酷睿i7 14650HX和i7 13700hx选哪个 i714650HX和i713700hx对比
    i713700hx采用10nm工艺16个核和24个线程,基本的频率为2.1GHZ,甚至可以提升到4.96ghz。三级缓存55MB热设计功耗(TDP)55W支持最大内存128GB内存类型DDR43200MHzDDR54800MHz集成显卡IntelUHDGraphics选i713700hx还是i714650HX这些点很重要看过你就懂了http://ww......
  • 评测 酷睿i7 14650HX和i9 13980HX选哪个 i714650HX和i913980HX差距
    i913980HX采用10纳米制作工艺最高睿频5.6GHz二十四核心三十二线程三级缓存36MB热设计功耗(TDP)157W支持最大内存128GB内存类型DDR43200MHzDDR55600MHz集成显卡IntelIrisXeGraphics选i913980HX还是i714650HX这些点很重要看过你就懂了http://www.adiann......
  • LeetCode-1379. 找出克隆二叉树中的相同节点【树 深度优先搜索 广度优先搜索 二叉树】
    LeetCode-1379.找出克隆二叉树中的相同节点【树深度优先搜索广度优先搜索二叉树】题目描述:解题思路一:递归,由于我们比较的是节点而不是节点值(例如C++比较的是地址),所以下面的代码也适用于树中有值相同节点的情况(本题的进阶问题)。解题思路二:递归这题有几个关键点,一:判......
  • 代码随想录 Day34 贪心算法 1005.K次取反后最大化的数组和 134. 加油站 135. 分发糖果
    1005.K次取反后最大化的数组和 classSolution{public:intlargestSumAfterKNegations(vector<int>&nums,intk){sort(nums.begin(),nums.end());intsum=0;inti=0;while(k>0){nums[i]=0-nums[i]......
  • 13.Android 四大组件之一活动单元Activity总结 上
    Activity是一个负责与用户交互的组件即我们常用的layout和Activity_java类1生命周期的探索五种状态七种方法启动状态很短暂当Acitivity启动后便会进入运行状态运行状态用户交互界面Android会尽可能保持这种状态暂停状态用户操作无响应被覆盖的Activit......
  • 洛谷题单指南-图的基本应用-P1347 排序
    原题链接:https://www.luogu.com.cn/problem/P1347题意解读:在给出多对关系字母的比较关系之后,判断能否确定所有字母的顺序。解题思路:对字母的关系建立图,如A<B建立A指向B的一条边。如果在拓扑排序过程中,每次寻找入度为0的点只有一个,且最终可以形成拓扑序,则可以确定所有字母的顺......