2013-09-16 99 views
2

我是一名大学生。现在,我正在执行一个项目,该项目必须使用LDAP连接在登录过程中验证用户的用户名和密码。所以,我的网站是使用JSP开发的。我试图解决代码的错误,但我不能。我是否犯了一些错误?javax.naming.InvalidNameException:[LDAP:error code 34 - invalid DN]

<%@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:尝试{32://连接以ldap 33:新的 InitialLdapContext(env,null); 34:35://连接 成功36:System.out.println(“Connection succeeded!”);

堆栈跟踪: 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 原因

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) 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 原因

javax.naming.InvalidNameException:[LDAP:错误代码34 - 无效的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 (HttpServlet.java:722) org.apache.jasper.servlet.JspServletWrapper。服务(JspServletWrapper.java:432) org.apache.jasper.servlet.Js pServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)note备注 full堆栈跟踪的根本原因可在Apache Tomcat/7.0.27日志中找到。

的Apache Tomcat/7.0.27

+0

如果你在Mac上,请确保你的build.xml文件中没有任何'"' – Archmede

回答

4

这是重要的一行: 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"); 
+0

我已经尝试过,但它不起作用。感谢您的回答。 :D – Best

+0

在代码中,您看起来像是收到电子邮件并将其作为SECURITY_PRINCIPAL传入。看看我上面发布的代码的格式。字符串'S.用户'是用户名。它是一个组织单位和一个组织内的领域。如果您不熟悉我在谈论的内容,则应该查看LDAP结构:http://en.wikipedia.org/wiki/Distinguished_Name#Directory_structure – dsingleton

0

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