簡體   English   中英

使用Spring錯誤代碼32,使用Spring Data LdapRepository創建新用戶失敗

[英]Creating new users with Spring Data LdapRepository fails with LDAP error code 32

我試圖使用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接口,LdapUserRepository.java:

public interface LdapUserRepository extends LdapRepository<LdapUser>{

   LdapUser findByCn(String cn);

   LdapUser findBySn(String sn);

   LdapUser save(LdapUser ldapUser);
}

無論是否明確公開save方法,我都得到相同的響應。

我的配置在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時使用Ldap錯誤代碼32但是我在保存操作期間沒有明確地添加dn,並且更新操作正常。

檢索記錄,更改uid值並保存它會產生相同的錯誤。 檢索記錄並檢查uid只顯示“uid=testy1@testy.org”部分,而不是“ou = people,ou = myou,dc = bar,dc = foo”部分。

假設正在使用默認實現(SimpleLdapRepository - 如果您還沒有自己實現存儲庫接口,則會得到這個),那么當SimpleLdapRepository嘗試確定它是應該“保存”還是“更新”您的實體時,就會出現問題。 這里開始 ,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接口和isNew()方法來返回你的實體是否是新的。 如果您的實體被確定為新的(您可以調用例如ldapUser.setNew(true);在保存之前),它將被保存而不是更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM