属性Sun Directory ServerLDAP学习笔记(二)——API说明及代码样例

directory listing denied  时间:2021-01-22  阅读:()

从JDK5.0开始对LDAP协议的数据访问操作就被集成在javax的扩展API包中并随同JDK一并发布这一章节我们主要介绍API包中的类信息。javax.naming.directory包的结构

常用AP I解析javax.naming.directory. InitialDirContext初始化目录服务上下文类该类是LDAP数据内容的操作工具类通过该类可以执行绑定LDAP服务器、新增LDAP条目、获取条目实例、修改条目属性、删除条目和根据条件搜索条目等操作。常用方法说明如下

初始化LDAP 目录服务上下文(相当于使用JDBC打开一个数据库链接)

 InitialDirContext(Hashtable<?,?> environment)

绑定/创建LDAP条目对象相当于新增一个LDAP条目数据bind(Name name, Object obj, Attributes attrs)

 bind(String name, Object obj, Attributes attrs)

 createSubcontext(Name name, Attributes attrs)

 createSubcontext(String name, Attributes attrs)

获取条目实例属性集

 getAttributes(Name name)

 getAttributes(Name name, String[] attrIds)

 getAttributes(String name)

 getAttributes(String name, String[] attrIds)

修改条目属性

 modifyAttributes(Name name, int mod_op, Attributes attrs) modifyAttributes(Name name, ModificationItem[] mods) modifyAttributes(String name, int mod_op, Attributes attrs) modifyAttributes(String name, ModificationItem[] mods)

删除条目

 destroySubcontext(Name name)

 destroySubcontext(String name)

根据属性集搜索条目

 search(Name name, Attributes matchingAttributes)

 search(Name name, Attributes matchingAttributes, String[]attributesToReturn)

 search(String name, Attributes matchingAttributes)

 search(String name, Attributes matchingAttributes, String[]attributesToReturn)

根据过滤器搜索条目

 search(Name name, String filterExpr, Object[] filterArgs,SearchControls cons)

 search(Name name, String filter, SearchControls cons)

 search(String name, String filterExpr, Object[] filterArgs,

SearchControls cons)

 search(String name, String filter, SearchControls cons)javax.naming.directory.BasicAttribute LDAP基本属性对象

该类用来表示LDAP条目中的单个属性对象。在目录服务中每个属性名称是可以对应多个的属性值的。

构建属性对象

 BasicAttribute(String id)

 BasicAttribute(String id, boolean ordered)

 BasicAttribute(String id, Object value)

 BasicAttribute(String id, Object value, boolean ordered)添加属性值

 add(int ix, Object attrVal) 添加属性值到多值属性的指定位置

 add(Object attrVal)  追加属性值到多值属性尾部

判断属性值是否包含

 contains(Object attrVal)  多值属性中有一个值是匹配的返回true获取属性值

 get() 取得属性值中的一个

 get(int ix) 从多值属性中的指定位置取值

获取属性ID

 getID() 属性的ID就是属性名

删除属性值

 remove(int ix) 删除指定位置的属性值

 remove(Object attrval) 删除指定的属性值

javax.naming.directory.BasicAttributes LDAP实体的属性集

该类表示一个LDAP条目绑定的属性集合在绝大多数情况下一个LDAP条目存在多个属性。

构造属性集

 BasicAttributes()

 BasicAttributes(boolean ignoreCase) 属性ID是否大小写敏感建议

不要使用敏感

 BasicAttributes(String attrID, Object val)

 BasicAttributes(String attrID, Object val, boolean ignoreCase)获取属性集中的单个属性对象

 get(String attrID)

获取全部属性的枚举

 getAll ()

获取全部属性的ID枚举

 getIDs()

添加新属性

 put(Attribute attr)

 put(String attrID, Object val)

移除指定属性

 remove(String attrID)javax.naming.directory.SearchControls , LDAP目录服务搜索控制对象该类负责控制LDAP搜索行为的范围、设定返回结果数上限搜索耗时上限指定结果所包括的属性集等。

设定搜索行为的范围

 setSearchScope(int scope)

设定返回结果数上限

 setCountLimit(long limit)

设定搜索耗时上限

 setTimeLimit(int ms)  以毫秒为单位

指定结果所包括的属性集

 setReturningAttributes(String[] attrs)javax.naming.directory.SearchResult  表示. search()方法的返回结果集中的一项。

SearchResult类是对LDAP条目属性集的封装。在search()操作中可能返回完整的条目属性也可能是条目属性的一部分。

获取SearchResult封装的条目属性

 getAttributes()

以上只列举了LDAP操作API的常用部分更多更详细的描述请参考SunJava6.0 API DOC。

LDAP操作代码样例

在这个章节中我们将结合LDAP操作的代码实例了解API使用。

初始化LDAP 目录服务上下文

该例子中我们使用uid=linly,ou=People,dc=jsoso,dc=net这个账号链接位于本机8389端口的LDAP服务器ldap://localhost:8389 认证方式采用simple类型 即用户名/密码方式。

Java代码

1. private static void initialContext() throws NamingException{

2. if(singleton == null) {

3. singleton = new LDAPConnection() ;

4. /*

5. *在实际编码中这些环境变量应尽可能通过配置文件读取

6. */

7. //LDAP服务地址

8. singleton. sLDAP_URL = "ldap://localhost:8389";

9. //管理员账号

10. singleton. sMANAGER_DN = "uid=linly,ou=People,dc=jsoso,dc=net;

11. //管理员密码

12. s ingleton. sMANAGER_PASSWORD = "coffee";

13. //认证类型

14. singleton. sAUTH_TYPE = "simple";

15. //JNDI Context工厂类

16. singleton. sCONTEXT_FACTORY = "com. sun. jndi. ldap.LdapCtxFactory";

17.

18. singleton.envProps. setProperty(Context. INITIAL_CONTEXT_FACTORY, singleton. sCONTEXT_FACTORY) ;

19. singleton.envProps. setProperty(Context.PROVIDER_URL, singleton. sLDAP_URL) ;

20. singleton.envProps. setProperty(Context.SECURITY_AUTHENTICATION, singleton. sAUTH_TYPE) ;

21. singleton.envProps. setProperty(Context.SECURITY_PRINCIPAL, s ingl eton. sMANAGER_DN) ;

22. singleton.envProps. setProperty(Context.SECURITY_CREDENTIALS, s ingleton. sMANAGER_PASSWORD) ;

23. /*

24. *绑定ldap服务器

25. */

26. singleton.dirCtx = new InitialDirContext(singleton. envProps) ;

27. }

28. }

通过一个Hashtable或者Properties对象为LDAP的Context设置参数而后初始化InitialDirContext即可绑定LDAP服务。这相当于JDBC中获取数据库的Connection对象。

绑定/创建LDAP条目对象

用户可以使用b ind方法创建新的LDAP条目下面的代码创建一个DN"ou=Employee , dc=jsoso ,dc=net"的OrganizationUnit类LDAP条目如下:Java代码

1. public boolean createOrganizationUnit() {

2. String ldapGroupDN = "ou=Employee , dc=jsoso ,dc=net";

3. try {

4. /*

5. *查找是否已经存在指定的OU条目

6. *如果存在则打印OU条目的属性信息

7. *如果不存在则程序会抛出NamingException异常进入异常处理

8. */

9. Attributes attrs = dirContext.getAttributes(ldapGroupDN) ;

10. System.out.println("Find the group , attributes list :") ;

11. NamingEnumeration<String> nEnum = attrs.getIDs() ;

12. for( ; nEnum.hasMore() ; ) {

13. String attrID = nEnum.next() ;

14. Attribute attr = (Attribute)attrs.get(attrID) ;

15. System.out.println(attr. toString() ) ;

16. }

17. return false;

18. } catch (NamingException e) {

19. /*

20. *没有找到对应的Group条目新增Group条目

21. */

22. //创建objectclass属性

23. Attribute objclass = new BasicAttribute("objectclass") ;

24. objclass.add("top") ;

25. objclass.add("organizationalunit") ;

26. //创建cn属性

27. Attribute cn = new BasicAttribute("ou", "Employee") ;

28. //创建Attributes并添加objectclass和cn属性

29. Attributes attrs = new BasicAttributes() ;

30. attrs.put(objclass) ;

31. attrs.put(cn) ;

32. //将属性绑定到新的条目上创建该条目

33. try {

34. dirContext.bind(ldapGroupDN, null, attrs) ;

35. System.out.println("Group created successful") ;

36. return true;

37. } catch (NamingException e1) {

38. e1.printStackTrace() ;

39. }

40. }

41. return false;

42. }

或者使用createSubcontext方法创建亦可 以下例子我们新增一个inetorgperson类的LDAP条目

Java代码

1. /**

2. *创建LDAP用户条目

3. * @param user

4. * @return

5. */

