【问题标题】:Spring Security LDAP "bind must be completed on the connection"Spring Security LDAP“必须在连接上完成绑定”
【发布时间】: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 

}

【问题讨论】:

    标签: php spring spring-security ldap spring-security-ldap


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-07-12
      • 2016-11-14
      • 2019-09-18
      • 2015-01-18
      • 2016-04-13
      • 2015-09-30
      • 2020-07-18
      • 2017-10-10
      • 2014-04-15
      相关资源
      最近更新 更多