Ich muss LDAP-Authentifizierung für eine Anwendung durchführen.LDAP-Authentifizierung mit Java
Ich habe versucht, das folgende Programm:
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;
}
}
folgende Ausnahme Anfahrt:
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)
paar Punkte zu beachten:
Früher war ich mit
tomcat 5.3.5
aber jemand sagte mir, dass nur Kater 6 sup portiert es so, dass ichtomcat 6.0.35
heruntergeladen und derzeit nur diese Version verwendet.konfiguriert
server.xml
und fügte den folgenden Code -
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99" connectionURL="ldap://server.domain.com:389/"
userPattern="{0}" />
kommentiert den folgenden Code aus
server.xml
-<!-- Commenting for LDAP <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> -->
Schritte 2 und 3 von article
Jemand schlug vor, dass es einige JAR-Dateien, die auf Tomcat, um kopiert werden sollen
ldap
Authentifizierung auszuführen, ist, dass etwas, was ich tun muss? Und welchejar
Dateien?Auch verwende ich die richtigen Anmeldeinformationen sicher, dann, was verursacht dieses Problem?
Gibt es eine Möglichkeit, die richtigen Attribute für LDAP herauszufinden, wenn ich falsche verwende?
Es gibt bessere Bibliotheken für diese, aber hier ist eine ähnliche Frage http://stackoverflow.com/a/12165647/1286621 ich mit @jasim um die Haupt zustimmen. Sie müssen herausfinden, welches Format Ihr LDAP-Server verwendet. Hier ist ein Beispiel für meinen Active Directory Server "CN = bindUserName, CN = Benutzer, DC = meineDepartment, DC = myNetwork". Die LDAP-Leute sollten ziemlich schnell in der Lage sein, Ihnen zu sagen, was das Format ist. Es gibt auch GUI-Tools, die sich mit LDAP verbinden und die Verzeichnisse durchsuchen können. Sprich jedoch zuerst mit deinen Admins. – Mike
Nur noch ein weiterer Kommentar, Sie wissen, dass es normalerweise einen "Bind" Benutzer/Passwort gibt, der berechtigt ist, Informationen im LDAP Server nachzuschlagen? Sobald Sie an den Server gebunden haben, können Sie die Benutzeranmeldeinformationen authentifizieren. – Mike