PersonRepoImpl.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package com.primeton.poctag.service.ldap.impl;
  2. import com.primeton.poctag.service.ldap.Person;
  3. import com.primeton.poctag.service.ldap.PersonRepo;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. import org.springframework.ldap.core.LdapTemplate;
  7. import javax.naming.InvalidNameException;
  8. import java.util.List;
  9. import java.util.stream.Collectors;
  10. import static org.springframework.ldap.query.LdapQueryBuilder.query;
  11. /**
  12. * <pre>
  13. *
  14. * Created by zhenqin.
  15. * User: zhenqin
  16. * Date: 2021/9/14
  17. * Time: 16:41
  18. * Vendor: yiidata.com
  19. *
  20. * </pre>
  21. *
  22. * @author zhenqin
  23. */
  24. public class PersonRepoImpl implements PersonRepo {
  25. private LdapTemplate ldapTemplate;
  26. public void setLdapTemplate(LdapTemplate ldapTemplate) {
  27. this.ldapTemplate = ldapTemplate;
  28. }
  29. @Override
  30. public List<String> getAllPersonNames() {
  31. /*
  32. return ldapTemplate.search(
  33. query().where("uid").is("zhenqin"),
  34. new AttributesMapper<String>() {
  35. public String mapFromAttributes(Attributes attrs)
  36. throws NamingException {
  37. return attrs.get("uid").get().toString();
  38. }
  39. });
  40. */
  41. final List<Person> people = ldapTemplate.find(query().where("uid").isPresent(), Person.class);
  42. System.out.println(people);
  43. return people.stream().map(Person::getUid).collect(Collectors.toList());
  44. }
  45. public static void main(String[] args) throws InvalidNameException {
  46. ApplicationContext context = new ClassPathXmlApplicationContext("spring-ldap-test.xml");
  47. final PersonRepoImpl personRepo = context.getBean(PersonRepoImpl.class);
  48. System.out.println(personRepo.getAllPersonNames());
  49. /*
  50. Person vo = new Person();
  51. vo.setDn(new LdapName("uid=admin"));
  52. vo.setCn("admin");
  53. vo.setSn("admin");
  54. vo.setUid("admin");
  55. vo.setPassword("123456");
  56. personRepo.ldapTemplate.create(vo);
  57. */
  58. }
  59. }