java - 使用 Spring Data LdapRepository 创建新用户失败,LDAP 错误代码为 32

标签 java spring ldap spring-data

我正在尝试使用 Spring Data 和 LdapRepository api 在 Ldap 中读取、更新和创建用户数据。我已经能够成功读取和更新,但每当我尝试插入新记录时,我都会收到此错误:

2017-07-25 17:31:01.966 ERROR 14404 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 
nested exception is org.springframework.ldap.NameNotFoundException: 
[LDAP: error code 32 - The search base entry 'uid=testy1@testy.org,ou=people,ou=myou,dc=bar,dc=foo' does not exist]; 
nested exception is javax.naming.NameNotFoundException: 
[LDAP: error code 32 - The search base entry 'uid=testy1@testy.org,ou=people,ou=myou,dc=bar,dc=foo' does not exist]; remaining name 'uid=testy1@testy.org'] 
with root cause
javax.naming.NameNotFoundException: [LDAP: error code 32 - The search base entry 'uid=testy1@testy.org,ou=people,ou=myou,dc=bar,dc=foo' does not exist]

我的 application.properties 是:

user-api.ldap.contextSource.url=ldap://server.address.com:1389
user-api.ldap.contextSource.userDn=cn=manager role
user-api.ldap.contextSource.password=apasswordwashere
user-api.ldap.contextSource.base=ou=people,ou=myou,dc=bar,dc=foo

我的 repo 接口(interface),LdapUserRepository.java:

public interface LdapUserRepository extends LdapRepository<LdapUser>{

   LdapUser findByCn(String cn);

   LdapUser findBySn(String sn);

   LdapUser save(LdapUser ldapUser);
}

无论是否显式公开保存 方法,我都会得到相同的响应。

我的配置是在 LdapConfiguration.java 中完成的:

@Configuration
@EnableLdapRepositories(basePackages = "foo.bar.userapi.dao.ldap", ldapTemplateRef="userLdapTemplate")
public class LdapConfiguration {

    @Autowired
    Environment env;

    @Bean
    public LdapContextSource contextSource () {
        LdapContextSource contextSource= new LdapContextSource();
        contextSource.setUrl(env.getRequiredProperty("user-api.ldap.contextSource.url"));
        contextSource.setBase(env.getRequiredProperty("user-api.ldap.contextSource.base"));
        contextSource.setUserDn(env.getRequiredProperty("user-api.ldap.contextSource.userDn"));
        contextSource.setPassword(env.getRequiredProperty("user-api.ldap.contextSource.password"));
        return contextSource;
    }

    @Bean(name="userLdapTemplate")
    public LdapTemplate ldapTemplate() {
        return new LdapTemplate(contextSource());       
    }

}

还有我的测试例程:

public void test() {
   LdapUser ldapUser =  new LdapUser();

   ldapUser.setCn("198777777");
   ldapUser.setCountry("United States");
   ldapUser.setGivenName("Hepsibah");
   ldapUser.setIsActive("true");
   ldapUser.setSn("Testerson91");
   ldapUser.setStatus("active");
   ldapUser.setUid(LdapUtils.newLdapName("uid=testy1@testy.org"));
   ldapUser.setUserModifyTimestamp("20160222145439Z");
   ldapUser.setUserPassword("Password-12345");
   ldapUser = ldapUserRepository.save(ldapUser);
}

要强调的是,配置适用于除插入之外的所有内容,因为如果我这样做,它会起作用:

   public void test2() {
          LdapUser ldapUser =  ldapUserRepository.findBySn("Testerson");
          if ( ldapUser != null) {
                 System.out.println(ldapUser.getUid().toString());
                 System.out.println("result: " + ldapUser.toString());

                 ldapUser.setSn("Testerson2");
                 ldapUser.setUserPassword("Password-12345!");
                 ldapUser = ldapUserRepository.save(ldapUser);

                 ldapUser = ldapUserRepository.findBySn("Testerson2");
                 if ( ldapUser != null) {
                       System.out.println("result: " + ldapUser.toString());
                 }
          }
   }

我怀疑答案可能与 Ldap error code 32 while adding user to ldap 有关但我没有在保存操作期间明确添加 dn,并且更新操作工作正常。

检索记录、更改 uid 值并保存它会产生相同的错误。检索记录并检查 uid 仅显示“uid=testy1@testy.org”部分,而不显示“ou=people,ou=myou,dc=bar,dc=foo”部分。

最佳答案

假设正在使用默认实现(SimpleLdapRepository - 如果您自己没有实现存储库接口(interface),您将得到这个)那么当 SimpleLdapRepository 试图确定它应该“保存”还是“更新”时就会出现问题你的实体。来自 here SimpleLdapRepository 中的代码如下所示:

 @Override
 public <S extends T> S More ...save(S entity) {
     Assert.notNull(entity, "Entity must not be null");
     Name declaredId = odm.getId(entity);

     if (isNew(entity, declaredId)) {
         ldapOperations.create(entity);
     } else {
         ldapOperations.update(entity);
     }

     return entity;
 }

要获得您看到的错误,很可能您的实体未被确定为新实体,当发现该实体不存在且无法更新时,执行此更新失败。

isNew() 方法如下所示:

     private <S extends T> boolean More ...isNew(S entity, Name id) {
     if (entity instanceof Persistable) {
         Persistable<?> persistable = (Persistable<?>) entity;
         return persistable.isNew();
     } else {
         return id == null;
     }
 }

在您的示例中,您似乎需要设置 ID。所以另一种选择是实现 Persistable 接口(interface)和 isNew() 方法来返回您的实体是否是新的。如果您的实体被确定为新的(您可以在保存之前调用例如 ldapUser.setNew(true);),它将被保存而不是更新。

关于java - 使用 Spring Data LdapRepository 创建新用户失败,LDAP 错误代码为 32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45309925/

相关文章:

java - 升级到Java 7后编译报错: package sun. security.pkcs11不存在

java - 打开套接字时 CPU 密集程度如何

java - EclipseLink @MappedSuperclass 和泛型

spring - 在哪里可以使用@Transactional

java - spring boot 2.0.1 版本中的 GridFSDBFile

c# - 安全 LDAP 身份验证问题

c# - 事件目录查询问题

java - 从 android 中的 Activity X 启动 Activity X

java - 带有多部分文件上传的 Spring REST API 不适用于可选参数

java - 如何从我的Java项目中删除com.sun.jndi.ldap引用?