javax.naming.InvalidNameException: [LDAP: 错误代码 34 - 无效的 DN]

4

我是一名大学生。现在,我正在做一个项目,必须使用LDAP连接验证登录过程中用户的用户名和密码。因此,我的网站使用JSP进行开发。我尝试解决代码错误,但我无法解决。我是否犯了一些错误?

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="java.util.regex.*" %>
<%@ page import="javax.naming.directory.*" %>
<%@ page import="java.util.Hashtable.*" %>
<%@ page import="javax.naming.ldap.*" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            String username = request.getParameter("email");
String password = request.getParameter("password");
            Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://ldap-pj.sit.kmutt.ac.th");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);

try {
            //Connect with ldap
            new InitialLdapContext(env, null);  

            //Connection succeeded
            System.out.println("Connection succeeded!");
        } catch (AuthenticationException e) {

            //Connection failed
            System.out.println("Connection failed!");
            e.printStackTrace();
        }  
%>
    </body>
</html>

我运行代码时遇到了这个错误信息。
HTTP状态500 -
类型 异常报告
消息
描述 服务器遇到内部错误(),阻止其满足此请求。
异常
org.apache.jasper.JasperException:处理JSP页面/ldap_checking.jsp时出现异常,位于第33行
30:31:try {32://与ldap连接33:new InitialLdapContext(env,null);34:35://连接成功36:System.out.println(“连接成功!”);
堆栈跟踪:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 根本原因
javax.servlet.ServletException: javax.naming.InvalidNameException: [LDAP:error code 34 - invalid DN] org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840) org.apache.jsp.ldap_005fchecking_jsp._jspService(ldap_005fchecking_jsp.java:212) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 根本原因
javax.naming.InvalidNameException:[LDAP:error code 34 - invalid DN] com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:3028) com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2835) com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2749) com.sun.jndi.ldap.LdapCtx.(LdapCtx.java:316) com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:193) com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:211) com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:154) com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:84) javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684) javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307) javax.naming.InitialContext.init(InitialContext.java:242) javax.naming.ldap.InitialLdapContext.(InitialLdapContext.java:153) org.apache.jsp.ldap_005fchecking_jsp._jspService(ldap_005fchecking_jsp.java:97) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 注意根本原因的完整堆栈跟踪在Apache Tomcat/7.0.27日志中可用。
Apache Tomcat/7.0.27

如果您使用的是 Mac,请确保您的 build.xml 文件中没有任何 &quot; - Archmede
2个回答

7
这是重要的一行: javax.naming.InvalidNameException: [LDAP: 错误代码 34 - 无效的 DN]
您可以在这里查看: https://wiki.servicenow.com/index.php?title=LDAP_Error_Codes 了解34的含义,但看起来您正在尝试使用的可分辨名称不正确。 您的主体可能格式不正确。 请参阅Oracle关于执行LDAP身份验证的指南: http://docs.oracle.com/javase/jndi/tutorial/ldap/security/ldap.html 特别注意其中设置环境条目的部分。
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
env.put(Context.SECURITY_CREDENTIALS, "mysecret");

我之前尝试过,但是它没有起作用。谢谢你的回答。:D - Best
在代码中,看起来你正在获取电子邮件并将其作为SECURITY_PRINCIPAL传递。请查看我上面发布的代码格式。字符串“S. User”是用户名。它在组织单位和组织内部进行域间隔。如果您不熟悉我所说的内容,您应该查看LDAP结构:http://en.wikipedia.org/wiki/Distinguished_Name#Directory_structure - dsingleton
第一个链接已经失效,现在在这里:https://ldapwiki.com/wiki/LDAP%20Result%20Codes - 不幸的是,编辑队列已满。 - dessert

2

你堆栈跟踪中的 "javax.naming.InvalidNameException: [LDAP: error code 34 - invalid DN] " 是关键。 你的LDAP服务器不喜欢你发送的值。我建议完全限定用户名,例如 cn=username, ou=some_container, o=mycompany。实际的语法将由您的LDAP服务器驱动。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接