可以使用java示例向我发送LDAP服务器身份验证的示例

时间:2014-02-03 05:26:17

标签: java ldap

用于apache DS, OpenLDAP的, 活动目录 或任何其他服务器。

以下代码我已经使用过。 我已经通过提交login.jsp来调用此服务进行身份验证。

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    final String SUCCESS = "Success.html";
    final String FAILURE = "Failure.html";
    String strUrl = "login.html";
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    Hashtable env = new Hashtable(11);

    boolean b = false;

    env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:10389");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "uid="+ username +",ou=system");
    env.put(Context.SECURITY_CREDENTIALS, password);

    try {
    // Create initial context
    DirContext ctx = new InitialDirContext(env);

    // Close the context when we're done
    b = true;
    ctx.close();

    } catch (NamingException e) {
    b = false;
    }finally{
    if(b){
    System.out.print("Success");
    strUrl = SUCCESS;
    }else{
    System.out.print("Failure");
    strUrl = FAILURE;
    }
    }
    RequestDispatcher rd = request.getRequestDispatcher(strUrl);
    rd.forward(request, response);

    }

任何人都可以帮我配置ldap服务器。

1 个答案:

答案 0 :(得分:1)

我们有several JNDI Examples。 -Jim