问题描述
我是一名大学生。现在,我正在做一个必须使用LDAP连接来验证登录过程中用户的用户名和密码的项目。所以,我的网站是用JSP开发的。我试图解决代码的错误,但我不能。我是否犯了一些错误?
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?
<%@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.
类型异常报告
消息
描述服务器遇到内部错误(),阻止
来完成此请求。
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
exception
org.apache.jasper.JasperException:在第33行处理
JSP页面/ldap_checking.jsp时发生异常
org.apache.jasper.JasperException: An exception occurred processing JSP page /ldap_checking.jsp at line 33
30:31:尝试{32://用ldap 33连接:new
InitialLdapContext(env,null); 34:35://连接
成功36:System.out.println(连接成功!);
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
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:错误代码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
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:错误代码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: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(HttpServ let.java:722)注意根据原因的
完整堆栈跟踪在Apache
Tomcat / 7.0.27日志中可用。
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
Apache Tomcat/7.0.27
推荐答案
这是重要的一行:
javax .naming.InvalidNameException:[LDAP:错误代码34 - 无效DN]
This is the important line: javax.naming.InvalidNameException: [LDAP: error code 34 - invalid DN]
您可以在这里查看:
代表34的含义,但看起来你尝试使用的专有名称是不正确的。看起来你的校长可能格式不正确。请参阅oracle关于ldap身份验证的指南:
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
特别注意这部分地方他们设置了环境条目:
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");
这篇关于javax.naming.InvalidNameException:[LDAP:错误代码34 - 无效的DN]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!