SpringBoot集成LDAP同步数据
1、pom引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-ldap</artifactId> </dependency>
2、创建一个资源类LdapRepository
首先需要创建一个实体
@Data public class LdapServer { /** * ldap服务器 */ @NotBlank private String url; /** * 端口 */ @NotBlank private Integer port; /** * 基础域 */ @NotBlank private String baseDN; /** * 用户名 */ @NotBlank private String userName; /** * 密码 */ @NotBlank private String password; }
建立连接:
private LdapTemplate ldapTemplate;
/** * 使用前必须先连接 * * @param server */ public LdapRepository connect(LdapServer server) { if(server.getUrl().contains("ldaps")){ SSLLdapContextSource contextSource = new SSLLdapContextSource(); contextSource.setUrl(server.getUrl() + ":" + server.getPort()); contextSource.setUserDn(server.getUserName()); contextSource.setPassword(server.getPassword()); contextSource.setPooled(false); contextSource.setBase(server.getBaseDN()); contextSource.afterPropertiesSet(); contextSource.setReferral("follow"); // 设置连接超时时间 3s Map<String, Object> envProperties = new HashMap<>(); envProperties.put("com.sun.jndi.ldap.connect.timeout", "3000"); envProperties.put("com.sun.jndi.ldap.read.timeout", "3000"); contextSource.setBaseEnvironmentProperties(envProperties); ldapTemplate = new LdapTemplate(contextSource); }else { LdapContextSource contextSource = new LdapContextSource(); contextSource.setUrl(server.getUrl() + ":" + server.getPort()); contextSource.setUserDn(server.getUserName()); contextSource.setPassword(server.getPassword()); contextSource.setPooled(false); contextSource.setBase(server.getBaseDN()); contextSource.afterPropertiesSet(); // important contextSource.setReferral("follow"); // 设置连接超时时间 3s Map<String,Object> envProperties = new HashMap<>(); envProperties.put("com.sun.jndi.ldap.connect.timeout","3000"); envProperties.put("com.sun.jndi.ldap.read.timeout","3000"); contextSource.setBaseEnvironmentProperties(envProperties); ldapTemplate = new LdapTemplate(contextSource); } ldapTemplate.setIgnorePartialResultException(true); return this; }
测试认证连接:
public void authenticate(String username, String password) { ldapTemplate.getContextSource().getContext(username, password); }
一次查询所有人员:
/** * 查询所有人员 */ public List findAll(LdapQuery ldapQuery) { List<BasicAttributes> basicAttributesList = (List) ldapTemplate.search(ldapQuery, new AttributesMapper<Object>() { @Override public Object mapFromAttributes(Attributes attributes) throws NamingException { BasicAttributes basicAttributes = (BasicAttributes) attributes; return basicAttributes; } }); return basicAttributesList; }
如果数据量太大,需要使用分页查询:
public List<BasicAttributes> findAllPageNew(LdapQuery ldapQuery) { String searchFilter = "(&(objectClass=person)(!(objectclass=computer)))"; List<BasicAttributes> attributesList = new ArrayList<>(); ldapTemplate.setIgnorePartialResultException(true); SearchControls searchControls = new SearchControls(); /** * 0:OBJECT_SCOPE,搜索指定的命名对象。 * 1:ONELEVEL_SCOPE,只搜索指定命名对象的一个级别,这是缺省值。 * 2:SUBTREE_SCOPE,搜索以指定命名对象为根结点的整棵树 */ searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); // 每次查询条数:默认1000条 PagedResultsDirContextProcessor processor = new PagedResultsDirContextProcessor(1000); //返回的参数 AttributesMapper CN_ATTRIBUTES_MAPPER = attributes -> { BasicAttributes basicAttributes = (BasicAttributes) attributes; return basicAttributes; }; do { List<BasicAttributes> searchList = (List) ldapTemplate.search("", searchFilter, searchControls, CN_ATTRIBUTES_MAPPER, processor); attributesList.addAll(searchList); } while(processor.hasMore()); return attributesList; }
分类:
SpringBoot开发
, LDAP
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)