我已经使用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文件,但是它不起作用,因此我需要有关此问题的帮助。
您找到答案了吗?我有同样的问题。