我正在尝试使用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 error code 32 while adding user to ldap有关但我在保存操作期间没有明确地添加dn,并且更新操作正常。
检索记录,更改uid值并保存它会产生相同的错误。检索记录并检查uid只显示“uid=testy1@testy.org”部分,而不是“ou = people,ou = myou,dc = bar,dc = foo”部分。
答案 0 :(得分:3)
假设正在使用默认实现(SimpleLdapRepository - 如果你自己没有实现存储库接口,那么你会得到这个)然后当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接口和isNew()
方法来返回你的实体是否是新的。如果您的实体被确定为新的(您可以在保存前调用ldapUser.setNew(true);
),则会保存而不是更新。