Java 为带有基本DN的测试设置嵌入的Spring数据LDAP

Java 为带有基本DN的测试设置嵌入的Spring数据LDAP,java,spring,spring-data,spring-ldap,unboundid-ldap-sdk,Java,Spring,Spring Data,Spring Ldap,Unboundid Ldap Sdk,我对SpringDataLDAP有一种奇怪的行为,我想知道如何应对它 从外观上看,当我使用“适当的”LDAP服务器和嵌入式版本时,base信息似乎丢失或处理方式不同。 嵌入式版本应该用于我的一些集成测试。但是,当我这样配置LDAP服务器时,什么工作得非常好: spring: ldap: urls: ldap://localhost:389 base: dc=e-mehlbox,dc=eu username: cn=admin,dc=e-mehlbox,dc=eu

我对SpringDataLDAP有一种奇怪的行为,我想知道如何应对它

从外观上看,当我使用“适当的”LDAP服务器和嵌入式版本时,
base
信息似乎丢失或处理方式不同。 嵌入式版本应该用于我的一些集成测试。但是,当我这样配置LDAP服务器时,什么工作得非常好:

spring:
  ldap:
    urls: ldap://localhost:389
    base: dc=e-mehlbox,dc=eu
    username: cn=admin,dc=e-mehlbox,dc=eu
    password: root
在我的应用程序中.yml。但一旦我设置了嵌入式服务器,我的搜索就会失败:

spring:
  ldap:
    urls: ldap://localhost:9321
    base: dc=e-mehlbox,dc=eu
    username: uid=admin
    password: secret
    embedded:
      base-dn: dc=e-mehlbox,dc=eu
      credential:
        username: uid=admin
        password: secret
      ldif: classpath:test-schema.ldif
      port: 9321
      validation:
        enabled: false
启用调试时,将显示缺少的基本DN。以下是针对“真实”LDAP服务器的工作配置的对应行:

2018-01-10 18:06:55.296 DEBUG 23275 --- [           main] o.s.ldap.core.LdapTemplate               : Searching - base=ou=internal,ou=Users, finalFilter=(&(&(objectclass=inetOrgPerson)(objectclass=organizationalPerson)(objectclass=person)(objectclass=qmailUser))(uid=big.bird)), scope=javax.naming.directory.SearchControls@6a013bdd
2018-01-10 18:06:55.311 DEBUG 23275 --- [           main] o.s.l.c.support.AbstractContextSource    : Got Ldap context on server 'ldap://localhost:389/dc=e-mehlbox,dc=eu'
有趣的一点是Ldap上下文,其中包含基础

这是切换到嵌入式LDAP时的输出:

2018-01-10 18:08:42.836 DEBUG 23569 --- [           main] o.s.ldap.core.LdapTemplate               : Searching - base=ou=internal,ou=Users, finalFilter=(&(&(objectclass=inetOrgPerson)(objectclass=organizationalPerson)(objectclass=person)(objectclass=qmailUser))(uid=big.bird)), scope=javax.naming.directory.SearchControls@55202ba6
2018-01-10 18:08:42.871 DEBUG 23569 --- [           main] o.s.l.c.support.AbstractContextSource    : Got Ldap context on server 'ldap://localhost:9321'
我有点迷路了,因为我找不到任何其他配置选项来设置基本DN

我的项目的一些细节:

现在,我正在使用以下与Spring数据LDAP相关的依赖项(我的项目是Gradle驱动的):

下面是我的一个实体类:

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@EqualsAndHashCode(doNotUseGetters = true)
@ToString(doNotUseGetters = true)
@Entry(
        objectClasses = {"inetOrgPerson", "organizationalPerson", "person", "qmailUser"},
        base = "ou=internal,ou=Users")
public class User implements Serializable {

    @Id
    private Name dn;

    @Attribute(name = "entryUuid", readonly = true)
    private String entryUuid;

    @Attribute(name = "uid")
    private String username;

    @Attribute(name = "userPassword")
    private byte[] password;

    @Attribute(name = "mail")
    private String internalMailAddress;

