springboot+ldap+Jpa

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<LdapUser, Name> {

    //cn是员工姓名,不是员工编码,所以用姓名搜索
    List<LdapUser> 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<LdapUser> findByName(String name) {
        return ldapUserRepository.findAllByName(name);
    }

    //测试
    public List<LdapUser> findAll() {
        Iterable<LdapUser> a = ldapUserRepository.findAll();
        List<LdapUser> 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<LdapUser> ldap(@PathVariable String name) {
        System.out.println("name#####" + name);
        return ldapService.findByName(name);
    }

    @GetMapping("/test")
    public List<LdapUser> 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

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值