如何使用Spring Boot在ldif文件(嵌入式LDAP)中创建新条目

时间:2019-10-22 08:21:59

标签: spring-boot ldap spring-ldap ldif

我正在尝试在使用Spring Boot完美配置的LDIF文件中插入用户详细信息,因为即使我能够使用LdapTemplate.bind()方法,我也能够获取数据。

我尝试了各种代码,最后,我能够使用ldapTemplate.bind()方法,但是它仅将我的条目与LDAP树绑定,这意味着ldif文件中没有条目,并且当我重新启动LDAP服务器时,该条目会丢失。

错误:

org.springframework.ldap.InvalidNameException: Invalid name: abcdtestuser@gmail.com; nested exception is javax.naming.InvalidNameException: Invalid name: abcdtestuser@gmail.com
    at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:136)
    at org.springframework.ldap.support.LdapUtils.newLdapName(LdapUtils.java:416)
    at org.springframework.ldap.support.LdapNameBuilder.add(LdapNameBuilder.java:121)
    at in.indg.cdac.ldap.service.LdapService.saveInLdap(LdapService.java:71)
    at in.indg.cdac.ldap.service.LdapService.create(LdapService.java:57)
    at in.indg.cdac.ldap.controller.LdapController.saveUserDetail(LdapController.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

实体:

@Entry(base="ou=vikaspedia", objectClasses = {"inetOrgPerson", "person", "top"})
public class User {

    @Id
    private Name id;
    private @Attribute(name = "mail") String uid;
    private @Attribute(name = "cn") String firstName;
    private @Attribute(name = "sn") String lastName;
    private @Attribute(name = "userPassword") String password;
    private @Attribute(name = "description") String userrole;

创建方法:

    public String create(UserResponsePojo userResponsePojo) {
        try {
            Name dn = LdapNameBuilder
                      .newInstance()
                      .add("ou", "vikaspedia")
                      .add("mail", userResponsePojo.getUid())
//                    .add("cn", userResponsePojo.getUsername())
                      .build();
                    DirContextAdapter context = new DirContextAdapter(dn);

//                  Name id = LdapNameBuilder
//                          .newInstance()
//                          .add(userResponsePojo.getUid())
//                          .build();

                    context.setAttributeValues("objectclass", new String[]{"inetOrgPerson", "person", "top"});
                    context.setAttributeValue("cn", userResponsePojo.getUsername());
                    context.setAttributeValue("sn", userResponsePojo.getLastname());
                    context.setAttributeValue("description", userResponsePojo.getUserrole());
                    context.setAttributeValue("mail", userResponsePojo.getUid());
                    context.setAttributeValue("userPassword", digestSHA(userResponsePojo.getPassword()));

                    ldapTemplate.bind(context);
                    ldapTemplate.create(context);

也许Id的自动递增可以解决问题,但是我不知道如何像使用JPA @GeneratedValue(strategy = GeneratedType.IDENTITY)那样实现这一目标

任何帮助将不胜感激。预先感谢!

0 个答案:

没有答案