Necesito saber como autenticarse en el dominio con java. Con un servidor de Dominio Windows Server 2003.
Si me dicen una herramienta free que lo haga tambien me serviría de gran ayuda.
De todas formas para que lo sepan, intenté con este código que les pongo a continuacion pero me da este error:
Código PHP:
Naming exception javax.naming.CommunicationException: dominio.com:389 [Root exception is java.net.ConnectException: Connection timed out: connect]
Exception in thread "main" java.lang.NullPointerException
at cu.co.cenatav.utils.ldapfastbind.Authenticate(ldapfastbind.java:53)
at cu.co.cenatav.utils.fastbindclient.main(fastbindclient.java:19)
Clase ldapfastbind
Código PHP:
/**
* ldapfastbind.java
*
* Sample JNDI application to use Active Directory LDAP_SERVER_FAST_BIND connection control
*
*/
import java.util.Hashtable;
import javax.naming.*;
import javax.naming.ldap.*;
import javax.naming.directory.*;
class FastBindConnectionControl implements Control {
public byte[] getEncodedValue() {
return null;
}
public String getID() {
return "1.2.840.113556.1.4.1781";
}
public boolean isCritical() {
return true;
}
}
public class ldapfastbind {
public Hashtable env = null;
public LdapContext ctx = null;
public Control[] connCtls = null;
public ldapfastbind(String ldapurl) {
env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.PROVIDER_URL,ldapurl);
connCtls = new Control[] {new FastBindConnectionControl()};
//first time we initialize the context, no credentials are supplied
//therefore it is an anonymous bind.
try {
ctx = new InitialLdapContext(env,connCtls);
}
catch (NamingException e) {
System.out.println("Naming exception " + e);
}
}
public boolean Authenticate(String username, String password) {
try {
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL,username);
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS,password);
ctx.reconnect(connCtls);
System.out.println(username + " is authenticated");
return true;
}
catch (AuthenticationException e) {
System.out.println(username + " is not authenticated");
return false;
}
catch (NamingException e) {
System.out.println(username + " is not authenticated");
return false;
}
}
public void finito() {
try {
ctx.close();
System.out.println("Context is closed");
}
catch (NamingException e) {
System.out.println("Context close failure " + e);
}
}
}
Código PHP:
/**
* fastbindclient.java
*
* Sample JNDI application to use LDAP_SERVER_FAST_BIND connection control
*
* This is just a test harness to invoke the ldapfastbind methods
*/
class fastbindclient{
public static void main (String[] args) {
//Could also use ldaps over port 636 to protect the communication to the
//Active Directory domain controller. Would also need to add
//env.put(Context.SECURITY_PROTOCOL,"ssl") to the "server" code
String ldapurl = "ldap://dominio.com:389";
boolean IsAuthenticated = false;
ldapfastbind ctx = new ldapfastbind(ldapurl);
IsAuthenticated = ctx.Authenticate("DOMINIO-DS\\usuario","password");
ctx.finito();
}
}
Salu2