我需要爲應用程序執行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