Search code examples
oracle-databasespring-securityoracle11gldapspring-security-ldap

LDAP: Empty binddn"" and Authentication type becomes simple from none


Am Developing an application in Spring MVC & JPA with Spring Security. Now Integrating the OUD (Oracle Unified Directory) through LDAP. While User authentication the response from the LDAP in OUD log file is

CONNECT conn=909681 from ******* to *******:1636 protocol=LDAPS

Bind REQ conn=909681 op=0 msgID=1 type=SIMPLE dn="" version=3

BindRES conn=909681 op=0 msgID=1 result=1 message="The directory server could not find a network group for the bind dn "" because the client connection does not match the connection criteria for any network groups."

DISCONNECT conn=909681 reason="Client Disconnect"

Mapping between application and LDAP in security.xml file

<bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
    <constructor-arg value="ldaps://192.168.0.182:1636/o=company"/>
    <property name="userDn" value="cn=userid,o=company,ou=groups"/>
    <property name="password" value="password"/>
</bean>

<bean id="ldapAuthProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider" >
     <constructor-arg>
          <bean class="in.web.service.impl.CustomLdapBindAuthenticator">
               <constructor-arg ref="contextSource"/>
               <property name="userDnPatterns">
                   <list>
                       <value>cn={0},ou=groups</value>
                   </list>
               </property>
           </bean>
     </constructor-arg>
     <constructor-arg>
           <bean class="in.web.service.impl.CustomLdapUserAuthoritiesPopulator">
           </bean>
     </constructor-arg>     
 </bean>

Code in CustomLdapBindAuthenticator

public LdapUserDetails search(String cn) throws Exception {
        Hashtable env = new Hashtable();
        String sp = "com.sun.jndi.ldap.LdapCtxFactory";
        env.put(Context.INITIAL_CONTEXT_FACTORY, sp);

        String [] urls = contextSource.getUrls();
        for(String url: urls){
            System.out.println("ldapurls="+url);
        }
        env.put(Context.PROVIDER_URL, urls[0]);

        DirContext dctx = new InitialDirContext(env);

        String base = "ou=groups,o=company";
        System.out.println("BASE DN="+base);

        SearchControls sc = new SearchControls();
        String[] attributeFilter = {"cn", "fullName", "mail", "l", "mobile"};
        sc.setReturningAttributes(attributeFilter);
        sc.setSearchScope(SearchControls.SUBTREE_SCOPE);

        String filter = "(&(cn="+cn+")(objectClass=*))";

        NamingEnumeration results = dctx.search(base, filter, sc);
        LdapUserDetails user = new LdapUserDetails();
        while (results.hasMore()) {
            SearchResult sr = (SearchResult) results.next();
            Attributes attrs = sr.getAttributes();

            user.setCn(getAttribute(attrs, "cn"));
            user.setFullName(getAttribute(attrs, "fullName"));
            user.setMail(getAttribute(attrs, "mail"));
            user.setMobile(getAttribute(attrs, "mobile"));
            user.setLocation(getAttribute(attrs, "l")); 
        }
        dctx.close();
        return user;
        }

Pl. Help me to solve the issues

  1. I didn't mention the authentication as simple. From where i am getting type=simple in log file
  2. Why I am getting dn=""

Thanks in advance


Solution

  • Default authentication is "simple Bind" which means a DN and a password. You are not setting any credentials in the Environment env, so JNDI does the default of trying to do a Bind anonymous, i.e. empty DN and password.