ZooKeeper Notes 7使用super身份对有权限的节点进行操作.docx

上传人:b****6 文档编号:8498550 上传时间:2023-01-31 格式:DOCX 页数:3 大小:15.70KB
下载 相关 举报
ZooKeeper Notes 7使用super身份对有权限的节点进行操作.docx_第1页
第1页 / 共3页
ZooKeeper Notes 7使用super身份对有权限的节点进行操作.docx_第2页
第2页 / 共3页
ZooKeeper Notes 7使用super身份对有权限的节点进行操作.docx_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

ZooKeeper Notes 7使用super身份对有权限的节点进行操作.docx

《ZooKeeper Notes 7使用super身份对有权限的节点进行操作.docx》由会员分享,可在线阅读,更多相关《ZooKeeper Notes 7使用super身份对有权限的节点进行操作.docx(3页珍藏版)》请在冰豆网上搜索。

ZooKeeper Notes 7使用super身份对有权限的节点进行操作.docx

ZooKeeperNotes7使用super身份对有权限的节点进行操作

【ZooKeeperNotes7】使用super身份对有权限的节点进行操作

【ZooKeeperNotes7】使用super身份对有权限的节点进行操作

 

2012-07-1408:

08:

00

标签:

权限super实践zookeeperdigest

 

原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。

否则将追究法律责任。

 

转载请用注明:

@ni掌柜nileader@

如果客户端设置了权限,那么其它人如果没有授权,就无法对这个节点进行操作。

但是对于管理员来说,有没有一种方法,可以对任意节点进行操作呢,答案是有的~

方法简单描述如下:

1.确认是否开启zookeeper的superDigest模式。

方法如下:

首先配置如下启动参数,然后重启server

"-Dzookeeper.DigestAuthenticationProvider.superDigest=super:

/7ahZf2EjED/untmtb2NRkHhVlA="2.在java代码中进行digest模式的授权,方法如下:

