12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package com.primeton.poctag.service.ldap.impl;
- import com.primeton.poctag.service.ldap.Person;
- import com.primeton.poctag.service.ldap.PersonRepo;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.ldap.core.LdapTemplate;
- import javax.naming.InvalidNameException;
- import java.util.List;
- import java.util.stream.Collectors;
- import static org.springframework.ldap.query.LdapQueryBuilder.query;
- /**
- * <pre>
- *
- * Created by zhenqin.
- * User: zhenqin
- * Date: 2021/9/14
- * Time: 16:41
- * Vendor: yiidata.com
- *
- * </pre>
- *
- * @author zhenqin
- */
- public class PersonRepoImpl implements PersonRepo {
- private LdapTemplate ldapTemplate;
- public void setLdapTemplate(LdapTemplate ldapTemplate) {
- this.ldapTemplate = ldapTemplate;
- }
- @Override
- public List<String> getAllPersonNames() {
- /*
- return ldapTemplate.search(
- query().where("uid").is("zhenqin"),
- new AttributesMapper<String>() {
- public String mapFromAttributes(Attributes attrs)
- throws NamingException {
- return attrs.get("uid").get().toString();
- }
- });
- */
- final List<Person> people = ldapTemplate.find(query().where("uid").isPresent(), Person.class);
- System.out.println(people);
- return people.stream().map(Person::getUid).collect(Collectors.toList());
- }
- public static void main(String[] args) throws InvalidNameException {
- ApplicationContext context = new ClassPathXmlApplicationContext("spring-ldap-test.xml");
- final PersonRepoImpl personRepo = context.getBean(PersonRepoImpl.class);
- System.out.println(personRepo.getAllPersonNames());
- /*
- Person vo = new Person();
- vo.setDn(new LdapName("uid=admin"));
- vo.setCn("admin");
- vo.setSn("admin");
- vo.setUid("admin");
- vo.setPassword("123456");
- personRepo.ldapTemplate.create(vo);
- */
- }
- }
|