用户可以使用bind方法创建新的LDAP条目,下面的代码创建一个DN:"ou=Employee , dc=jsoso ,dc=net"的OrganizationUnit类LDAP条目如下:
public boolean createOrganizationUnit(){
String ldapGroupDN = "ou=Employee , dc=jsoso ,dc=net";
try {
/*
*查找是否已经存在指定的OU条目*如果存在,则打印OU条目的属性信息*如果不存在,则程序会抛出NamingException异常,进入异常处理*/
Attributes attrs = dirContext.getAttributes(ldapGroupDN);
System.out.println("Find the group , attributes list :");
NamingEnumeration nEnum = attrs.getIDs();
for( ; nEnum.hasMore() ; ){
String attrID = nEnum.next();
Attribute attr = (Attribute)attrs.get(attrID);
System.out.println(attr.toString());
}
return false;
} catch (NamingException e) {
/*
*没有找到对应的Group条目,新增Group条目*/
//创建objectclass属性Attribute objclass = new BasicAttribute("objectclass");
objclass.add("top");
objclass.add("organizationalunit");
//创建cn属性Attribute cn = new BasicAttribute("ou", "Employee");
//创建Attributes,并添加objectclass和cn属性Attributes attrs = new BasicAttributes();
attrs.put(objclass);
attrs.put(cn);
//将属性绑定到新的条目上,创建该条目try {
dirContext.bind(ldapGroupDN, null, attrs);
System.out.println("Group created successful");
return true;
} catch (NamingException e1) {
e1.printStackTrace();
}
}
return false;
}获取条目属性下面一段代码获取entryDN参数指定条目中的属性集合,并打印到控制台/**
*获取一个指定的LDAP Entry
* @param entryDN
*/
public void find(String entryDN){
try {
Attributes attrs = dirContext.getAttributes(entryDN);
if (attrs != null) {
NamingEnumeration nEnum = attrs.getIDs();
for( ; nEnum.hasMore() ; ){
String attrID = nEnum.next();
Attribute attr = (Attribute)attrs.get(attrID);
System.out.println(attr.toString());
}
System.out.println();
}else{
System.out.println("No found binding.");
}
}catch(NamingException ne) {
ne.printStackTrace();
}
}修改条目属性修改DN=user.getDistinguishedName()的条目中的cn、givenname、sn和userpassword四个属性值。(注:参数DirContext.REPLACE_ATTRIBUTE有另外两个常量:DirContext.ADD_ATTRIBUTE;DirContext.REMOVE_ATTRIBUTE,分别表示新增属性和删除属性。)/**
*修改用户信息* @param user
* @return
* @throws Exception
*/
public boolean modifyUser(LDAPUser user) throws Exception {
//用户对象为空if (user == null) {
throw new Exception("No user information!n");
}
//检查uid
String userDN = user.getDistinguishedName();
if (userDN == null && userDN.length() == 0) {
throw new NamingException("No userDN you specify!n");
}
//判断用户条目是否已经存在if(!isUserexist(userDN)){
return false;
}
//设置属性Attributes attrs = new BasicAttributes();
setBasicAttribute(attrs, "cn", user.getCommomName());
setBasicAttribute(attrs, "givenname", user.getFirstName());
setBasicAttribute(attrs, "sn", user.getLastName());
setBasicAttribute(attrs, "userpassword", user.getPassword());
//修改属性try{
dirContext.modifyAttributes(user.getDistinguishedName(),DirContext.REPLACE_ATTRIBUTE, attrs);
System.out.println("User(" + user.getDistinguishedName() + ") information modified.n");
return true;
}catch(NamingException ne){
ne.printStackTrace();
}
return false;
}根据属性集搜索条目根据属性集matchingAttributes中的匹配值,在上下文DN= "ou=People,dc=jsoso ,dc=net"中搜索它的所有子树中的匹配条目。(注:SearchControls的SCOPE参数详见SearchControls SCOPE补充说明)/**
*通过属性搜索LDAP范例* @return
*/
public void searchByAttribute(Attributes matchingAttributes){
String baseDN = "ou=People,dc=jsoso ,dc=net";
SearchControls cons = new SearchControls();
cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
try {
Name baseName = new LdapName(baseDN);
NamingEnumeration ne = dirContext.search(baseName, matchingAttributes);
SearchResult entry = null;
for(;ne.hasMore();){
entry = ne.next();
showEntry(entry);
}
} catch (NamingException e) {
e.printStackTrace();
}
}根据过滤器搜索条目根据过滤器条件,在上下文DN = "ou=People,dc=jsoso ,dc=net"中,搜索它的所有子树中的匹配条目。(注:过滤器filter的相关语法详见LDAP filter语法补充说明)/**
*通过过滤器搜索LDAP范例* @return
*/
public void searchByFilter(String filter){
String baseDN = "ou=People,dc=jsoso ,dc=net";
SearchControls cons = new SearchControls();
cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
try {
NamingEnumeration ne = dirContext.search(baseDN, filter , cons);
SearchResult entry = null;
for(;ne.hasMore();){
entry = ne.next();
showEntry(entry);
}
} catch (NamingException e) {
e.printStackTrace();
}