Visualizzazione dei risultati da 1 a 2 su 2

Discussione: [java] Ldap

  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2005
    Messaggi
    47

    [java] Ldap

    Ciao a tutti,
    ho bisogno di aiuto poichè sono un niubbo su questo tipo di programmazione.

    praticamente devo creare un'applicazione web la cui connessione si basa su LDAP ma non so da dove cominciare..

    ho installato il server LDAP (openDS).
    chi può darmi un aiuto per iniziare?

    grazie
    ciao
    ..::AXIS RuLez::..

  2. #2
    Ciao,
    questo codice che trovi ti puo' essere utile, l'avevo fatto per interfacciami con LDAP per inserire e leggere le informazioni utente.

    codice:
    package spektra.system.ldap;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import spektra.pom.model.Utenti;
    
    import javax.naming.Context;
    import javax.naming.NamingEnumeration;
    import javax.naming.NamingException;
    import javax.naming.directory.*;
    import java.util.Date;
    import java.util.Hashtable;
    import java.util.ResourceBundle;
    
    public class LDAPCommand {
        private ResourceBundle rbConfig = ResourceBundle.getBundle("config");
        private Log log = LogFactory.getLog(LDAPCommand.class);
        private DirContext ctx;
    
        public LDAPCommand() {
            boolean isSSL = Boolean.parseBoolean(rbConfig.getString("spektra.ldap.secure"));
            String ldapHostname = rbConfig.getString("spektra.ldap.host");
            int ldapPort = Integer.parseInt(rbConfig.getString("spektra.ldap.port"));
            String ldapDn = rbConfig.getString("spektra.ldap.dn");
            String ldapPwd = rbConfig.getString("spektra.ldap.pwd");
    
            String ldapUri = "";
    
            if (isSSL)
                ldapUri += "ldaps://";
            else
                ldapUri += "ldap://";
    
            ldapUri += ldapHostname;
            ldapUri += ":";
    
            if (ldapPort > 0)
                ldapUri += "" + ldapPort;
    
            log.debug("Ldap uri:" + ldapUri);
    
            log.info("Init context");
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, ldapUri);
    
            if (isSSL)
                env.put(Context.SECURITY_AUTHENTICATION, "ssl");
            else
                env.put(Context.SECURITY_AUTHENTICATION, "simple");
    
            env.put(Context.SECURITY_PRINCIPAL, ldapDn);
            env.put(Context.SECURITY_CREDENTIALS, ldapPwd);
    
            try {
                ctx = new InitialDirContext(env);
            } catch (NamingException e) {
                log.error("Errore inizializzazione contesto", e);
            }
            log.info("Context created successfully");
        }
    
        public boolean aggiungiUtente(Utenti u) {
            log.info("Add new user to LDAP");
    
            SearchControls ctls = new SearchControls();
            ctls. setReturningObjFlag(true);
            ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            String filter = "(uidNumber=*)";
            String tree = "ou=" + rbConfig.getString("spektra.users.ldap.ou") + "," + rbConfig.getString("spektra.ldap.suffix");
            NamingEnumeration answer = null;
            int newUid = 1000;
            try {
                log.debug("Find next user uid");
                answer = ctx.search(tree, filter, ctls);
    
                while (answer.hasMore()) {
    
                    SearchResult sr = (SearchResult) answer.next();
                    NamingEnumeration attrs = sr.getAttributes().getAll();
    
                    while (attrs.hasMore()) {
                        Attribute a = (Attribute) attrs.next();
                        if (a.getID().equalsIgnoreCase("uidNumber")) {
    
                            int temp = 0;
                            try {
                                String s = (String) a.getAll().next();
    
                                temp = Integer.parseInt(s);
                            } catch (NumberFormatException nfe) {
                                temp = 1000;
                            }
                            if (temp >= newUid)
                                newUid = temp;
    
    
                        }
                    }
                }
            } catch (NamingException e) {
                log.error("Unable to find next uid add user aborted", e);
                return false;
            }
            newUid++;
    
            log.debug("Start buid entry");
    
    
            Attributes attrs = new BasicAttributes(true);
            Attribute objectClass = new BasicAttribute("objectClass");
            Attribute ou = new BasicAttribute("ou");
    
    
            objectClass.add("posixAccount");
            objectClass.add("account");
            objectClass.add("shadowAccount");
            objectClass.add("top");
            ou.add("People");
    
            attrs.put(objectClass);
            attrs.put(ou);
            String cn = u.getNome().trim() + " " + u.getCognome().trim();
            attrs.put("uid", u.getUsername().trim());
            attrs.put("cn", cn.trim());
            attrs.put("homeDirectory", rbConfig.getString("spektra.users.home.prefix") + u.getUsername().trim());
            attrs.put("loginShell", rbConfig.getString("spektra.users.shell"));
            attrs.put("shadowLastChange", Long.toString(daysSince1970()));
            attrs.put("shadowWarning", rbConfig.getString("spektra.users.shadowWarning"));
            attrs.put("shadowMax", rbConfig.getString("spektra.users.shadowMax"));
            attrs.put("gidNumber", rbConfig.getString("spektra.users.group.gid"));
            attrs.put("uidNumber", "" + newUid);
    
            attrs.put("userPassword", "" + u.getPasswd().trim());
    
            System.out.println("--->" + attrs.toString());
            try {
                String newCtx = "uid=" + u.getUsername().trim() + ",ou=" + rbConfig.getString("spektra.users.ldap.ou") + "," + rbConfig.getString("spektra.ldap.suffix");
                ctx.createSubcontext(newCtx, attrs);
    
    
            } catch (NamingException e) {
                log.error("Unable to add user", e);
                return false;
    
            }
            log.info("User addedd");
            return true;
    
    
        }
    
        public boolean modificaPassword(String username, String oldPasswd, String newPasswd) {
            String[] attrs = new String[1];
            attrs[0] = "userPassword";
    
            String tree = "uid=" + username + ",ou=" + rbConfig.getString("spektra.users.ldap.ou") + "," + rbConfig.getString("spektra.ldap.suffix");
    
            try {
                Attributes resultAttrs = ctx.getAttributes(tree, attrs);
    
                if (resultAttrs == null) {
                    return false;
                }
    
    
                ModificationItem[] mod = new ModificationItem[2];
                Attribute newpwd = new BasicAttribute("userPassword");
                newpwd.add(newPasswd);
    
                Attribute newshadow = new BasicAttribute("shadowLastChange");
                newshadow.add("" + daysSince1970());
    
                mod[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, newpwd);
                mod[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, newshadow);
                ctx.modifyAttributes(tree, mod);
    
            } catch (NamingException e) {
                return false;
            }
    
    
            return true;
    
    
        }
    
        public boolean cancellaUtente(String username) {
            String tree = "uid=" + username + ",ou=" + rbConfig.getString("spektra.users.ldap.ou") + "," + rbConfig.getString("spektra.ldap.suffix");
    
            try {
                ctx.destroySubcontext(tree);
            } catch (NamingException e) {
                return false;
            }
    
    
            return true;
        }
    
    
        private long daysSince1970() {
            //Numero giorni a partire d al 1970/1/1
            Date d = new Date();
            long dmills = d.getTime();
            return (((((dmills) / 1000) / 60) / 60) / 24);
    
        }
    
    
    }
    Alfredo

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.