无法使用JNDI将信息作为属性存储在LDAP中

时间:2014-03-24 12:56:56

标签: java ldap jndi apacheds

我正在尝试根据给定的here教程将java对象绑定为一组属性

//In the main() method of the program
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:10389/o=csRepository");
env.put(Context.OBJECT_FACTORIES, "sample.UserFactory");
 // Create the initial context
DirContext ctx = new InitialDirContext(env);

User usr = new User("Goro");
ctx.bind("cn=Kill Bill, ou=employees", null, usr.getAttributes(""));

我的User类如下,

public class User implements DirContext {
    String employer;
    Attributes myAttrs;

    public User(String employer){
          this.employer = employer;
          myAttrs = new BasicAttributes(true);
          Attribute oc = new BasicAttribute("objectclass");
          oc.add("extensibleObject");
          oc.add("top");

          myAttrs.put(oc);
          myAttrs.put("em", employer);
       }

    public Attributes getAttributes(String name) throws NamingException {
        if (!name.equals("")) {
            throw new NameNotFoundException();
        }
        return (Attributes) myAttrs.clone();
    }
    //other methods
}

当我试图绑定时,

javax.naming.directory.NoSuchAttributeException: [LDAP: error code 16
- NO_SUCH_ATTRIBUTE: failed for MessageType : ADD_REQUEST Message ID : 2
    Add Request : Entry
    dn[n]: cn=Kill Bill, ou=employees,o=csRepository
    objectclass: extensibleObject
    objectclass: top
    cn: Kill Bill
    em: Goro
    ManageDsaITImpl Control
        Type OID    : '2.16.840.1.113730.3.4.2'
        Criticality : 'false' ' : ERR_04269 ATTRIBUTE_TYPE for OID em does not exist!]; remaining name 'cn=Kill Bill, ou=employees'

1 个答案:

答案 0 :(得分:1)

  

为什么它抱怨OID em不存在。

因为它没有。使用extensibleObject授权您将任何现有属性添加到对象。它并没有授权你创造新的属性。