Sercan Ozdemir
Sercan Ozdemir

Reputation: 4692

OpenLdap server, Spring ldap user bind (inetOrgPerson) No such object

i just installed a OpenLDAP server and trying to connect, add, get users,groups on it. Since i 'm very new on ldap it would be very nice if you share tutorials about spring-ldap :/

So here's my bind code:

User class:

public class User implements Serializable{

    private String id;
    private String userName;
    private String firstName;
    private String lastName;
    private String email;
    private String password;
    private String department;
    private String groups[];
}

save method:

public User save( final User user ){
        init();
        Name dn = buildDn( user );
        Attributes attributes = buildAttributes( user );

        logger.info( "trying to save DN  " + dn + " and attributes " + attributes );

        ldapTemplate.bind( dn, null, attributes );

        // Update Groups
        for( String group : user.getGroups() ){
            try{
                DistinguishedName groupDn = new DistinguishedName();
                groupDn.add( "ou", "Groups" );
                groupDn.add( "cn", group );
                DirContextOperations context = ldapTemplate.lookupContext( groupDn );
                context.addAttributeValue( "memberUid", user.getUserName() );
                ldapTemplate.modifyAttributes( context );
            }
            catch( Exception e ){
                e.printStackTrace();
            }
        }

        logger.info( "successfuly saved " );
        return user;

    }

buildDN:

private Name buildDn( final User user ){
        DistinguishedName dn = new DistinguishedName();
        dn.add( "ou", "People" );
        if( user.getDepartment() != null ){
            dn.add( "ou", user.getDepartment() );
        }
        dn.add( "uid", user.getUserName() );
        return dn;
    }

buildAttributes:

private Attributes buildAttributes( final User user ){
        Attributes attrs = new BasicAttributes();
        BasicAttribute ocattr = new BasicAttribute( "objectClass" );
        // ocattr.add( "person" );
        ocattr.add( "inetOrgPerson" );
        attrs.put( ocattr );
        attrs.put( "cn", user.getFirstName() );
        // attrs.put( "rdn", "uid" ); // TODO check how to assign rdn
        attrs.put( "sn", user.getLastName() );
        attrs.put( "userPassword", "{SHA}" + this.encrypt( user.getPassword() ) );
        attrs.put( "mail", user.getEmail() );

        return attrs;
    }

and here's the exception:

org.springframework.ldap.NameNotFoundException: [LDAP: error code 32 - No Such Object]; nested exception is javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object]; remaining name 'uid=sercan,ou=People'
    at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:183)
    at org.springframework.ldap.core.LdapTemplate.executeWithContext(LdapTemplate.java:820)
    at org.springframework.ldap.core.LdapTemplate.executeReadWrite(LdapTemplate.java:812)
    at org.springframework.ldap.core.LdapTemplate.bind(LdapTemplate.java:990)
    at com.***.usr.mng.library.service.impl.UserServiceImpl.save(UserServiceImpl.java:101)
    at com.***.usr.mng.library.handler.impl.UserHandlerImpl.saveUser(UserHandlerImpl.java:45)
    at com.***.usr.mng.library.handler.impl.UserManagementHandlerImpl.handleRequest(UserManagementHandlerImpl.java:62)
    at com.***.usr.mng.service.controller.UserManagementController.processRequest(UserManagementController.java:83)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:685)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object]; remaining name 'uid=sercan,ou=People'
    at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3112)
    at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:3033)
    at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2840)
    at com.sun.jndi.ldap.LdapCtx.c_bind(LdapCtx.java:420)
    at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_bind(ComponentDirContext.java:295)
    at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.bind(PartialCompositeDirContext.java:215)
    at javax.naming.directory.InitialDirContext.bind(InitialDirContext.java:182)
    at org.springframework.ldap.core.LdapTemplate$21.executeWithContext(LdapTemplate.java:992)
    at org.springframework.ldap.core.LdapTemplate.executeWithContext(LdapTemplate.java:817)

Upvotes: 2

Views: 3518

Answers (2)

marthursson
marthursson

Reputation: 3300

While the code you present will certainly work for doing what you want there are a couple of things included in the library that will make things easier for you.

The simplest approach is to use the Object-Directory-Mapping - ODM - feature, which pretty much removes all boiler plate and uses declarative mapping on the domain classes instead.

If you decide to continue using DirContextAdapter I would encourage you to take full advantage of what it can do (more specifically you can submit a DirContextOperation instance directly to the designated bind method):

Name dn = buildDn(user);
DirContextAdapter context = new DirContextAdapter(dn);

context.setAttributeValues("objectclass", new String[] {"top", "person"});
context.setAttributeValue("cn", user.getFullname());
// populate more attributes

ldapTemplate.bind(context);

The reference documentation has plenty of examples, and there are also a couple of sample applications included with the source to help you get started (there's one simple sample using ODM, another one using DirContextOperations, and finally a complete user admin application using ODM).

Finally, please note that the DistinguishedName class is deprecated in version 2.x; you should use the LdapNameBuilder or the utility methods in LdapUtils for working with Distinguished Names (which you will typically not have to do if you use ODM).

Upvotes: 1

Sercan Ozdemir
Sercan Ozdemir

Reputation: 4692

I found the solution,

dn.add( "ou", "People" );

i'm trying to add person to People organizational unit, but it does not exist !

Thanks.

Upvotes: 0

Related Questions