我需要为应用程序执行LDAP身份验证。使用Java的LDAP身份验证
我尝试了以下程序:
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
public class LdapContextCreation {
public static void main(String[] args) {
LdapContextCreation ldapContxCrtn = new LdapContextCreation();
LdapContext ctx = ldapContxCrtn.getLdapContext();
}
public LdapContext getLdapContext(){
LdapContext ctx = null;
try{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "Simple");
//it can be <domain\\userid> something that you use for windows login
//it can also be
env.put(Context.SECURITY_PRINCIPAL, "[email protected]");
env.put(Context.SECURITY_CREDENTIALS, "password");
//in following property we specify ldap protocol and connection url.
//generally the port is 389
env.put(Context.PROVIDER_URL, "ldap://server.domain.com");
ctx = new InitialLdapContext(env, null);
System.out.println("Connection Successful.");
}catch(NamingException nex){
System.out.println("LDAP Connection: FAILED");
nex.printStackTrace();
}
return ctx;
}
}
获得以下异常:
LDAP Connection: FAILED javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials] at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3053) at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2999) at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2801) at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2715) at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:305) at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:187) at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:205) at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:148) at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:78) at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:235) at javax.naming.InitialContext.initializeDefaultInitCtx(InitialContext.java:318) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:348) at javax.naming.InitialContext.internalInit(InitialContext.java:286) at javax.naming.InitialContext.init(InitialContext.java:308) at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:99) at LdapContextCreation.getLdapContext(LdapContextCreation.java:27) at LdapContextCreation.main(LdapContextCreation.java:12)
数点考虑:
早些时候,我使用
tomcat 5.3.5
但有人告诉我只有tomcat 6 sup端口它,所以我下载了tomcat 6.0.35
,目前只使用这个版本。配置
server.xml
并添加以下代码 -
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99" connectionURL="ldap://server.domain.com:389/"
userPattern="{0}" />
评论从
server.xml
以下代码 -<!-- Commenting for LDAP <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> -->
步骤2和步骤3从article
有人建议有一些jar文件应该被复制到tomcat以运行
ldap
身份验证,那我需要做些什么?哪个jar
文件?此外,我使用正确的凭据肯定,那么是什么原因造成这个问题?
有没有一种方法,我可以找出LDAP的正确属性,以防我使用不正确的属性?
这有更好的库,但这里是一个类似的问题http://stackoverflow.com/a/12165647/1286621我@jasim同意有关负责人。你需要弄清楚你的LDAP服务器使用什么格式。以下是我的Active Directory服务器“CN = bindUserName,CN = Users,DC = myDepartment,DC = myNetwork”的示例。 LDAP人员应该很快就能告诉你格式是什么。还有一些gui工具可以连接到LDAP并浏览目录。首先与你的管理员交谈。 – Mike
还有一点意见,你知道通常有一个“绑定”用户/密码,一个有权在LDAP服务器中查找信息的权限正确吗?一旦绑定到服务器,您就可以验证用户凭证。 – Mike