【发布时间】:2021-03-09 12:57:06
【问题描述】:
我正在尝试使用 Spring Boot 使用以下代码将用户添加到 Active Directory 组。
public boolean addMemberToGroup(String groupName, Person p) {
boolean status = false;
Name groupDn = buildGroupDn(groupName);
Name personDn = buildPersonDn(p);
try {
DirContextOperations ctx = ldapTemplate.lookupContext(groupDn);
ctx.addAttributeValue(UNIQUE_MEMBER, personDn);
ldapTemplate.modifyAttributes(ctx);
} catch (Exception e) {
logException
}
return status;
}
private Name buildGroupDn(String groupName) {
return LdapNameBuilder.newInstance(baseLdapPath)
.add("OU", "ghtc")
.add("OU", "IT")
.add("OU", "Groups")
.add("CN", groupName)
.build();
}
baseLdapPath = DC=emea,DC=testdir,DC=net 并添加到 application.properties 文件中。
但是当ldapTemplate.lookupContext(groupDn);执行时,出现如下异常:
嵌套异常是 javax.naming.NameNotFoundException:[LDAP:错误代码 32 - 0000208D:NameErr:DSID-03100241,问题 2001 (NO_OBJECT),数据 0,最佳匹配: 'DC=emea,DC=testdir,DC=net' 剩余名称 'CN=GroupAMC,OU=Groups,OU=IT,OU=ghtc,DC=emea,DC=testdir,DC=net'*
我已验证该组的专有名称是CN=GroupAMC,OU=Groups,OU=IT,OU=ghtc,DC=emea,DC=testdir,DC=net
我在这里错过了什么?
【问题讨论】:
-
我们看不到传入这个过程的代码,但是你确定组名是“对应组的DN,包括groupSearchBase”(docs.spring.io/spring-security/site/docs/4.2.5.RELEASE/apidocs/…)跨度>
-
我也添加了 buildGroupDn 代码。是否与将范围设置为 SUBTREE_SCOPE 有关。因为当我使用 SearchControls 搜索时,当我将范围设置为 SUBTREE_SCOPE 时,我能够找到该组
-
只是出于好奇,您确定 "baseLdapPath" 设置为 "DC=emea,DC=testdir,DC=net" 吗?你能用 ldapsearch 做同样的搜索吗?
-
是的,我已经验证过了。我也可以搜索组。和访问有关系吗?我的 LDAP 服务器的凭据还没有写入权限。我认为通过读取访问我应该能够执行 lookupContext 并且在修改属性时应该遇到异常
-
如果您可以通过 ldapsearch 使用匿名查找,那么这应该不是问题。如果你不能,那么是的,那可能是你的问题。我要求您检查是因为“最佳匹配”位,但我认为它做得正确
标签: java spring spring-boot ldap ldap-query