zkClient.addAuthInfo("digest","super:

yinshi.nc-1988".getBytes());

3.具体样例参见下面:

importjava.util.ArrayList;importjava.util.List;importorg.apache.zookeeper.WatchedEvent;importorg.apache.zookeeper.Watcher;importorg.apache.zookeeper.ZooDefs.Ids;importorg.apache.zookeeper.data.ACL;importorg.apache.zookeeper.server.auth.DigestAuthenticationProvider;/***Description:

ZooKeeper-AuthenticationTest*@authornileader/nileader@*@DateJul12,2012*/publicclassDemoAuth2implementsWatcher{finalstaticStringSERVER_LIST="127.0.0.1:

2181";finalstaticStringPATH="/yinshi_auth_test";finalstaticStringPATH_DEL="/yinshi_auth_test/will_be_del";finalstaticStringauthentication_type="digest";finalstaticStringcorrectAuthentication="taokeeper:

true";finalstaticStringbadAuthentication="taokeeper:

errorCode";finalstaticStringsuperAuthentication="super:

yinshi.nc-1988";staticZkClientzkClient=null;publicstaticvoidmain(String[]args)throwsException{System.out.println(DigestAuthenticationProvider.generateDigest("super:

yinshi.nc-1988"));List<ACL>acls=newArrayList<ACL>

(1);for(ACLids_acl:

Ids.CREATOR_ALL_ACL){acls.add(ids_acl);}try{zkClient=newZkClient(SERVER_LIST,50000);zkClient.addAuthInfo(authentication_type,correctAuthentication.getBytes());}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}try{zkClient.createPersistent(PATH,acls,"initcontent");System.out.println("使用授权key:

"+correctAuthentication+"创建节点:

"+PATH+",初始内容是:

initcontent");}catch(Exceptione){e.printStackTrace();}try{zkClient.createPersistent(PATH_DEL,acls,"待删节点");System.out.println("使用授权key:

"+correctAuthentication+"创建节点:

"+PATH_DEL+",初始内容是:

initcontent");}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}//获取数据getDataByNoAuthentication();getDataByBadAuthentication();getDataByCorrectAuthentication();getDataByBadAuthentication();getDataBySuperAuthentication();//////更新数据//updateDataByNoAuthentication();//updateDataByBadAuthentication();//updateDataByCorrectAuthentication();//////获取数据//getDataByNoAuthentication();//getDataByBadAuthentication();//getDataByCorrectAuthentication();//////删除数据//deleteNodeByBadAuthentication();//deleteNodeByNoAuthentication();//deleteNodeByCorrectAuthentication();////deleteParent();zkClient.close();}/**获取数据:

采用错误的密码*/staticvoidgetDataByBadAuthentication(){Stringprefix="[使用错误的授权信息]";try{System.out.println(prefix+"获取数据:

"+PATH);zkClient=newZkClient(SERVER_LIST,50000);zkClient.addAuthInfo(authentication_type,badAuthentication.getBytes());System.out.println(prefix+"成功获取数据:

"+zkClient.readData(PATH));}catch(Exceptione){System.err.println(prefix+"获取数据失败,原因:

"+e.getMessage());}}/**获取数据:

不采用密码*/staticvoidgetDataByNoAuthentication(){Stringprefix="[不使用任何授权信息]";try{System.out.println(prefix+"获取数据:

"+PATH);zkClient=newZkClient(SERVER_LIST,50000);System.out.println(prefix+"成功获取数据:

"+zkClient.readData(PATH));}catch(Exceptione){System.err.println(prefix+"获取数据失败,原因:

"+e.getMessage());}}/**采用正确的密码*/staticvoidgetDataByCorrectAuthentication(){Stringprefix="[使用正确的授权信息]";try{System.out.println(prefix+"获取数据:

"+PATH);zkClient=newZkClient(SERVER_LIST,50000);zkClient.addAuthInfo(authentication_type,correctAuthentication.getBytes());System.out.println(prefix+"成功获取数据:

"+zkClient.readData(PATH));}catch(Exceptione){System.out.println(prefix+"获取数据失败,原因:

"+e.getMessage());}}/**采用超级用户的密码*/staticvoidgetDataBySuperAuthentication(){Stringprefix="[使用超级用户的授权信息]";try{System.out.println(prefix+"获取数据:

"+PATH);zkClient=newZkClient(SERVER_LIST,50000);zkClient.addAuthInfo(authentication_type,superAuthentication.getBytes());System.out.println(prefix+"成功获取数据:

"+zkClient.readData(PATH));}catch(Exceptione){System.out.println(prefix+"获取数据失败,原因:

"+e.getMessage());}}/***更新数据:

不采用密码*/staticvoidupdateDataByNoAuthentication(){Stringprefix="[不使用任何授权信息]";System.out.println(prefix+"更新数据:

"+PATH);try{zkClient=newZkClient(SERVER_LIST,50000);if(zkClient.exists(PATH)){zkClient.writeData(PATH,prefix);System.out.println(prefix+"更新成功");}}catch(Exceptione){System.err.println(prefix+"更新失败,原因是:

"+e.getMessage());}}/***更新数据:

采用错误的密码*/staticvoidupdateDataByBadAuthentication(){Stringprefix="[使用错误的授权信息]";System.out.println(prefix+"更新数据:

"+PATH);try{zkClient=newZkClient(SERVER_LIST,50000);zkClient.addAuthInfo(authentication_type,badAuthentication.getBytes());if(zkClient.exists(PATH)){zkClient.writeData(PATH,prefix);System.out.println(prefix+"更新成功");}}catch(Exceptione){System.err.println(prefix+"更新失败,原因是:

"+e.getMessage());}}/***更新数据:

采用正确的密码*/staticvoidupdateDataByCorrectAuthentication(){Stringprefix="[使用正确的授权信息]";System.out.println(prefix+"更新数据:

"+PATH);try{zkClient=newZkClient(SERVER_LIST,50000);zkClient.addAuthInfo(authentication_type,correctAuthentication.getBytes());if(zkClient.exists(PATH)){zkClient.writeData(PATH,prefix);System.out.println(prefix+"更新成功");}}catch(Exceptione){System.err.println(prefix+"更新失败,原因是:

"+e.getMessage());}}/***不使用密码删除节点*/staticvoiddeleteNodeByNoAuthentication()throwsException{Stringprefix="[不使用任何授权信息]";try{System.out.println(prefix+"删除节点:

"+PATH_DEL);zkClient=newZkClient(SERVER_LIST,50000);if(zkClient.exists(PATH_DEL)){zkClient.delete(PATH_DEL);System.out.println(prefix+"删除成功");}}catch(Exceptione){System.err.println(prefix+"删除失败,原因是:

"+e.getMessage());}}/***采用错误的密码删除节点*/staticvoiddeleteNodeByBadAuthentication()throwsException{Stringprefix="[使用错误的授权信息]";try{System.out.println(prefix+"删除节点:

"+PATH_DEL);zkClient=newZkClient(SERVER_LIST,50000);zkClient.addAuthInfo(authentication_type,badAuthentication.getBytes());if(zkClient.exists(PATH_DEL)){zkClient.delete(PATH_DEL);System.out.println(prefix+"删除成功");}}catch(Exceptione){System.err.println(prefix+"删除失败,原因是:

"+e.getMessage());}}/***使用正确的密码删除节点*/staticvoiddeleteNodeByCorrectAuthentication()throwsException{Stringprefix="[使用正确的授权信息]";try{System.out.println(prefix+"删除节点:

"+PATH_DEL);zkClient=newZkClient(SERVER_LIST,50000);zkClient.addAuthInfo(authentication_type,correctAuthentication.getBytes());if(zkClient.exists(PATH_DEL)){zkClient.delete(PATH_DEL);System.out.println(prefix+"删除成功");}}catch(Exceptione){System.out.println(prefix+"删除失败,原因是:

"+e.getMessage());}}/***使用正确的密码删除节点*/staticvoiddeleteParent()throwsException{try{zkClient=newZkClient(SERVER_LIST,50000);zkClient.addAuthInfo(authentication_type,correctAuthentication.getBytes());if(zkClient.exists(PATH)){zkClient.delete(PATH);}}catch(Exceptione){e.printStackTrace();}}@Overridepublicvoidprocess(WatchedEventevent){//TODOAuto-generatedmethodstub}}

本文出自“ni掌柜的笔记”博客,请务必保留此出处

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1