@RunWith(SpringRunner.class)
@SpringBootTest
public class OpenLDAPJavaAPITest {
private static Hashtable ev = new Hashtable();
private static DirContext dirContext;
static {
ev.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ev.put(Context.PROVIDER_URL, "ldap://127.0.0.1:389/dc=shpun,dc=com");
ev.put(Context.SECURITY_AUTHENTICATION, "simple");
ev.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=shpun,dc=com");
ev.put(Context.SECURITY_CREDENTIALS, "secret");
try {
dirContext = new InitialDirContext(ev);
} catch (NamingException e) {
throw new RuntimeException(e);
}
}
/**
* Java LDAP 查询 person cn属性
*/
@Test
public void getAllPersonNamesJavaLDAp() {
List<String> list = new ArrayList<String>();
NamingEnumeration results = null;
try {
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
results = dirContext.search("", "(objectclass=inetOrgPerson)", controls);
while (results.hasMore()) {
SearchResult searchResult = (SearchResult) results.next();
Attributes attributes = searchResult.getAttributes();
Attribute attr = attributes.get("cn");
String cn = attr.get().toString();
list.add(cn);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (results != null) {
try {
results.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (dirContext != null) {
try {
dirContext.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* uid=zhangsan1001uid,ou=people,dc=shpun,dc=com 不行
* uid=zhangsan1001uid,ou=people 可以
*/
@Test
public void getByDn() throws Exception{
Object obj = dirContext.lookup("uid=zhangsan1001uid,ou=people");
}
/**
* dirContext.search 的 name参数置空可以
*/
@Test
public void searchByAttribute() {
NamingEnumeration results = null;
try {
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String returnedAtts[] = { "cn","sn","description","title","uid","displayName" };
controls.setReturningAttributes(returnedAtts);
results = dirContext.search("ou=people", "uid=zhangsan1001uid", controls);
while (results.hasMoreElements()) {
SearchResult searchResult = (SearchResult)results.next();
System.out.println(">>>" + searchResult.getName());
Attributes attrs = searchResult.getAttributes();
if (attrs != null) {
for (NamingEnumeration<? extends Attribute> names = attrs.getAll(); names.hasMore();) {
Attribute attr = names.next();
System.out.println("AttributeID: " + attr.getID());
for (NamingEnumeration<?> e = attr.getAll(); e.hasMore();) {
System.out.println("Attributes:" + e.next());
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (results != null) {
try {
results.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (dirContext != null) {
try {
dirContext.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 添加
*/
@Test
public void insert(){
Attributes attrs = new BasicAttributes();
attrs.put("uid", "zhangsan1006uid");
attrs.put("sn", "zhangsan1006sn");
attrs.put("cn", "zhangsan1006cn");
attrs.put("description", "zhangsan1006description");
attrs.put("title", "zhangsan1006title");
attrs.put("displayName", "zhangsan1006displayName");
Attribute objClass = new BasicAttribute("objectClass");
objClass.add("top");
objClass.add("person");
objClass.add("organizationalPerson");
objClass.add("inetOrgPerson");
attrs.put(objClass);
try {
dirContext.createSubcontext("uid=zhangsan1006uid,ou=people", attrs);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dirContext != null) {
try {
dirContext.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 更新单个属性
* 需要登录验证,在ev的Hashtable中添加OpenLDAP的账号密码
*/
@Test
public void updateAttribute() {
ModificationItem[] mods = new ModificationItem[1];
Attribute attr = new BasicAttribute("cn", "zhangsan1006cnUpdate");
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
try {
dirContext.modifyAttributes("uid=zhangsan1006uid,ou=people", mods);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dirContext != null) {
try {
dirContext.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 删除
*/
@Test
public void delete(){
try {
dirContext.destroySubcontext("uid=zhangsan1006uid,ou=people");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dirContext != null) {
try {
dirContext.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
Java API 操作 OpenLDAP
猜你喜欢
转载自blog.csdn.net/qq_36160730/article/details/97117246
今日推荐
周排行