为具有基本DN的测试设置Spring Data LDAP Embedded

时间:2018-01-10 20:49:47

标签: java spring spring-data spring-ldap unboundid-ldap-sdk

我有一个奇怪的Spring Data Ldap行为,我想知道如何对抗它。

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

spring:
  ldap:
    urls: ldap://localhost:389
    base: dc=e-mehlbox,dc=eu
    username: cn=admin,dc=e-mehlbox,dc=eu
    password: root

在我的application.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。以下是工作配置的相应行。" real" 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 Data LDAP相关的依赖项(我的项目是Gradle驱动的):

compile (
    "org.springframework.boot:spring-boot-starter-data-ldap:1.5.9.RELEASE",
    "org.springframework.data:spring-data-ldap:1.0.9.RELEASE"
)

testCompile (
    "org.springframework.ldap:spring-ldap-test:2.3.2.RELEASE",
    "com.unboundid:unboundid-ldapsdk:4.0.3"
)

这是我的一个实体类:

@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;

}

有什么想法吗?这是一个错误还是我没有看到明显的错误?

2 个答案:

答案 0 :(得分:1)

感谢@vdubus和this question,我明白了。

似乎嵌入式LDAP服务器版本没有设置配置的基本DN(请参阅其他SO问题)。但是将以下类添加到我的项目中会修复此问题:

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;
    }
}

答案 1 :(得分:0)

如果您只想在测试属性中解决它,则可以将基础添加到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