📄 firstdemo.java
字号:
package com.paul.ldap.demo;
import java.util.List;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
public class FirstDemo {
protected static final Log log = LogFactory.getLog(FirstDemo.class);
// public void LinkedList(Collection<? extends E> c)
// {
//
// }
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractApplicationContext lbf =
new ClassPathXmlApplicationContext("/applicationContext-ldap.xml");
lbf.registerShutdownHook();
GenericBeanFactoryAccessor gbfa = new GenericBeanFactoryAccessor(lbf);
LdapTemplate lt = (LdapTemplate)gbfa.getBean("ldapTemplate");
// inetOrgPerson也可以被person替代
List usersList = lt.search(
"", "(|(member=cn=zgzhang*)(cn=zgzhang))",
new AttributesMapper() {
public Object mapFromAttributes(Attributes attrs)
throws NamingException {
NamingEnumeration names = attrs.getAll();
while(names.hasMoreElements())
{
log.info(names.next());
}
return "";
//return attrs.get("notesdn").get();
}
});
System.out.print(usersList);
// 打印出用户集合
log.info(usersList);
// inetOrgPerson也可以被person替代
List passwordsList = lt.search(
"", "(objectclass=inetOrgPerson,cn=zgzhang)",
new AttributesMapper() {
public Object mapFromAttributes(Attributes attrs)
throws NamingException {
return attrs.get("userpassword").get();
}
});
// 打印出用户密码集合
//log.info(passwordsList);
List rolesList = lt.search(
"", "(objectclass=groupOfNames)",
new AttributesMapper() {
public Object mapFromAttributes(Attributes attrs)
throws NamingException {
return attrs.get("cn").get();
}
});
// 打印出角色集合
//log.info(rolesList);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -