使用JSP / JNDI搜索并添加ldap条目

时间:2015-06-10 13:15:53

标签: jsp ldap jndi

我正在尝试使用jsp / jndi添加ldap条目。代码非常粗糙,我正在学习,所以如果你有什么建议请告诉我。 SEARCH部分工作正常。 ADDENTRY部分没有。它告诉我:

route()

这是我的代码:

@Test
public void testResetValueWithFakeRequest() {
    Call call = controllers.routes.Application.resetValue(1);
    ImmutableMap<String, String> formData = ImmutableMap.of("username", "Jakob");
    RequestBuilder request = fakeRequest(call).bodyForm(formData);
    Result result = route(request);
    assertEquals(OK, result.status());
}

我添加了“删除条目”部分:

" An exception occurred: [LDAP: error code 50 - The entry cn=m,o=Rubrica,dc=example,dc=com cannot be added due to insufficient access rights] "

该页面给出了相同的身份验证错误。 PS。我在我的机器上使用ldap + SASL。也许这可能是问题。

[解决] 问题是关于创建上下文的指令顺序不正确。在上面的代码中,我正在进行匿名验证。 遵循正确的操作流程:

<%@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 AN INITIAL CONTEXT for search function:
    //context = objects whose state is a set of bindings (=ldap entries), that have distinct atomic names. 
    //The Hashtable class represents the 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 is a person)
        Attribute classes = new BasicAttribute("objectClass");
        classes.add("top");
        classes.add("person");
//   classes.add("organizationalPerson");
        // Add the objectClass attribute to the attribute set
           attrs1.put(classes);
        // Store the other attributes in the attribute set
           attrs1.put("sn", surname);
           attrs1.put("telephonenumber", pn);
        // Add the new entry to the 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>

1 个答案:

答案 0 :(得分:0)

根据您的错误,您似乎没有访问权限来在您尝试添加条目的树层次结构中添加条目。执行以下任务之一:

  1. 使用admin用户添加条目。
  2. 尝试在层次结构中添加您有权添加条目的条目。
  3. 授予您用于添加条目的用户的权限。