ldap是可以通过java控制增删查改么?

ldap是可以通过java控制增删查改么?,第1张

可以的。
static DirContext dc = null;
static String username = "Manager";
static String password = "admin";
Map m = new HashMap();
public TestLdap() {
getDC();
// add();
}
// 获取连接
public static InitialDirContext getDC() {
Hashtable env = new Hashtable();
DirContext d = null;
// 连接工厂
envput(ContextINITIAL_CONTEXT_FACTORY,
"comsunjndildapLdapCtxFactory");
envput(ContextPROVIDER_URL, "ldap://自己的IP:389/");
// 设定安全模式为simple
envput(ContextSECURITY_AUTHENTICATION, "simple");
// 用户名
envput(ContextSECURITY_PRINCIPAL, "cn=" + username + "," + "dc=root");
// 密码
envput(ContextSECURITY_CREDENTIALS, password);
try {
d = new InitialDirContext(env);
Systemoutprint("连接成功!");
} catch (NamingException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
return (InitialDirContext) d;
}
// 关闭
public void close() {
if (dc != null) {
try {
dcclose();
} catch (NamingException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
// 添加
public static boolean add(DirContext dc) {
boolean result = true;
String name = "zhangsan";
BasicAttributes attrs = new BasicAttributes();
BasicAttribute attr = new BasicAttribute("objectclass");
attradd("organization");
attrsput(attr);
attrsput("o", name);
try {
dccreateSubcontext("o=" + name + ",o=zhangsan,o=person,dc=root",
attrs);
} catch (NamingException e) {
eprintStackTrace();
result = false;
}
return result;
}
// 删除
public static boolean delete(DirContext dc, String d) {
boolean result = true;
try {
dcdestroySubcontext(d);
Systemoutprintln("删除成功!");
} catch (NamingException e) {
// TODO Auto-generated catch block
eprintStackTrace();
result = false;
}
return result;
}
/
修改 @param base的取值来判断需要执行哪个方法 @param atr属性名称 @param value属性值


/
public static boolean modifytest(DirContext dc, String dn, String base,
String atr, String value) {
boolean result = true;
ModificationItem[] item = new ModificationItem[1];
try {
if ("add"equals(base)) {
// 没有这个属性,添加这个属性
Attribute atr1 = new BasicAttribute(atr, value);
item[0] = new ModificationItem(DirContextADD_ATTRIBUTE, atr1);
} else if ("modify"equals(base)) {
// 修改原有属性(如果有此属性,修改属性的值,如没有添加此属性)
Attribute atr1 = new BasicAttribute(atr, value);
item[0] = new ModificationItem(DirContextREPLACE_ATTRIBUTE,
atr1);
} else {
// 移除此属性(属性值需和ldap中的一样)
Attribute atr2 = new BasicAttribute(atr, value);
item[0] = new ModificationItem(DirContextREMOVE_ATTRIBUTE,
atr2);
}
dcmodifyAttributes(dn, item);
} catch (NamingException e) {
// TODO Auto-generated catch block
eprintStackTrace();
result = false;
}
return result;
}
/
搜索,查询

@param base
:根节点(在这里是“dc=root”)
@param scope
:搜索范围,分为“base”(本节点),“one”(单层),“”{遍历}
@param filter
:指定子节点(格式为“(objectclass=)”)“,是指全部,你也可以指定某一特定类型的树节点
/
public static List searchtest(DirContext dc, String base, String scope,
String filter) {
List list = new ArrayList();
SearchControls sc = new SearchControls();
if (scopeequals(base)) {
scsetSearchScope(SearchControlsOBJECT_SCOPE);
} else if (scopeequals("one")) {
scsetSearchScope(SearchControlsONELEVEL_SCOPE);
} else {
scsetSearchScope(SearchControlsSUBTREE_SCOPE);
}
NamingEnumeration na = null;
try {
na = dcsearch(base, filter, sc);
// hasMore 确定枚举中是否有该元素
while (nahasMore()) {
SearchResult sr = (SearchResult) nanext();
// String name=srgetName();
// if(base!=null&&!baseequals(""))
// {
// Systemoutprintln("entry:"+name+""+base);
// }else
// {
// Systemoutprintln("entry:"+name);
// }
Attributes as = srgetAttributes();
NamingEnumeration na1 = asgetAll();
while (na1hasMore()) {
Attribute at = (Attribute) na1next();
String attype = atgetID();
NamingEnumeration na2 = atgetAll();
Vector v = new Vector();
while (na2hasMore()) {
Object val = na2nextElement();
listadd(val);
}
}
}
} catch (NamingException e) {
eprintStackTrace();
}
return list;
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zz/13432986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-08-05
下一篇 2023-08-05

发表评论

登录后才能评论

评论列表(0条)

保存