这里安装部署都是基于docker的,供参考
安装docker
1、yum list docker
2、yum install -y yum-utils device-mapper-persistent-data lvm2
3、yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
4、yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
5、systemctl start docker
开始安装LDAP
备注:这里openldap、phpldapadmin版本是20.10.3
1、docker pull osixia/openldap
2、docker pull osixia/phpldapadmin
验证:
[root@host1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
osixia/phpldapadmin latest dbb580facde3 21 months ago 309MB
osixia/openldap latest 31d1d6e16394 21 months ago 257MB
3、启动ldap
docker run -p 389:389 -p 636:636 --name ldap --env LDAP_ORGANISATION="testldap" --env LDAP_DOMAIN="testldap.com" --env LDAP_ADMIN_PASSWORD="hello" --detach osixia/openldap
4、看一下是否启动成功
[root@host1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d0ab3bfc144f osixia/openldap "/container/tool/run" 10 seconds ago Up 9 seconds 0.0.0.0:389->389/tcp, :::389->389/tcp, 0.0.0.0:636->636/tcp, :::636->636/tcp ldap
5、启动phpLdapAdmin 连到该LDAP,这里IP就是ldap服务所在的主机IP,这里其实就是本机localhost
docker run -d --privileged -p 9991:80 --name phpLdapAdmin --env PHPLDAPADMIN_HTTPS=false --env PHPLDAPADMIN_LDAP_HOSTS=192.168.1.9 --detach osixia/phpldapadmin
6、看一下是否启动成功
[root@host1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
89f123bb9db0 osixia/phpldapadmin "/container/tool/run" 7 seconds ago Up 6 seconds 443/tcp, 0.0.0.0:9991->80/tcp, :::9991->80/tcp phpLdapAdmin
d0ab3bfc144f osixia/openldap "/container/tool/run" 3 minutes ago Up 3 minutes 0.0.0.0:389->389/tcp, :::389->389/tcp, 0.0.0.0:636->636/tcp, :::636->636/tcp ldap
开始使用
1、登录网页
http://192.168.1.9:9991/
IP即phpLdapAdmin服务所在主机IP,port即我们启动该服务时的映射端口
2、登录
这里DN和密码是启动dlap 时env设置的,没有设置的话有个默认的。
3、登录后如图
4、点击Create a child entry创建组织提交
5、选择左侧创建的组织,再点击Create a child entry创建一个组
6、选择左侧ou=Baidu下cn=user,点击create a child entry,创建账号提交
7、提交后可以看到我们创建的账号
springboot 集成openLDAP
1、maven依赖
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-ldap -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
<version>3.0.0</version>
</dependency>
2、yml配置
spring:
ldap:
urls: 'ldap://192.168.1.9:389'
username: 'cn=admin,dc=testldap,dc=com'
password: 'helloa'
#注意这里配置base后,添加用户时,DN(Distinguished Name)不需要再追加,否则会报LDAP: error code 32 - No Such Object
base: 'dc=testldap,dc=com'
3、代码部分
LdapService
package com.h.service;
import com.h.dto.ldap.Person;
import java.util.List;
/**
* @author R
* @date 11:41 2022/12/2
* @description
*/
public interface LdapService {
Person getLdapUser(String uid);
boolean authenticate(String uid, String password);
Person addLdapUser(Person person);
void updateLdapUser(Person person);
void deleteLdapUser(Person person);
List<Person> getAll();
}
LdapServiceImpl
package com.h.service.impl;
import com.h.dto.ldap.Person;
import com.h.service.LdapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.stereotype.Service;
import java.util.List;
import static org.springframework.ldap.query.LdapQueryBuilder.query;
/**
* @author R
* @date 11:42 2022/12/2
* @description
*/
@Service
@Slf4j
public class LdapServiceImpl implements LdapService {
@Autowired
private LdapTemplate ldapTemplate;
@Override
public Person getLdapUser(String uid) {
LdapQuery query = query().where("uid").is(uid);
try {
return ldapTemplate.findOne(query, Person.class);
} catch (EmptyResultDataAccessException e) {
return null;
}
}
@Override
public boolean authenticate(String uid, String password) {
EqualsFilter filter = new EqualsFilter("uid", uid);
return ldapTemplate.authenticate("", filter.toString(), password);
}
@Override
public Person addLdapUser(Person person) {
ldapTemplate.create(person);
log.info("添加用户成功,name={}", person.getCn());
return person;
}
@Override
public void updateLdapUser(Person person) {
ldapTemplate.update(person);
log.info("更新用户成功,name={}", person.getCn());
}
@Override
public void deleteLdapUser(Person person) {
ldapTemplate.delete(person);
log.info("删除用户成功,name={}", person.getCn());
}
@Override
public List<Person> getAllUser() {
return ldapTemplate.findAll(Person.class);
}
}
LdapController
package com.h.controller;
import com.h.common.RestPage;
import com.h.common.RestResult;
import com.h.dto.ldap.Person;
import com.h.service.LdapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.web.bind.annotation.*;
import javax.naming.ldap.LdapName;
import java.util.List;
/**
* @author R
* @date 11:26 2022/12/2
* @description
*/
@Api(tags = "LDAP")
@RestController
@RequestMapping("/hi")
public class LdapController {
@Autowired
LdapService ldapService;
@ApiOperation(value = "LDAP查询列表", notes = "LDAP查询列表")
@GetMapping("/ldap")
public ResponseEntity<RestResult> getFunRequest() {
List<Person> personList = ldapService.getAll();
return ResponseEntity.ok().body(RestResult.success(RestPage.transformPage(personList)));
}
@ApiOperation(value = "LDAP添加用户", notes = "LDAP添加用户")
@PostMapping("/ldap")
public ResponseEntity<RestResult> addUser(@RequestBody Person person) {
person.setSn(person.getUid());
person.setCn(person.getUid());
person.setGidNumber(500);
person.setUidNumber(1002);// 需调整
LdapName ldapName = LdapUtils.newLdapName("cn= rrr,cn=user,ou=Baidu"); // 需调整
person.setDistinguishedName(ldapName);
ldapService.addLdapUser(person);
return ResponseEntity.ok().body(RestResult.success());
}
}