Как добавить запись в файл LDIF, используя встроенный в Spring Boot ldap сервер - PullRequest
1 голос
/ 17 апреля 2019

Я создал весеннее загрузочное REST-приложение с аутентификацией LDAP, используя unboundid в качестве встроенного ldap-сервера.Аутентификация основана на простом файле 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, но он не работает, поэтому мне нужна помощь с этой проблемой.

...