    @Attribute(name = "mailAlternateAddress")
    private List<String> mailAddresses;

    @Attribute(name = "displayName")
    private String displayName;

    @Attribute(name = "accountStatus")
    private String status;

    @Attribute(name = "givenName")
    private String firstName;

    @Attribute(name = "sn")
    private String lastName;

    @Attribute(name = "mailMessageStore")
    private String mailboxHome;

}
@Builder
@AllArgsConstructor
@诺尔格构装师
@吸气剂
@塞特
@EqualsAndHashCode(donotusegatters=true)
@ToString(doNotUseGetters=true)
@入口(
objectClasses={“inetOrgPerson”、“organizationalPerson”、“person”、“qmailUser”},
base=“ou=内部,ou=用户”)
公共类用户实现可序列化{
@身份证
私名dn;
@属性(name=“entryUuid”,readonly=true)
私有字符串入口UUID;
@属性(name=“uid”)
私有字符串用户名;
@属性(name=“userPassword”)
专用字节[]密码;
@属性(name=“mail”)
私有字符串地址;
@属性(name=“mailAlternateAddress”)
私人名单邮寄地址;
@属性(name=“displayName”)
私有字符串显示名;
@属性(name=“accountStatus”)
私有字符串状态;
@属性(name=“givenName”)
私有字符串名;
@属性(name=“sn”)
私有字符串lastName;
@属性(name=“mailMessageStore”)
私人字符串mailboxHome;
}
有什么想法吗?这是一个bug还是只是我没有看到明显的问题?

多亏了@vdubus,我让它工作起来了

嵌入式LDAP服务器版本似乎没有设置配置的基本DN(请参阅另一个问题)。但将以下类添加到我的项目中可以解决此问题:

import com.unboundid.ldap.listener.InMemoryDirectoryServer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.ldap.LdapProperties;
import org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.env.Environment;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.support.LdapContextSource;

@Configuration
@EnableConfigurationProperties({LdapProperties.class, EmbeddedLdapProperties.class})
@ConditionalOnClass(InMemoryDirectoryServer.class)
public class EmbeddedLdapConf {

    private final Environment environment;
    private final LdapProperties properties;


    public EmbeddedLdapConf(Environment environment, LdapProperties properties) {
        this.environment = environment;
        this.properties = properties;
    }

    @Bean
    @DependsOn("directoryServer")
    public ContextSource ldapContextSource() {
        final LdapContextSource source = new LdapContextSource();
        source.setUrls(this.properties.determineUrls(this.environment));
        source.setBase(this.properties.getBase());
        return source;
    }
}

如果您希望纯粹在测试属性中解决这个问题,那么可以将基添加到ldap url中

我不知道为什么配置嵌入式ldap会弄乱正常的ldap配置,但通过这种方式,您至少可以验证它是一种仅属性的东西,不需要额外的代码就可以工作

 spring:
  ldap:
    urls:
      - ldap://localhost:12345/dc=stuff,dc=test,dc=my
    embedded:
      base-dn: dc=stuff,dc=test,dc=my
      ldif: classpath:test.ldif
      port: 12345
      validation:
        enabled: false

我也有同样的问题……事实上,即使是基本配置也不能正确地为我工作,我需要定义
LdapContextSource
LdapTemplate
,使其正常工作。但是,我发现(在代码中)对于嵌入式版本,通常不需要定义
spring.ldap.url
spring.ldap.base
属性,因为它是由嵌入式配置自动设置的。然而,对我来说,
spring.ldap.embedded.base dn
属性并没有像它应该的那样设置在
spring.ldap.base
中,即使url和端口部分正在生效。我发现了这篇文章,我认为它应该解决你的问题:作为信息,我已经创建了一个关于这个问题的网站。真棒。谢谢@vdubus
 spring:
  ldap:
    urls:
      - ldap://localhost:12345/dc=stuff,dc=test,dc=my
    embedded:
      base-dn: dc=stuff,dc=test,dc=my
      ldif: classpath:test.ldif
      port: 12345
      validation:
        enabled: false