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

标签 java ldap

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

最佳答案

这是重要的一行: javax.naming.InvalidNameException: [LDAP: 错误代码 34 - 无效的 DN]

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

对于 34 的含义,但看起来您尝试使用的专有名称不正确。看起来您的委托(delegate)人格式可能不正确。请参阅 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");

关于javax.命名.InvalidNameException : [LDAP: error code 34 - invalid DN],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18832031/

相关文章:

java - 具有继承和泛型的流畅 API

java - 无法使用不同的用户 ID 登录我的 Facebook 与 Android 应用程序集成

Python通过包含列表的字典列表循环创建新字典的最佳方法

PHP LDAP 自动验证

Spring Security & Active Directory LDAP 错误 32 问题 2001 (NO_OBJECT)

java - 从 HttpServletRequest 获取当前使用的协议(protocol)名称?

java - 将固定数据存储在 JSON 而不是 SQLite 中是否好

java - 当 Spring Boot Test WebEnvironment 设置为 RANDOM_PORT 时如何为 Ribbon 配置服务器列表

php - 使用 PHP 创建 LDAP 服务器

php ldap-bind 密码过期