javaspring-securityldapspring-ldapapacheds

How to fix Attribute sn not declared in objectClasses of entry


I am trying to create an entry using Spring-Data-LDAP and when I try to save entry it gives me Attribute sn not declared in objectClasses of entry ou=users,dc=example,dc=com . I am new to LDAP.

I have created a model class that contains basic attributes and I have auto wired LdapRepository.

The problem is I am able to retrieve the entries from Apache DS ldap but not able to save the Entry with my model.

Below is the model class.


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

    private static final String BASE_DN = "dc=example,dc=com";

    @JsonIgnore
    @Id
    private Name id;
    @JsonProperty("userName")

    private @Attribute(name="uid")  String uid;
    @JsonProperty("firstName")
    private @Attribute(name = "cn") String firstName;
    @JsonIgnore
    private @Attribute(name = "displayname") String displayName;
    @JsonProperty("lastName")

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

    public User() {

    }

    public User(String uid, String firstName, String displayName, String lastName) {
        this.uid = uid;
        this.firstName = firstName;
        this.displayName = displayName;
        this.lastName = lastName;
    }
    public User(String userName, String firstName, String lastName) {       
         Name dn = LdapNameBuilder.newInstance()
                 .add("ou", "users")                 
                 .build();
        this.id = dn;       
        this.uid = userName;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    // Setter and Getter
}

Below is the ldif file,

dn: dc=example,dc=com
objectclass: top
objectclass: domain
dc: example

dn: ou=users,dc=example,dc=com
objectClass: organizationalUnit
objectClass: top
ou: users

Below is the error log

There was an unexpected error (type=Internal Server Error, status=500).
[LDAP: error code 65 - OBJECT_CLASS_VIOLATION: failed for MessageType : MODIFY_REQUEST Message ID : 2 Modify Request Object : 'ou=users,dc=example,dc=com' Modification[0] Operation : add Modification uid: abc Modification[1] Operation : add Modification cn: Abc Modification[2] Operation : add Modification sn: Xyzsorg.apache.directory.api.ldap.model.message.ModifyRequestImpl@1b7e931e ManageDsaITImpl Control Type OID : '2.16.840.1.113730.3.4.2' Criticality : 'false' ' : ERR_277 Attribute sn not declared in objectClasses of entry ou=users,dc=example,dc=com]

Thanks In Advance


Solution

  • Not sure of the code but it appears you are building the DN for the user in this line:

    Name dn = LdapNameBuilder.newInstance()
                     .add("ou", "users")                 
                     .build();
    

    Which does NOT contain the uid of the user.

    The AddRequest must be:

    AddRequest ::= [APPLICATION 8] SEQUENCE 
    {
        entry           LDAPDN,
        attributes      AttributeList 
    } 
    

    Where the LDAPDN is the Fully Distinguished Name of the entry.