发布网友 发布时间:2022-04-22 03:08
共2个回答
热心网友 时间:2024-06-04 08:16
首先,我声明一下,我没用过这款软件,用过LDAP Browser和Apache 的LDAP Studio,既然是管理LDAP的,应该一样。
你安装好后,象建立数据源一样,把你要连接的ldap server的IP、端口、base DN、用户名、密码统统填好,然后就能连上了,一般是能看到整个ldap树的情况,接下去就直接在树的节点上操作就行了。
热心网友 时间:2024-06-04 08:17
1、LDAP的专用名词:
DN=Distinguished Name 一个目录条目的唯一标识名
CN=Common Name 为用户名或服务器名,最长可以到80个字符,可以为中文; OU=Organization Unit为组织单元,最多可以有四级,每级最长32个字符,可以为中文;
O=Organization 为组织名,可以3—个字符长
C=Country为国家名,可选,为2个字符长
L=Location 地名,通常是城市的名称
ST 州或省的名称
STREET 街道地址 UID 用户标识
2、LDAP的基本操作:
1) LDAP的连接
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class LDAP {
private DirContext ctx;
private Hashtable env;
// 服务器地址
private String LDAP_URL = "ldap://localhost:103";
// 管理域的dn
private String MANAGER_DN = "uid=admin,ou=system";
// 管理域的密码
private String MANAGER_PASSWORD = "secret";
// 管理域的验证方式
private String AUTH_TYPE = "type";
//
private String CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
public LDAP(){
connectLDAP();
}
public void connectLDAP(){
try{
env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY);
env.put(Context.PROVIDER_URL, LDAP_URL);
env.put(Context.SECURITY_AUTHENTICATION, AUTH_TYPE);
env.put(Context.SECURITY_PRINCIPAL, MANAGER_DN);
env.put(Context.SECURITY_CREDENTIALS, MANAGER_PASSWORD);
ctx = new InitialDirContext(env);
System.out.println("LDAP建立连接成功!");
}catch(NamingException e){
System.out.println(e);
}
}
}
1) LDAP连接的关闭
// 关闭到LDAP服务器的连接
private void closeConnection() {
try {
ctx.close();
} catch (NamingException ne) {
System.out.println(ne);
}
}
2) 创建一个分组OU
// 创建一个OU(当用户组不存在时创建)
public Boolean createOU(String ouName,String baseDN,ArrayList<String> names) throws NamingException {
try {
// load the attributes
BasicAttributes attrs = new BasicAttributes();
attrs.put(addObjclassSet(names));
attrs.put("ou", ouName);
ctx.createSubcontext("ou="+ouName+","+baseDN, attrs);
return new Boolean(true);
} catch (NamingException ne) {
System.out.println(ne);
return new Boolean(false);
}
}
//生成Objectclass属性
BasicAttribute addObjectclassSet (ArrayList<String> names){
BasicAttribute objclassSet = new BasicAttribute("objectclass");
objclassSet.add("top");
for(Iterator<String> iterator = names.iterator();iterator.hasNext();)
objclassSet.add(iterator.next());
// load the attributes
return objclassSet;
}
3) 添加一个UID
//添加一个uid entry
public boolean addUID(String uidName, String baseDN, ArrayList<String> names) throws NamingException{
try{
BasicAttributes attrs = new BasicAttributes();
attrs.put("uid",uidName);
attrs.put(addObjectclassSet(names));
ctx.createSubcontext("uid=" + uidName + "," + baseDN, attrs);
return true;
}catch(NamingException e){
e.printStackTrace();
}
return false;
}
4) 删除Entry
//传入entity的DN,删除这个entity
public boolean delEntity(String DN){
try {
ctx.destroySubcontext(DN);
System.out.println("del Entity success!");
return true;
} catch (NamingException e) {
System.out.println("del Entity error!!");
e.printStackTrace();
return false;
}
}
5) 修改entity的名字
//修改entity名字,传入entity的DN
public boolean reName(String oldDN,String newDN){
try {
ctx.rename(oldDN, newDN);
System.out.println("rename Entity success!");
return true;
} catch (NamingException e) {
System.out.println("rename Entity error!!");
e.printStackTrace();
return false;
}
}
6) 获取UID的DN
// 传入uid的名字,查找目录DN,得到 的这个entity的 DN
public String getUIDDN(String uidName,String baseDN) throws NamingException{
String DN = new String();
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
String searchFilter = "(&(uid=%1))";
searchFilter = searchFilter.replaceAll("%1", uidName);
// Search for objects that have those matching attributes
NamingEnumeration answer;
try {
answer = ctx.search(baseDN, searchFilter, constraints);
while (answer.hasMore()) {
SearchResult sr = (SearchResult) answer.next();
DN = sr.getNameInNamespace();
}
} catch (NamingException e) {
e.printStackTrace();
}
return DN;
}
7) 获取OU的DN
// 传入ou的名字,查找目录DN,得到 的这个entity的 DN
public String getOUDN(String ouName,String searchDN) throws NamingException{
String DN =new String();
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
String searchFilter = "(&(ou=%1))";
searchFilter = searchFilter.replaceAll("%1", ouName);
// Search for objects that have those matching attributes
NamingEnumeration answer;
try {
answer = ctx.search(searchDN, searchFilter, constraints);
while (answer.hasMore()) {
SearchResult sr = (SearchResult) answer.next();
DN = sr.getNameInNamespace();
}
} catch (NamingException e) {
e.printStackTrace();
}
return DN;
}