1、ldap数据结构图
2、springboot项目application-dev.properties配置
#AD域
spring.ldap.urls=ldap://192.168.2.133
spring.ldap.base=DC=dms,DC=yudean,DC=com
spring.ldap.username=bigdata
spring.ldap.password=hand
3、springboot启动类加@EnableLdapRepositories
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.ldap.repository.config.EnableLdapRepositories;
@SpringBootApplication
@EnableLdapRepositories
public class DatahubApplication {
public static void main(String[] args) {
SpringApplication.run(DatahubApplication.class, args);
}
}
4、项目entity与ldap映射实体
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import javax.naming.Name;
@Entry(base = "OU=广东xxxx有限公司", objectClasses = {"organizationalPerson", "person", "top", "user"})
public class LdapUser {
@Id
@JsonIgnore
private Name dn;
@Attribute(name = "sAMAccountName")
private String code;
@Attribute(name = "cn")
private String name;
@Attribute(name = "department")
private String deptName;
@Attribute(name = "company")
private String compName;
@Attribute(name = "email")
private String email;
@Attribute(name = "mobile")
private String mobile;
public Name getDn() {
return dn;
}
public void setDn(Name dn) {
this.dn = dn;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getCompName() {
return compName;
}
public void setCompName(String compName) {
this.compName = compName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
5、Jpa的LdapUserRepository
import com.magus.datahub.master.orghandle.entity.LdapUser;
import org.springframework.data.repository.CrudRepository;
import javax.naming.Name;
import java.util.List;
/**
* LdapUserRepository继承CrudRepository接口实现基于Ldap的增删改查操作
*/
public interface LdapUserRepository extends CrudRepository {
//cn是员工姓名,不是员工编码,所以用姓名搜索
List findAllByName(String name);
}
6、LdapService
import com.google.common.collect.Lists;
import com.magus.datahub.master.orghandle.entity.LdapUser;
import com.magus.datahub.master.orghandle.repository.LdapUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Description
* @Author lxk
* @Date 2019/12/26 0026 10:38
*/
@Service
public class LdapService {
@Autowired
private LdapUserRepository ldapUserRepository;
public List findByName(String name) {
return ldapUserRepository.findAllByName(name);
}
//测试
public List findAll() {
Iterable a = ldapUserRepository.findAll();
List myList = Lists.newArrayList(a);
return myList;
}
}
7、LdapController
import com.magus.datahub.master.orghandle.entity.LdapUser;
import com.magus.datahub.master.orghandle.service.LdapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Description
* @Author lxk
* @Date 2019/12/23 0023 17:10
*/
@RestController
@RequestMapping("/ldap")
public class LdapController {
@Autowired
private LdapService ldapService;
@GetMapping("/test/{name}")
public List ldap(@PathVariable String name) {
System.out.println("name#####" + name);
return ldapService.findByName(name);
}
@GetMapping("/test")
public List ldapAll() {
return ldapService.findAll();
}
特别注意
核心一:
spring.ldap.base=DC=dms,DC=yudean,DC=com
核心二:
@Entry(base = "OU=广东xxxx有限公司", objectClasses = {"organizationalPerson", "person", "top", "user"})
核心三:
@Id
@JsonIgnore
private Name dn;
核心四:
@Attribute(name = "cn")
参考:https://www.jb51.net/article/131694.htm