【问题标题】:How to add entry to LDIF file using Spring Boot embedded ldap server如何使用 Spring Boot 嵌入式 ldap 服务器向 LDIF 文件添加条目
【发布时间】:2019-04-17 10:49:15
【问题描述】:

我已经使用 unboundid 作为嵌入式 ldap 服务器构建了一个带有 LDAP 身份验证的 Spring Boot REST 应用程序。身份验证基于简单的 LDIF 文件,现在我需要向该文件添加新条目的能力,以便稍后使用这些条目进行身份验证。如何将新条目直接保存到 LDIF?

我曾尝试使用LdapTemplate 来做到这一点,但它仅适用于一个应用程序会话(据我所知,LdapTemplate 将新条目添加到一些“内部的、一个会话的”LDAP)以及当应用程序停止,LDIF 文件保持不变。

这是我的 application.properties 文件

#LDAP config
spring.ldap.embedded.base-dn=dc=time-tracking-service,dc=com
spring.ldap.embedded.credential.username=uid=admin
spring.ldap.embedded.credential.password=pass1
spring.ldap.embedded.ldif=classpath:users.ldif
spring.ldap.embedded.validation.enabled=false
spring.ldap.embedded.port=8389
ldap.url=ldap://localhost:8389/

这是我的入门类

@Entry(
    objectClasses = {"inetOrgPerson", "organizationalPerson", "person", "top"}
)
@Data
@NoArgsConstructor
@AllArgsConstructor
public final class LdapPerson{

    @Id
    private Name dn;

    @DnAttribute(value = "uid", index = 1)
    private String uid;

    @DnAttribute(value = "ou", index = 0)
    @Transient
    private String group;

    @Attribute(name = "cn")
    private String fullName;

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

    @Attribute(name = "userPassword")
    private String password;

    public LdapPerson(String uid, String fullName, String lastName, String group, String password) {
        this.dn = LdapNameBuilder.newInstance("uid=" + uid + ",ou=" + group).build();
        this.uid = uid;
        this.fullName = fullName;
        this.lastName = lastName;
        this.group = group;
        this.password = password;
    }

还有我的 LdapConfig

@Configuration
@PropertySource("classpath:application.properties")
@EnableLdapRepositories
public class LdapConfig {

    @Autowired
    private Environment env;

    @Bean
    public LdapContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setUrl(env.getProperty("ldap.url"));
        contextSource.setBase(env.getRequiredProperty("spring.ldap.embedded.base-dn"));
        contextSource.setUserDn(env.getRequiredProperty("spring.ldap.embedded.credential.username"));
        contextSource.setPassword(env.getRequiredProperty("spring.ldap.embedded.credential.password"));
        contextSource.afterPropertiesSet();
        return contextSource;
    }

    @Bean
    public LdapTemplate ldapTemplate() {
        return new LdapTemplate(contextSource());
    }
}

我只是使用添加条目

ldapTemplate.create(ldapPerson);

我希望使用 LdapTemplate 能够向 LDIF 文件添加新条目,但它不起作用,所以我需要帮助解决这个问题。

【问题讨论】:

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


    【解决方案1】:

    据我了解,LdapTemplate 将新条目添加到一些“内部的、单会话的”LDAP

    有点晚了,但你是对的,Spring 的嵌入式 LDAP 在保存时不会更改 LDIF 文件的内容(并且几乎没有 LDAP 实现)

    ldapTemplate.create(ldapPerson); 只是在您上面初始化的内存中 LDAP 实例中创建一条新记录。当您终止应用程序时,一切都将丢失。

    如果您想持久化数据,您必须与一个LDAP implementations 集成。此外,LdapTemplate 可通过 org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration 在 Spring Boot 上进行配置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 1970-01-01
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      相关资源
      最近更新 更多