如何将 ActiveDirectory 与 Spring-Security LDAP 结合使用

How to use ActiveDirectory with Spring-Security LDAP

我正尝试在我的 Spring MVC 应用程序上为 LDAP 身份验证设置 Spring 安全性。我似乎无法获得 simple/principal 身份验证以使用 LdapAuthenticationProvider,因此我尝试使用默认情况下执行此操作的 ActiveDirectoryLdapAuthenticationProvider。

我在创建上下文后得到一个带有 detailMessage 的 NameNotFoundException(我认为 LDAP 绑定已经发生),来自这一行(ActiveDirectoryLdapAuthenticationProvider.java 中的 310):

return SpringSecurityLdapTemplate.searchForSingleEntryInternal(context,
                searchControls, searchRoot, searchFilter,
                new Object[] { bindPrincipal });

错误信息:

[LDAP: error code 32 - 0000208D: NameErr: DSID-03100213, problem 2001 (NO_OBJECT), data 0, best match of:
'DC=my,DC=company,DC=com']

搜索过滤器正在寻找 class "user" 的对象,其 userPrincipalName 等于我通过身份验证的用户名,并与我的域的域名连接。例如,"me@my.company.com"。具有该值的属性存在,因为我可以在此方法中使用 JXplorer 进行身份验证,然后执行该搜索以找到我的用户对象。

我的 WebSecurityConfigurerAdapter 子class 的配置,我在其中连接了一个 AuthenticationManagerBuilder,基本上是这样的:

@Autowired
public void init(AuthenticationManagerBuilder auth) throws Exception {
   ActiveDirectoryLdapAuthenticationProvider provider =
            new ActiveDirectoryLdapAuthenticationProvider("my.company.com", "LDAPS://ad.my.company.com:636/dc=my,dc=company,dc=com");
   provider.setConvertSubErrorCodesToExceptions(true);
   auth.authenticationProvider(provider);
}

是什么导致了 NameNotFoundException?这是配置 ActiveDirectory 身份验证的正确方法吗?

脸掌。 LDAP 服务器的 URL 不应包括 X.501 域组件部分,至少在我的目录中是这样。我想这是有道理的,因为第一个构造函数参数是域名(FQDN 样式)。所以构造函数参数应该是...

new ActiveDirectoryLdapAuthenticationProvider("my.company.com", "ldaps://ad.my.company.com:636");

错误消息提示了这一点,因为绑定已完成,但搜索失败。确切的错误以“NO_OBJECT”为原因,这是搜索基础关闭的线索。我最初配置的搜索实际上添加了两次搜索库 (DC)。