<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-ldapartifactId>
dependency>
spring:
ldap:
urls: ldap://192.168.16.34:389
base: dc=testldap,dc=com
username: dn=admin,dc=testldap,dc=com
password: 123456
Person中字段为需要从Ldap中查询的数据字段,利用注解@Attribute(name=“xx”)
进行注解,Entry中定义的objectClass和base为Ldap中数据资源的定位信息。查询的时候可以作为返回对象来接收数据。
@Data
@ToString
@Entry(objectClasses = {"bicPersonExt", "bicPerson"}, base = "ou=person,dc=coreservice")
public class Person {
/**
* 主键
*/
@Attribute
private String personId;
/**
* 人员姓名
*/
@Attribute(name = "cn")
private String personName;
/**
* 组织ID
*/
@Attribute(name = "orgId")
private String orgId;
/**
* 性别
*/
@Attribute(name = "sex")
private Integer sex;
/**
* 电话
*/
@Attribute(name = "mobile")
private String mobile;
/**
* 邮箱
*/
@Attribute(name = "email")
private String email;
/**
* 工号
*/
@Attribute(name = "jobNo")
private String jobNo;
/**
* 学号
*/
@Attribute(name = "studentId")
private String studentId;
/**
* 证件类型
*/
@Attribute(name = "certType")
private Integer certType;
/**
* 证件号码
*/
@Attribute(name = "certificateNo")
private String certNo;
@Attribute
protected Date createTime;
/**
* 更新时间
*/
@Attribute
protected Date updateTime;
/**
* 状态
*/
@Attribute
protected Integer status;
@Attribute
protected Integer disOrder;
/**
* 工作单位
*/
@Attribute
private String company;
}
public interface IPersonRepo {
void setLdapTemplate(LdapTemplate ldapTemplate);
List<String> getAllPersonNames();
List<String> getAllPersonNamesWithTraditionalWay();
List<Person> getAllPersons();
Person findPersonWithDn(String dn);
List<String> getPersonNamesByOrgId(String orgId);
}
public class PersonRepoImpl implements IPersonRepo {
private LdapTemplate ldapTemplate;
@Override
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
/**
* 查询部分字段集合
* @return
*/
@Override
public List<String> getAllPersonNames() {
return ldapTemplate.search(
query().where("objectclass").is("person"), (AttributesMapper<String>) attrs -> (String) attrs.get("cn").get());
}
/**
* 传统LDAP查询方式
* @return
*/
@Override
public List<String> getAllPersonNamesWithTraditionalWay() {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://10.33.47.7:7003/dc=platform,dc=xxx,dc=com");
env.put(Context.SECURITY_PRINCIPAL, "ou=acs,ou=componentaccounts,dc=platform,dc=xxx,dc=com");
env.put(Context.SECURITY_CREDENTIALS, "UlAwRkYl");
DirContext ctx;
try {
ctx = new InitialDirContext(env);
} catch (NamingException e) {
throw new RuntimeException(e);
}
List<String> list = new LinkedList<String>();
NamingEnumeration results = null;
try {
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
results = ctx.search("", "(objectclass=person)", 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 (NameNotFoundException e) {
// The base context was not found.
// Just clean up and exit.
} catch (NamingException e) {
//throw new RuntimeException(e);
} finally {
if (results != null) {
try {
results.close();
} catch (Exception e) {
// Never mind this.
}
}
if (ctx != null) {
try {
ctx.close();
} catch (Exception e) {
// Never mind this.
}
}
}
return list;
}
/**
* 查询对象映射集合
* @return
*/
@Override
public List<Person> getAllPersons() {
return ldapTemplate.search(query()
.where("objectclass").is("person"), new PersonAttributesMapper());
}
/**
* 根据DN查询指定人员信息
* @param dn
* @return
*/
@Override
public Person findPersonWithDn(String dn) {
return ldapTemplate.lookup(dn, new PersonAttributesMapper());
}
/**
* 组装查询语句
* @param orgId
* @return
*/
@Override
public List<String> getPersonNamesByOrgId(String orgId) {
LdapQuery query = query()
.base("ou=person,dc=coreservice")
.attributes("cn", "sn")
.where("objectclass").is("person")
.and("orgId").is(orgId);
return ldapTemplate.search(query,(AttributesMapper<String>) attrs -> (String) attrs.get("cn").get());
}
}
public class PersonAttributesMapper implements AttributesMapper<Person> {
/**
* Map Attributes to an object. The supplied attributes are the attributes
* from a single SearchResult.
*
* @param attrs attributes from a SearchResult.
* @return an object built from the attributes.
* @throws NamingException if any error occurs mapping the attributes
*/
@Override
public Person mapFromAttributes(Attributes attrs) throws NamingException {
Person person = new Person();
person.setPersonName((String)attrs.get("cn").get());
person.setOrgId((String)attrs.get("orgId").get());
return person;
}
}
测试用例
@RunWith(SpringRunner.class)
@SpringBootTest(classes={Chapter3ApplicationTest.class})
public class LdapTest {
@Autowired
private LdapTemplate ldapTemplate;
private PersonRepoImpl personRepo;
@Before
public void init(){
personRepo = new PersonRepoImpl();
personRepo.setLdapTemplate(ldapTemplate);
}
@Test
public void ldapRestTestPart1(){
// 查询所有人员名称
//personRepo.getAllPersonNames().forEach(p-> System.out.println(p));
//荣禧
//荣耀
//feng_p1
//fengzi_0917_1
//....
// 查询所有人员集合(指定字段映射)
//personRepo.getAllPersons().forEach(p-> System.out.println(p.toString()));
//Person(personId=null, personName=fengzi_0917_7, orgId=14ed2744-fbd4-4868-8ebc-6b0b94d5ae60, sex=null, mobile=null, email=null, jobNo=null, studentId=null, certType=null, certNo=null, createTime=null, updateTime=null, status=null, disOrder=null, company=null)
//Person(personId=null, personName=fengzi_0917_104, orgId=14ed2744-fbd4-4868-8ebc-6b0b94d5ae60, sex=null, mobile=null, email=null, jobNo=null, studentId=null, certType=null, certNo=null, createTime=null, updateTime=null, status=null, disOrder=null, company=null)
//根据dn查询
System.out.println(personRepo.findPersonWithDn("ou=person,dc=coreservice,dc=platform,dc=xxx,dc=com").toString());
//根据组织ID查询人员
//personRepo.getPersonNamesByOrgId("14ed2744-fbd4-4868-8ebc-6b0b94d5ae60").forEach(System.out::println);
//feng_0925_4687
//feng_0925_4693
//...
//传统查询方式
//personRepo.getAllPersonNamesWithTraditionalWay().forEach(System.out::println);
//荣禧
//荣福
//feng_p1
//fengzi_0917_1
//....
}
}