Search and add a ldap entry using JSP/JNDI -
i'm trying add ldap entry jsp/jndi. code rough, i'm learning, if have advice please tell me. search section works fine. addentry section doesn't. tells me :
" exception occurred: [ldap: error code 50 - entry cn=m,o=rubrica,dc=example,dc=com cannot added due insufficient access rights] "
this code:
<%@page import="javax.naming.namingenumeration"%> <%@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.ldap.*" %> <%@page import="javax.naming.directory.*"%> <%@page import="javax.naming.directory.initialdircontext"%> <%@page import="javax.naming.directory.dircontext"%> <%@page import="javax.naming.context" %> <%@page import="javax.naming.initialcontext" %> <%@page import="javax.naming.namingexception" %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>jsp page</title> </head> <body> <h2>rubrica</h2> <!-- search entry --> <br> <h3>search:</h3> <form action="" method="post"> search entry: <input type="text" name="search""><br> <input type="submit" value="search"> </form> <br><br> <% //creating initial context search function: //context = objects state set of bindings (=ldap entries), have distinct atomic names. //the hashtable class represents environments properties parameters hashtable env = new hashtable(); env.put(context.initial_context_factory,"com.sun.jndi.ldap.ldapctxfactory"); env.put(context.provider_url, "ldap://localhost:1389/o=rubrica,dc=example,dc=com"); dircontext ctx = new initialdircontext(env); env.put(context.security_principal,"cn=directory manager,dc=example,dc=com"); env.put(context.security_credentials,"secret"); string searchname = (string)request.getparameter("search"); try{ request.getparameter("search"); attributes attrs = ctx.getattributes("cn = " + searchname); out.println(attrs.get("cn").get()+": "); out.println(attrs.get("telephonenumber").get()); } catch (exception e){ out.println("an exception occurred: " + e.getmessage()); } %> <br><br>------------------------------------</br><br> <!-- add entry --> <br> <h3>add entry:</h3> <form action="" method="post"> add entry:<br><br> full name: <input type="text" name="addcn"><br> surname: <input type="text" name="surname"><br> phonenumber: <input type="text" name="pn"><br> <input type="submit" value="addentry"> </form> <br><br> <% string addcn = (string)request.getparameter("addcn"); string surname = (string)request.getparameter("surname"); string pn = (string)request.getparameter("pn"); try{ //create new set of attributes basicattributes attrs1 = new basicattributes(); //(the item person) attribute classes = new basicattribute("objectclass"); classes.add("top"); classes.add("person"); // classes.add("organizationalperson"); // add objectclass attribute attribute set attrs1.put(classes); // store other attributes in attribute set attrs1.put("sn", surname); attrs1.put("telephonenumber", pn); // add new entry directory server ctx.createsubcontext("ldap://localhost:1389/cn="+addcn+",o=rubrica,dc=example,dc=com", attrs1); } catch (exception e){ out.println("an exception occurred: " + e.getmessage()); } %> </body>
i added "remove entry" part:
<h3>remove entry:</h3> <form method="post"> insert entry remove: <input type="text" name="deluser""><br> <input type="submit" value="remove"> </form><br><br> <% string deluser = (string)request.getparameter("deluser"); try { ctx.destroysubcontext("cn="+deluser); } catch (exception e){ out.println("an exception occurred: " + e.getmessage()); } %>
and page gives me same authentication error. ps. i'm using ldap + sasl on machine. maybe problem.
[solved] problem order of instruction creation of context incorrect. in code above doing anonymous authetication. follows correct flow of operation:
env.put(context.initial_context_factory,"com.sun.jndi.ldap.ldapctxfactory"); env.put(context.provider_url, "ldap://localhost:1389/o=rubrica,dc=example,dc=com"); env.put(context.security_authentication, "simple"); env.put(context.security_principal,"cn=directory manager"); env.put(context.security_credentials,"secret"); dircontext ctx = new initialdircontext(env);
from error seems don't have access rights add entry in tree hierarchy trying add entry. 1 of these tasks:
- use admin user add entry.
- try add entry in hierarchy have rights add entry.
- grant rights user using add entry.
Comments
Post a Comment