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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18832031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:28:40  来源:igfitidea点击:

javax.naming.InvalidNameException: [LDAP: error code 34 - invalid DN]

javaldap

提问by Best

I'm a college student. Now, i'm doing a project that must use LDAP connection to authenticate the username and password of the user in log in process. So, my website is develop by use JSP. I try to solve the error of code but i can't. Did I makes some mistake on it?

我是一名大学生。现在,我正在做一个项目,必须使用 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>

and I got this error msg from running the code.

我从运行代码中得到了这个错误消息。

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /ldap_checking.jsp at line 33

30: 31: try { 32: //Connect with ldap 33: new InitialLdapContext(env, null); 34: 35: //Connection succeeded 36: System.out.println("Connection succeeded!");

Stacktrace: 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) root cause

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) root cause

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) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.

Apache Tomcat/7.0.27

HTTP 状态 500 -

输入异常报告

信息

说明 服务器遇到内部错误 (),阻止它完成此请求。

例外

org.apache.jasper.JasperException:在第 33 行处理 JSP 页面 /ldap_checking.jsp 时发生异常

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: 错误代码 34 - 无效 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)Httpx.servlet.http.Servlet服务(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: 错误代码 34 - 无效 DN] com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:3028) com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:第 2835 章.java:193) com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:211) com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:154.japxLds.com. .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 .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) note Apache Tomcat/7.0.27 日志中提供了根本原因的完整堆栈跟踪。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) note 根本原因的完整堆栈跟踪可在Apache Tomcat/7.0.27 日志。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) note 根本原因的完整堆栈跟踪可在Apache Tomcat/7.0.27 日志。722) 注意 Apache Tomcat/7.0.27 日志中提供了根本原因的完整堆栈跟踪。722) 注意 Apache Tomcat/7.0.27 日志中提供了根本原因的完整堆栈跟踪。

Apache Tomcat/7.0.27

回答by dsingleton

This is the important line: javax.naming.InvalidNameException: [LDAP: error code 34 - invalid DN]

这是重要的一行:javax.naming.InvalidNameException: [LDAP: error code 34 - invalid DN]

You can look here: https://wiki.servicenow.com/index.php?title=LDAP_Error_Codes

您可以在这里查看:https: //wiki.servicenow.com/index.php?title=LDAP_Error_Codes

for what 34 means, but it looks like the distinguished name you are trying to use is incorrect. It looks like your principal could be formatted incorrectly. See this guide from oracle on doing ldap authentication: http://docs.oracle.com/javase/jndi/tutorial/ldap/security/ldap.html

34 意味着什么,但看起来您尝试使用的专有名称不正确。看起来您的主体格式可能不正确。在进行 ldap 身份验证时,请参阅 oracle 的本指南:http: //docs.oracle.com/javase/jndi/tutorial/ldap/security/ldap.html

pay special attention to this part of it where they set up the environment entries:

特别注意他们设置环境条目的这部分:

env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
env.put(Context.SECURITY_CREDENTIALS, "mysecret");

回答by Andrew

The "javax.naming.InvalidNameException: [LDAP: error code 34 - invalid DN] " in your stack trace is the key. Your LDAP server doesn't like the value you are sending it. I would suggest fully qualifying the user name, something like cn=username, ou=some_container, o=mycompany. The actual syntax will be driven by your LDAP server.

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