【问题标题】:Setting up Spring Data LDAP Embedded for tests with base DN为具有基本 DN 的测试设置 Spring Data LDAP Embedded
【发布时间】:2018-01-10 20:49:47
【问题描述】:

我对 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 服务器的 working 配置的相应行:

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;

}

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

【问题讨论】:

  • 我也有同样的问题……事实上,即使是基本配置也无法正常工作,我需要定义 LdapContextSourceLdapTemplate 才能使其正常工作。但是,我能够发现(在代码中)通常,对于嵌入式版本,您不需要定义 spring.ldap.urlsspring.ldap.base 属性,因为它是由嵌入式配置自动设置的。然而,对我来说,spring.ldap.embedded.base-dn 属性并没有按照应有的方式设置到 spring.ldap.base 中,即使 url 和端口部分正在生效。
  • 发现这篇文章,我认为应该可以解决您的问题:stackoverflow.com/questions/43785743/…

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


【解决方案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;
    }
}

【讨论】:

  • 关于这个问题,我创建了一个issue
  • 太棒了。谢谢@vdubus
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2022-10-21
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 2020-06-01
    • 1970-01-01
    相关资源
    最近更新 更多