Sun Directory Server/LDAP学习笔记(二)——API说明及代码样例
常用API解析?
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的常用部分,更多更详细的描述,请参考?Sun Java6.0 API DOC。?
LDAP操作代码样例?
在这个章节中,我们将结合LDAP操作的代码实例了解API使用。?
初始化LDAP 目录服务上下文?
该例子中,我们使用uid=linly,ou=People,dc=jsoso,dc=net这个账号,链接位于本机8389端口的LDAP服务器(ldap://localhost:8389),认证方式采用simple类型,即用户名/密码方式。?
?
LDAP filter语法补充说明?
filter的运算符??
filter布尔运算符??
搜索过滤器示例?- 下列过滤器将搜索包含一个或多个 manager 属性值的条目。这也称为存在搜索: manager=*
- 下列过滤器将搜索包含通用名 Ray Kultgen 的条目。这也称为等价搜索:cn=Ray Kultgen
- 下列过滤器返回所有不包含通用名 Ray Kultgen 的条目:(!(cn=Ray Kultgen))
- 下列过滤器返回的所有条目中都有包含子字符串 X.500 的说明属性:description=*X.500*
- 下列过滤器返回所有组织单元为 Marketing 且说明字段中不包含子字符串 X.500 的条目: (&(ou=Marketing)(!(description=*X.500*)))
- 下列过滤器返回所有组织单元为 Marketing 且 manager 为 Julie Fulmer 或 Cindy Zwaska 的条目: (&(ou=Marketing)(|(manager=cn=Julie Fulmer,ou=Marketing,dc=siroe,dc=com)(manager=cn=Cindy Zwaska,ou=Marketing,dc=siroe,dc=com)))
- 下列过滤器返回所有不代表人员的条目: (!(objectClass=person))
- 下列过滤器返回所有不代表人员且通用名近似于 printer3b 的条目:(&(!(objectClass=person))(cn~=printer3b))
?
- getAttributes()
- setReturningAttributes(String[] attrs)
- setTimeLimit(int ms) , 以毫秒为单位?
- setCountLimit(long limit)?
- setSearchScope(int scope)??
- remove(String attrID)
- getIDs()?
- getAll()
- get(String attrID)
- getID(),属性的ID就是属性名
- contains(Object attrVal) , 多值属性中有一个值是匹配的,返回true