JNDI - LDAP Authentication

1k views Asked by At

I'm trying to authenticate to LDAP via JSP-JNDI on Tomcat 7. I'm using maven to organize and compile, and here's the code of the .jsp file:

<html>
<body>
<h2>Bind</h2>
<%
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:1389");
DirContext ctx = new InitialDirContext(env);

env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL,"cn=Manager");
env.put(Context.SECURITY_CREDENTIALS,"pwd");
DirContext ctx = new InitialDirContext(env);
%>
</body>
</html>

When I connect to localhost:8080/AddressBook/ the server says that every variable "cannot be resolved to a variable": Hashtable,Context.PROVIDER_URL,DirContext,InitialDirContext etc. I can immagine it could be a simple error. Do I have to import some class? If I have to where i have to specify it?

1

There are 1 answers

1
MdC On

I reached this point now, this is my JSP file.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.*" %>
<%@page import="javax.naming.directory.*" %>
<%@page import="javax.naming.ldap.*" %>
<%@page import="javax.naming.ldap.*" %>
<%@page import="javax.naming.Context" %>
<%@page import="javax.naming.directory.*"%>
<%@page import="javax.naming.directory.InitialDirContext"%> 
<%@page import="javax.naming.directory.DirContext"%>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Hello World!!!</h1>
    <%
    //connect to ldap server

        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://localhost:1389/dc=example,dc=com");
        DirContext ctx = new InitialDirContext(env);

    %>
</body>
</html>

It looks like it works. My second step is to listen all the entries I have on my LDAP directory. (LDAP is full and completely functional on my machine) Any advice ? Should I use a main or a class ?