javax.naming.InvalidNameException:

我是一名大学生。 现在,我正在做一个必须使用LDAP连接来validation登录过程中用户的用户名和密码的项目。 所以,我的网站是用JSP开发的。 我试图解决代码的错误,但我不能。 我犯了一些错误吗?

           JSP Page   <% String username = request.getParameter("email"); String password = request.getParameter("password"); Hashtable env = new Hashtable(); 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(); } %>   

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

HTTP状态500 –

类型exception报告

信息

description服务器遇到内部错误(),导致无法完成此请求。

例外

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

30:31:尝试{32://使用ldap 33连接:新的InitialLdapContext(env,null); 34:35://连接成功36:System.out.println(“连接成功!”);

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

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

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:97)org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java: 7 0)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

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

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

对于34意味着什么,但看起来你尝试使用的专有名称是不正确的。 看起来你的校长可能格式不正确。 在执行ldap身份validation时请参阅oracle的本指南: 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.naming.InvalidNameException:[LDAP:错误代码34 – 无效DN]”是关键。 您的LDAP服务器不喜欢您发送它的值。 我建议完全限定用户名,例如cn = username,ou = some_container,o = mycompany。 实际语法将由LDAP服务器驱动。