我尝试使用 ldap 与 spring 集成
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-ldap</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
我试图获取测试 Ldap 服务器中的所有用户,但是当我使用 LdapRepository 接口(interface)的 findAll 方法时,我得到的只是一个空列表。我在互联网上阅读了很多,很多文档,但我还没有发现我正在尝试做的这个任务出了什么问题。
以下是一些代码示例: 配置文件:
@Configuration
@PropertySource("classpath:application.yaml")
@EnableLdapRepositories(basePackages = "gr.mpass.aia.ldap")
public class AppConfig {
@Autowired
private Environment env;
@Bean
public LdapContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl(env.getRequiredProperty("ldap.url"));
contextSource.setUserDn(env.getRequiredProperty("ldap.principal"));
contextSource.setPassword(env.getRequiredProperty("ldap.password"));
return contextSource;
}
@Bean
public LdapTemplate ldapTemplate() {
return new LdapTemplate(contextSource());
}
@Bean
public LdapClientService ldapClient() {
return new LdapClientService();
}
}
入门级:
@Entry(base = "cn=eshop,ou=mpass,cn=admin,dc=userlogin,dc=mpass,dc=ltd", objectClasses = { "posixGroup", "inetOrgPerson", "top" })
public class LdapUser {
@Id
private Name id;
private @Attribute(name = "cn") String fullName;
private @Attribute(name = "mail") String email;
private @Attribute(name = "uidNumber") String uidNumber;
public LdapUser() {
}
public LdapUser(String fullName, String email, String uIdNumber) {
this.fullName = fullName;
this.email = email;
this.uidNumber = uidNumber;
}
public Name getId() {
return id;
}
public void setId(Name id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmail() {
return email;
}
public void setEmail(String password) {
this.email = email;
}
public String getUidNumber() {
return uidNumber;
}
public void setUidNumber(String uidNumber) {
this.uidNumber = uidNumber;
}
@Override
public String toString() {
return fullName;
}
}
存储库:
@Repository
public interface LdapUserRepository extends LdapRepository<LdapUser> {
LdapUser findByFullName(String fullName);
LdapUser findByEmail(String email);
LdapUser findByUidNumber(String uidNumber);
List<LdapUser> findByEmailLikeIgnoreCase(String email);
List<LdapUser> findByEmailNotLike(String email);
}
获取用户查询:
LdapQuery query = query().base("cn=eshop,ou=mpass,cn=admin,dc=userlogin,dc=mpass,dc=ltd")
.searchScope(SearchScope.ONELEVEL)
.where("objectclass").is("inetOrgPerson");
Iterable<LdapUser> ldapUsers = ldapUserRepository.findAll(query);
这将返回一个空列表。
我尝试了不带参数的 ldapUserRepository.findAll() 但它不起作用。它显示 javax.naming.NameNotFoundException: [LDAP: 错误代码 32 - 没有这样的对象]。
我尝试过这个答案 https://stackoverflow.com/a/48384297/1501205但这根本没有帮助。
最佳答案
您在查询和 Entry 对象中提供了条目的完整 DN 作为基础。我认为这些都有问题。正确的做法是:
在配置中:“cn=admin,dc=userlogin,dc=mpass,dc=ltd”
并在条目中:“cn=eshop,ou=mpass”
关于java - Spring data Ldap Repository 在使用 findAll 时返回 empry List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55359791/