【问题标题】:Spring data Ldap Repository return empry List when findAll is used使用 findAll 时 Spring data Ldap Repository 返回 empry List
【发布时间】:2019-03-29 18:30:44
【问题描述】:

我尝试使用 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 接口的 findAll 方法时,我得到的只是一个空列表。我在 Internet 上阅读了很多,很多文档,但我没有发现我正在尝试做的这个任务出了什么问题。

以下是一些代码示例: 配置文件:

@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: error code 32 - No such Object]。

我试过这个答案https://stackoverflow.com/a/48384297/1501205,但它根本没有帮助。

任何帮助将不胜感激。 提前致谢 这是 Ldap 服务器树的打印屏幕:

【问题讨论】:

    标签: java spring-boot ldap spring-data spring-ldap


    【解决方案1】:

    您在查询和条目对象中都提供了条目的完整 dn 作为基础。我认为这些有问题。正确的做法是:

    在配置中:"cn=admin,dc=userlogin,dc=mpass,dc=ltd" 并在条目中:"cn=eshop,ou=mpass"

    【讨论】:

      【解决方案2】:

      我的 Entry 中有一个错误的 ObjectClass。 PosixGroup 是错误的。我用 PosixAccount 替换了它,它可以工作。

      【讨论】:

        猜你喜欢
        • 2019-12-06
        • 1970-01-01
        • 2016-06-13
        • 2021-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-21
        相关资源
        最近更新 更多