首页 > 解决方案 > 使用 spring 绑定到 LDAP

问题描述

如果有错误,我为英语道歉。我需要通过 LDAP 向我的 spring 程序添加授权,但是在尝试授权客户端时出现错误:

Uncategorized exception occured during LDAP processing; nested exception is javax.naming.NamingException: [LDAP: error code 1 - 000004DC: LdapErr: DSID-0C0907E9, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v2580]; remaining name 'uid=ben,ou=Пользователи'

据我了解,这是服务器上的程序授权错误。有权使用 LDAP 协议在 AD 中搜索的帐户的凭据是通过 application.properties 设置的:

spring.ldap.username=
spring.ldap.password=

负责授权的代码片段与指南中的代码相同:

  @Override
  public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
      .ldapAuthentication()
        .userDnPatterns("(uid={0}),ou=Пользователи")
        .groupSearchBase("ou=groups")
        .contextSource()
          .url("xxx")
          .and()
        .passwordCompare()
          .passwordEncoder(new BCryptPasswordEncoder())
          .passwordAttribute("userPassword");
  }

对Spring不是很了解,如有错误请指教。此错误是否与密码编码有关(在 passwordEncoder() 方法中)?

UPD.:服务器使用 389-Server

标签: javaspringldap

解决方案


对于解决方案,您需要在配置请求时指定系统帐户数据:

auth
  .ldapAuthentication()
    .userSearchBase("ou=Пользователи")
    .userSearchFilter("sAMAccountName={0}")
      .contextSource()
        .url(sourceUrl)
        .managerDn(managerLogin)        // <--- this 
        .managerPassword(managerPassw); // <--- this

推荐阅读