Spring Security LDAP“必须在连接上完成绑定”

Posted

技术标签:

【中文标题】Spring Security LDAP“必须在连接上完成绑定”【英文标题】:Spring Security LDAP "bind must be completed on the connection" 【发布时间】:2021-11-10 12:17:43 【问题描述】:

我需要创建一个 SpringBoot 服务并使用 LDAP 提供一个身份验证服务。 我关注了这个example,它使用嵌入式本地 ldap 服务器对我来说很好(如教程中所建议的那样)

现在我尝试使用官方的公司 LDAP 服务器,但我收到了这个错误:

Uncategorized exception occured during LDAP processing; nested exception is javax.naming.NamingException: [LDAP: error code 1 - 000004DC: LdapErr: DSID-0C090A5C, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v4563];

此错误似乎与必需的 LDAP 绑定请求有关。但是如何将其添加到 Spring Security LDAP 中?

这是我尝试集成公司 LDAP 连接的代码:

@Override
  public void configure(AuthenticationManagerBuilder auth) throws Exception 
    auth
      .ldapAuthentication()
        .userDnPatterns("userPrincipalName=0,ou=users")
        .groupSearchBase("ou=users")
        .contextSource()
          .url("ldap://companyhost:389/dc=aa,dc=company,dc=com")
          .and()
        .passwordCompare()
          .passwordEncoder(new BCryptPasswordEncoder())
          .passwordAttribute("userPassword");
  

我也尝试了我的 php 示例(使用官方公司活动目录,这工作正常)

// connect to ldap server
    $ad = ldap_connect("ldap://".LDAP_HOST, LDAP_PORT)
        or die("Could not connect to LDAP server.");
    ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ad, LDAP_OPT_REFERRALS, 0);

    $user = $_POST['username'];
    $password = $_POST['password'];

    if (ldap_bind($ad, "$user"."@".LDAP_HOST, $password)) 

  // User authenticated 

【问题讨论】:

【参考方案1】:

您要连接的目录服务器不允许“匿名”访问,即您的应用程序首先必须在执行查询之前预先进行身份验证(通过和 LDAP BIND 操作)。 LdapContextSource

此外,使用密码比较 ('passwordCompare()') 不是 LDAP 最佳实践(我不知道为什么这仍在 Spring 文档中),而是应该使用 LDAP BIND 操作来验证用户身份。这让服务器可以比较密码,因为只有服务器知道保存密码时服务器端使用了哪种密码存储方案。

使用“userDnPattern”也不是 LDAP 最佳实践,因为这意味着您必须了解 LDAP 目录服务器的目录信息树 (DIT)。而是让目录服务器通过利用 'userSearchBase'、'userSearchFilter' LdapAuthenticationProviderConfigurer 执行搜索来确定可分辨名称 (DN)。

【讨论】:

以上是关于Spring Security LDAP“必须在连接上完成绑定”的主要内容,如果未能解决你的问题,请参考以下文章

Spring MVC + Spring Security + LDAP

如何使用 spring-security-core-ldap 插件在 grails 中实现 LDAP 身份验证?

Grails,Spring Security LDAP 插件

Spring Security和LDAP身份验证

如何将 Spring Security 从 ldap 更改为 ldap starttls

使用 Spring Security 的 ldap 身份验证