LDAP使用案例

LDAP使用案例

增删改查案例

在pom中引入如下依赖:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>

添加LDAP连接配置:

spring:
  ldap:
    urls: ldap://10.202.63.20:389
    base: dc=example,dc=org
    username: cn=admin,dc=example,dc=org
    password: admin

如图,测试用的ldap结构为

写一个与LDAP的entry互相映射的实体类

package com.szkingdom.ldap.entry;

import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;

@Entry(objectClasses = {"organizationalPerson","person","top"},base = "o=myorg")
public class Person {

    @Attribute(name="cn")
    private String cn;

    @Attribute(name="sn")
    private String sn;

    @Attribute(name="userPassword")
    private String userPassword;

    public String getCn() {
        return cn;
    }

    public String getSn() {
        return sn;
    }

    public String getUserPassword() {
        return userPassword;
    }


    public void setCn(String cn) {
        this.cn = cn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    @Override
    public String toString() {
        return "Person{" +
                "cn='" + cn + '\'' +
                ", sn='" + sn + '\'' +
                ", userPassword='" + userPassword + '\'' +
                '}';
    }
}

其中@Entry注解的objectClasses和base与ldap结构对应

使用ldapTemplate书写dao层的方法

此方法相当于dao层的实现

@Service
public class OdmPersonRepo {

    @Autowired
    private LdapTemplate ldapTemplate;

    public Person create(Person person){
        ldapTemplate.create(person);
        return person;
    }

    public Person findByCn(String cn){
        return ldapTemplate.findOne(query().where("cn").is(cn),Person.class);
    }
    
    public List<Person> list(){
        return ldapTemplate.find(query().where("objectclass").is("person"), Person.class);
    }

    public Person modifyPerson(Person person){
        ldapTemplate.update(person);
        return person;
    }

    public void deletePerson(Person person){
        ldapTemplate.delete(person);
    }

}

写一个简单的controller用于测试

@RestController
public class TestLdapController {
    @Autowired
    private OdmPersonRepo odmPersonRepo;
    
    @PostMapping(value = "/findOne")
    public Result<Person> findByCn(@RequestBody String cn){
        Person p = odmPersonRepo.findByCn(cn);
        return new Result<Person>(p);
    }
    
    @PostMapping(value = "/list")
    public ListResult<Person> list(){
        List<Person> l = odmPersonRepo.list();
        return new ListResult<Person>(l);
    }

    @PostMapping(value = "/create")
    public Result<Person> create(@RequestBody Person person){
        Person p =  odmPersonRepo.create(person);
        return new Result<Person>(p);
    }



    @PostMapping(value = "/update")
    public Result<Person> update(@RequestBody Person person){
        Person p =  odmPersonRepo.create(person);
        return new Result<Person>(p);
    }

    @PostMapping(value = "/delete")
    public Result<String> delete(@RequestBody String cn){
        Person person = new Person();
        person.setCn(cn);
        odmPersonRepo.deletePerson(person);
        return new Result<String>(cn);
    }

}

我们用postman调用新增人员接口进行测试:

可以看到LDAP服务器中数据增加了新的:

用postman调用查询接口:

http://localhost:8081/admin/list

返回如下:

  "code": "0",
  "msg": "请求处理成功",
  "detail": "",
  "head": {},
  "body": [
    {
      "cn": "testcn",
      "sn": "testsn",
      "userPassword": "49,50,51,52,53,54"
    },
    {
      "cn": "sy",
      "sn": "sy_sn",
      "userPassword": "49,50,51,52,53,54,55,56"
    }
  ]
}

调用删除接口:

可以看到LDAP服务器中数据被删除:

总结

KOCA为springboot项目,在KOCA中使用LDAP与其他spring项目使用LDAP本质上没有区别,使用LdapTemplate可以简单的对LDAP服务器完成增删改查。

3 个赞

点赞