6. public boolean createUser(LDAPUser user) {

7. if(user == null) {

8. return false;

9. }

10.

11. if(user.getUserID() == null | | user.getUserID() . length() ==

0

12. | | user.getFirstName() == null | | user.getFirstName() . length() == 0

13. | | user.getLastName() == null | | user.getLastName(). length() == 0

14. | | user.getCommomName() == nul l | | user.getCommomName() . length() == 0) {

15. return false;

16. }

17.

18. //判断用户条目是否已经存在

19. if(isUserexist(user.getDistinguishedName() ) ) {

20. return true;

21. }

22.

23. /*

24. *新建条目属性集

25. */

26. Attributes attrs = new BasicAttributes() ;

27. setBasicAttribute(attrs , "objectclass" , "top,person,organizationalPerson, inetorgperson") ;

28. setBasicAttribute(attrs , "cn" , user.getCommomName() ) ;

29. setBasicAttribute(attrs , "givenname" , user.getFirstName()) ;

30. setBasicAttribute(attrs , "sn" , user.getLastName() ) ;

31. setBasicAttribute(attrs , "uid" , user.getUserID() ) ;

32. setBasicAttribute(attrs , "userpassword" , user.getPassword() ) ;

33.

34. //添加用户条目节点

35. try {

36. dirContext.createSubcontext(user.getDistinguishedName(), attrs) ;

37. System.out.println("Add User(" + user.getDistinguishedName() + ") ok. ") ;

38. return true;

39. } catch (NamingException e) {

40. e.printStackTrace() ;

41. }

42. return false;

43. }

获取条目属性

下面一段代码获取entryDN参数指定条目中的属性集合并打印到控制台Java代码

1. /**

2. *获取一个指定的LDAP Entry

3. * @param entryDN

4. */

5. public void find(String entryDN) {

6. try {

7. Attributes attrs = dirContext.getAttributes(entryDN) ;

8. if (attrs != null) {

9. NamingEnumeration<String> nEnum = attrs.getIDs() ;

10. for( ; nEnum.hasMore() ; ) {

11. String attrID = nEnum.next() ;

12. Attribute attr = (Attribute)attrs.get(attrID) ;

JUSTG(5.99美元/月)最新5折优惠,KVM虚拟虚拟512Mkvm路线

Justg是一家俄罗斯VPS云服务器提供商,主要提供南非地区的VPS服务器产品,CN2高质量线路网络,100Mbps带宽,自带一个IPv4和8个IPv6,线路质量还不错,主要是用户较少,带宽使用率不高,比较空闲,不拥挤,比较适合面向非洲、欧美的用户业务需求,也适合追求速度快又需要冷门的朋友。justg的俄罗斯VPS云服务器位于莫斯科机房,到美国和中国速度都非常不错,到欧洲的平均延迟时间为40毫秒,...

spinservers($89/月),圣何塞10Gbps带宽服务器,达拉斯10Gbps服务器

spinservers是Majestic Hosting Solutions LLC旗下站点,主要提供国外服务器租用和Hybrid Dedicated等产品的商家,数据中心包括美国达拉斯和圣何塞机房,机器一般10Gbps端口带宽,高配置硬件,支持使用PayPal、信用卡、支付宝或者微信等付款方式。目前,商家针对部分服务器提供优惠码,优惠后达拉斯机房服务器最低每月89美元起,圣何塞机房服务器最低每月...

美国云服务器 1核 1G 100M 10G防御 39元/月 物语云计算

物语云计算(MonogatariCloud)是一家成立于2016年的老牌国人商家,主营国内游戏高防独服业务,拥有多家机房资源,产品质量过硬,颇有一定口碑。本次带来的是美国圣何塞 Equinix 机房的高性能I9-10980XE大带宽VPS,去程CN2GIA回程AS9929,美国原生IP,支持解锁奈飞等应用,支持免费安装Windows系统。值得注意的是,物语云采用的虚拟化技术为Hyper-V,资源全...

directory listing denied为你推荐
免费个人网站制作如何制作个人网站?要钱吗?软银孙正义孙正义和马云什么关系腾讯空间首页怎么才能让自己QQ空间被腾讯推荐在QQ空间首页里面?qq空间首页QQ空间的主页电脑杀毒软件哪个好电脑杀毒软件那个最好??浏览器哪个好用哪个浏览器比较好电视直播软件哪个好电视直播软件哪个好宝来和朗逸哪个好朗逸和宝来那个比较好些各方面宝来和朗逸哪个好朗逸和宝来买哪个好51空间登录51空间,怎么添加啊?怎么登陆?
便宜虚拟主机 二级域名查询 域名备案信息查询 国内免备案主机 轻博客 godaddy支付宝 华为云主机 长沙服务器 193邮箱 网站cdn加速 太原网通测速平台 100mbps 能外链的相册 申请网站 阿里云手机官网 国外网页代理 中国联通宽带测试 免费稳定空间 卡巴斯基官网下载 windowsserver2008r2 更多