DeemOpen / zkui

A UI dashboard that allows CRUD operations on Zookeeper.
2.36k stars 977 forks source link

can it support zookeeper password settings? #69

Open keepongjl opened 5 years ago

keepongjl commented 5 years ago

Haven't seen zookeeper userName and password config properties in the config.cfg,can you support it,thank you

shangjiahao commented 4 years ago

Easy,you can write code by yourself.It after zookeeper class create.The add zookeeper.addAuthInfo method and get property in the config.cfg.Show my code.

//ZookeeperUtil.class
    /**
     * addAuthInfo
     * @param zooKeeper    zk client connect
     * @param jsonAuthInfo 认证信息
     */
    public void addAuthInfo(ZooKeeper zooKeeper, String jsonAuthInfo) {
        if (jsonAuthInfo == null || jsonAuthInfo.trim().length() == 0) {
            return;
        } else {
            try {
                com.alibaba.fastjson.JSONObject authInfo = JSON.parseObject(jsonAuthInfo);
                String scheme = authInfo.getString("scheme");
                String auth = authInfo.getString("auth");
                if (auth == null || auth.trim().length() == 0) {
                    throw new RuntimeException();
                }
                zooKeeper.addAuthInfo(scheme, auth.getBytes());
            } catch (Exception e) {
                throw new RuntimeException("Unable to parse auth info " + jsonAuthInfo, e);
            }
        }
    }

//ServletUtil.class
  public ZooKeeper getZookeeper(HttpServletRequest request, HttpServletResponse response, String zkServer, Properties globalProps) {
        try {

            HttpSession session = request.getSession();
            ZooKeeper zk = (ZooKeeper) session.getAttribute("zk");
            if (zk == null || zk.getState() != ZooKeeper.States.CONNECTED) {
                Integer zkSessionTimeout = Integer.parseInt(globalProps.getProperty("zkSessionTimeout"));
                //Converting seconds to ms.
                zkSessionTimeout = zkSessionTimeout * 1000;
                zk = ZooKeeperUtil.INSTANCE.createZKConnection(zkServer, zkSessionTimeout);
                //here 
                ZooKeeperUtil.INSTANCE.addAuthInfo(zk,globalProps.getProperty("authInfo"));
                ZooKeeperUtil.INSTANCE.setDefaultAcl(globalProps.getProperty("defaultAcl"));
                if (zk.getState() != ZooKeeper.States.CONNECTED) {
                    session.setAttribute("zk", null);
                } else {
                    session.setAttribute("zk", zk);
                }

            }
            return zk;
        } catch (IOException | InterruptedException ex) {
            logger.error(Arrays.toString(ex.getStackTrace()));
        }
        return null;
    }

config.cfg authInfo={"scheme":"digest","auth":"super:xxx"}

keepongjl commented 4 years ago

thanks