Showing posts with label LDAP. Show all posts
Showing posts with label LDAP. Show all posts

Monday, September 5, 2011

Retrieve Users from LDAP server

I have already mentioned about how to set up LDAP server within Apache DS.Now you can add users to LDAP server by importing example.ldif file or creating users from scratch using this post.

After  following above steps you can view LDAP browser in Apache DS as follows,

Ex:-




Now it is the time to retrieve the users from LDAP server.
For that mainly I am using JAVA technology and Eclipse IDE.

Following is the simple code stuffs to retrieve all the users in LDAP server relevant to my LDAP browser as above and change colored stuffs which suit for your settings,

LdapSearch.java

import java.util.Properties;
import javax.naming.Context;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;

public class LdapSearch {

public static void main(String[] args) throws Exception {
       
Properties env = new Properties();              env.put("java.naming.factory.initial","com.sun.jndi.ldap.LdapCtxFactory");
env.put("java.naming.ldap.version", "3");
env.put("java.naming.provider.url", "ldap://localhost:10389");
env.put(Context.SECURITY_PRINCIPAL, "cn=jayan,ou=users,o=wso2");env.put(Context.SECURITY_CREDENTIALS, "075");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
       
DirContext ctx = null;
           
            try {
                ctx = new InitialDirContext(env);

                String url="ldap://localhost:10389/ou=users,o=wso2";
                                                                           // try changing yellow stuffs

                NamingEnumeration listResults = null;           
                listResults = ctx.list(url);
         
                while (listResults.hasMore()) {
                    NameClassPair ncp = (NameClassPair) listResults.next();                  
                    System.out.println(ncp.getName());        // users are here             
                }
            } catch (NamingException e) {               
                e.printStackTrace();
            }           
    }





Output :-
  
    
Download LdapSearch.java file ...

Thursday, August 25, 2011

Set up LDAP server using Apache Directory Studio on Linux

Basically we can set up LDAP server using command "apt-get install sldap".But when we are going to configure LDAP server which gives some troubles.Because of that, using Apache Directory Studio  we can set up and configure LDAP server easily.

1. Download Apache Directory Studio and extract it.

2. Then use command line and go to Apache DS home and use following command to run the Apache DS environment.
   
                  ~ApacheDS$ ./ApacheDirectoryStudio

3. Now we are going to set up LDAP SERVER within Apache DS.
First you have to open Servers Window using,
          Window -> Show View -> Other.. -> Apache DS -> Servers.

4.Let's create a new server clicking New Server icon in Servers Window and use a name for Server and edit default configuration setting by double clicking server name(server.xml) and start that server by clicking run icon.(I use default settings)

5. I have already created and started LDAP server and now I have to create a LDAP connection with running server to browse it as a tree structure.

6. For that use Menu Bar and open LDAP -> New Connection wizard and type Network Parameters and Authentication Parameters.

Network Parameters :
          Connection Name - {your own one}
          Host Name - {localhost}
          Port - {10389}

Authentication Parameters :
          Authentication Method - Simple Authentication
          Bind DN or User -  uid=admin ,ou =system
          Bind password - secret
         

7. Now go to Connection window and select your connection, then click open connection icon to browse the tree structure of LDAP server within LDAP browser window.

8. Then you can enjoy the LDAP server within Apache Directory Studio.