ad域控查看ldap端口命令_通过LDAP在AD域控上进行添加、删除、修改、查询等各种操作。...

用户可以使用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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值