简体   繁体   中英

LDAP + Java Aplication Authentication error code 49 - 52e

I have a problem with my code. I want to do the authentication for one application and I have the same problem ever.

I have the URL of the ldap server. And the username and password of the users in LDAP. Now I show us the Class that I use:

public final class ldapAuth {

    private String usuario;
    private String clave;
    private String servidor;
    private String dn;
    private String tipoAuth;
    private boolean autenticado;

    DirContext dc;

    /**
     * Constructor de la conexion con el Motor de LDAP
     *
     * @param server  Servidor en donde se encuentra el LDAP
     * @param dn      Directoria del arbol del LDAP
     * @param ta      Tipo de Autenticacion
     * @param usuario Usuario que desea realizar la conexion
     * @param clave   Clave del usuario
     *
     */
    public ldapAuth(String server, String dn, String ta,String usuario,String clave) {
        this.servidor = server;
        this.dn = dn;
        this.tipoAuth = ta;
        this.usuario=usuario;
        this.clave=clave;
        inicializarConexion();
    }

    public void inicializarConexion() {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, servidor);
        env.put(Context.SECURITY_AUTHENTICATION, tipoAuth);
        env.put(Context.SECURITY_PRINCIPAL, dn);
        env.put(Context.SECURITY_CREDENTIALS, clave);

        try {
            dc = new InitialDirContext(env);
            setAutenticado(true);
        } catch (NamingException ex) {
            System.out.println("Error Autenticando mediante LDAP, Error causado por : " + ex.toString());
            setAutenticado(false);
        }
    }

    /**
     * Retorna el Atributo de la conexion con LDAP actual
     * 
     * @param atributo Nombre del Atributo que se desea obtener
     * @return Attribute con la informacion correspondiente
     */

    public Attribute cargarPropiedadConexion(String atributo) {
        Attribute propiedad = null;

        try {
            Attributes attrs = dc.getAttributes(dn);

            if (attrs == null) {
                propiedad = null;
            } else {
                propiedad = attrs.get(atributo);
            }
        } catch (Exception e) {
            propiedad = null;
        }
        return propiedad;
    }


    /*Get's y Set's*/
    public boolean isAutenticado() {
        return autenticado;
    }
    public void setAutenticado(boolean autenticado) {
        this.autenticado = autenticado;
    }
    public String getUsuario() {
        return usuario;
    }
    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }
}

Now I show us the main code that call this functions:

import javax.naming.NamingException;
import javax.naming.directory.Attribute;


public class main {

    public static void main(String[] args) throws NamingException{
        System.out.println("Iniciando Autenticacion");

        String server="ldap://10.201.69.xxx"; // servidor de LDAP
        String usuario="CN =user, OU=Generic Users,OU=Ofimatica, OU=User Organizations, DC=mycompany, DC=vwg; // Usuario de Autenticacion
        String dn="ou=users,ou=ofimatica,ou=user organizations,dc=mycompany,dc=vwg"; // Ruta del Arbol LDAP
        String tipoAth="simple";//tipo de autentuicacion simple o por SSL
        String clave="mypassword";

        ldapAuth ldapAuth=new ldapAuth(server,dn,tipoAth,usuario,clave);

        if(ldapAuth.isAutenticado()){
            System.out.println("Usuario "+ldapAuth.getUsuario()+" Autenticado Correctamente");


        }
        else{
            System.out.println("Usuario "+ldapAuth.getUsuario()+" No se Puedo Autenticar");
        }
    }
}

When I execute I see the error code 49 data 52e, I google it and I see that it by invalid credentials so I don't understand how to solve this.

Later I make some changes and if I quit the User and Password the error doesn't show it.

Thanks :)

Your not providing the correct credentials for the binding account. Your DC does not allow anonymous LDAP look ups. So you need to provide credentials to pass the request. we had to provide something like domainShortName\\administrator password etc. Or you could configure your DC to allow anonymous LDAP binding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM