繁体   English   中英

设置 Spring Data LDAP Embedded 以使用基本 DN 进行测试

[英]Setting up Spring Data LDAP Embedded for tests with base DN

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

从它的外观来看,当我使用“正确的”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

在我的 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。 以下是“真实”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;

}

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

感谢@vdubus 和这个问题,我让它工作了。

似乎嵌入式 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;
    }
}

如果您更喜欢纯粹在测试属性中解决它,您可以改为将 base 添加到 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM