----------------------------------------------------------------
BEFORE YOU POST, search the faq at <http://java.apache.org/faq/>
WHEN YOU POST, include all relevant version numbers, log files,
and configuration files.  Don't make us guess your problem!!!
----------------------------------------------------------------

> Hello List,
> 
> The following is my requirement :
> I am storing User information (User name, password, group information)
> in Netscape Directory Server 4.0.
> 
> I now want to authenticate and authorize the user for my application
> using the information in LDAP Directory.
> 
> How can my servlets talk to LDAP directory ?
> 
> I am a newbie here and any information from you guys
> would be extremely helpful. Resources to this are also welcome.
> 
        Try Sun's JNDI (Java Naming and Directory Interface)

        http://java.sun.com/products/jndi/

        Read the tutorial...

        http://java.sun.com/products/jndi/tutorial/

        I'm doing very similar work.  Who are you?  What's your project?
(suggest we take this discussion off the list)

        here's a sample example code...

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import javax.naming.*;
import javax.naming.directory.*;

import java.util.Hashtable;

import java.util.Properties;

public class PowerUnits extends HttpServlet {

    public void service (HttpServletRequest request, HttpServletResponse
response)
        throws ServletException, IOException
    {
        // set content type and other response header fields first
        response.setContentType("text/html");

        // get the communication channel with the requesting client
        PrintWriter out = response.getWriter();

        // get the server identification
        String server =
getServletConfig().getServletContext().getServerInfo();

        // write the data
        out.println("<HTML><HEAD>");
                out.println("<pre>");

                String client_cn = "Good User";
                String base_dn = "ou=Users,o=CAI Example,c=US";
                String ldap_host = "localhost";
                String ldap_port = "389";

                out.println("<h1>Directory Data</h1>");
                // Set up environment for creating initial context

                Hashtable env = new Hashtable(11);
                env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
                
                // set the server details
                env.put(Context.PROVIDER_URL, 
                        "ldap://" + ldap_host + ":" + ldap_port + "/" +
base_dn);
                
                // authentication type
                env.put(Context.SECURITY_AUTHENTICATION, "none");
                
                // enable tracing
                env.put("com.sun.naming.ldap.trace.ber", System.err);
                
                try 
                {
                        // Create initial context
                        DirContext ctx = new InitialDirContext(env);
                        
                        // retreive an object from the directory
                        Attributes answer = ctx.getAttributes("cn=" +
client_cn);
                        // print the attributes
                     for (NamingEnumeration ae = answer.getAll();
ae.hasMore();) 
                     {
                        Attribute attr = (Attribute)ae.next();
                        out.println("attribute: " + attr.getID());
                        // print each value 
                        for (NamingEnumeration e = attr.getAll();
e.hasMore();
                                                out.println("value: " +
e.next()))
                        ;
                        }                       
                        
                        // Close the context when we're done
                        ctx.close();
                } 
                catch (NamingException e) 
                {
                        System.out.println("Connection failed!");
                        e.printStackTrace();
                }
                
                out.println("</pre>");
                out.println("</BODY></HTML>");
    }
}



--
--------------------------------------------------------------
Please read the FAQ! <http://java.apache.org/faq/>
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Archives and Other:  <http://java.apache.org/main/mail.html>
Problems?:           [EMAIL PROTECTED]

Reply via email to