Користувальницькькі налаштування

Налаштування сайту


coding:java:jndi

JNDI / LDAP

Рабочий пример:

LdapBasicExample.java
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.*;
import java.util.Hashtable;
 
/**
 * User: gmc
 * Date: 16/02/11
 * http://code.google.com/p/java-use-examples/source/browse/trunk/src/com/aw/ad/LdapBasicExample.java?r=2
 */
public class LdapBasicExample {
 
    public static void main(String[] args) {
        String userName = "ldapaccess";         // Логин входа в АД
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://192.168.2.250:389/dc=firma,dc=ua");     // Адрес сервера
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, new String("firma" + "\\" + userName));
        env.put(Context.SECURITY_CREDENTIALS, "ldapaccess");    // Пароль
        env.put(Context.REFERRAL, "follow");
        env.put("com.sun.jndi.ldap.read.timeout", "500");       // Настройка таймаута. Но не помогает :(
 
        DirContext ctx = null;
        NamingEnumeration results = null;
        try {
            ctx = new InitialDirContext(env);
            SearchControls controls = new SearchControls();
            controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            controls.getCountLimit();
 
//            String usersName = "ldapaccess";
//            String filter = "(userid="+usersName+")";
//            String filter = "(objectclass=person)&(userid=" + usersName + ")";
//            String filter = "(objectclass=person)";
 
            String filter = "(mail=*)";              // Фильтр поиска
 
            String base="";
 
            results = ctx.search(base, filter, controls);
            while (results.hasMore()) {
                SearchResult searchResult = (SearchResult) results.next();
                Attributes attributes = searchResult.getAttributes();
                Attribute attr = attributes.get("cn");
                String cn = (String) attr.get();
                System.out.println(" Person Common Name = " + attributes.get("cn"));
//                System.out.println(" Person Display Name = " + attributes.get("displayName"));
                System.out.println(" Person e-mail = " + attributes.get("mail"));
                //System.out.println(" Person logonhours = " + attributes.get("logonhours"));
//                System.out.println(" Person MemberOf = " + attributes.get("memberOf"));
            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (results != null) {
                try {
                    results.close();
                } catch (Exception e) {
                }
            }
            if (ctx != null) {
                try {
                    ctx.close();
                } catch (Exception e) {
                }
            }
        }
    }
}
javax.naming.PartialResultException [Root exception is javax.naming.CommunicationException: firma.UA:389 [Root exception is java.net.ConnectException: Connection timed out: connect]]
	at com.sun.jndi.ldap.LdapNamingEnumeration.hasMoreImpl(LdapNamingEnumeration.java:242)
	at com.sun.jndi.ldap.LdapNamingEnumeration.hasMoreReferrals(LdapNamingEnumeration.java:380)
	at com.sun.jndi.ldap.LdapNamingEnumeration.hasMoreImpl(LdapNamingEnumeration.java:226)
	at com.sun.jndi.ldap.LdapNamingEnumeration.hasMoreReferrals(LdapNamingEnumeration.java:380)
	at com.sun.jndi.ldap.LdapNamingEnumeration.hasMoreImpl(LdapNamingEnumeration.java:226)
	at com.sun.jndi.ldap.LdapNamingEnumeration.hasMore(LdapNamingEnumeration.java:189)
	at LdapBasicExample.main(LdapBasicExample.java:41)
Caused by: javax.naming.CommunicationException: firma.UA:389 [Root exception is java.net.ConnectException: Connection timed out: connect]
	at com.sun.jndi.ldap.LdapReferralContext.<init>(LdapReferralContext.java:92)
	at com.sun.jndi.ldap.LdapReferralException.getReferralContext(LdapReferralException.java:150)
	at com.sun.jndi.ldap.LdapNamingEnumeration.hasMoreReferrals(LdapNamingEnumeration.java:357)
	at com.sun.jndi.ldap.LdapNamingEnumeration.hasMoreImpl(LdapNamingEnumeration.java:226)
	... 6 more
Caused by: java.net.ConnectException: Connection timed out: connect
	at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
	at java.net.Socket.connect(Socket.java:579)
	at java.net.Socket.connect(Socket.java:528)
	at java.net.Socket.<init>(Socket.java:425)
	at java.net.Socket.<init>(Socket.java:208)
	at com.sun.jndi.ldap.Connection.createSocket(Connection.java:366)
	at com.sun.jndi.ldap.Connection.<init>(Connection.java:201)
	at com.sun.jndi.ldap.LdapClient.<init>(LdapClient.java:136)
	at com.sun.jndi.ldap.LdapClient.getInstance(LdapClient.java:1600)
	at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2698)
	at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:316)
	at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:193)
	at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:152)
	at com.sun.jndi.url.ldap.ldapURLContextFactory.getObjectInstance(ldapURLContextFactory.java:52)
	at javax.naming.spi.NamingManager.getURLObject(NamingManager.java:601)
	at javax.naming.spi.NamingManager.processURL(NamingManager.java:381)
	at javax.naming.spi.NamingManager.processURLAddrs(NamingManager.java:361)
	at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:333)
	at com.sun.jndi.ldap.LdapReferralContext.<init>(LdapReferralContext.java:111)
	... 9 more
СБОРКА УСПЕШНО ЗАВЕРШЕНА (общее время: 21 секунды)

More LDAP Operations] [[http://docs.oracle.com/javase/tutorial/jndi/ldap/result.html|LDAP. Search Results

LDAP. Search Results. rus

The JNDI Tutorial

Java™ Naming and Directory Interface (JNDI)

read LDAP with Java

http://www.adamretter.org.uk/blog/entries/LDAPTest.java

http://www.java2s.com/Code/Java/JNDI-LDAP/LDAPSearch.htm

Работа с атрибутами LDAP

Пакет javax.naming.ldap . rus

Все запросы к LDAP завершаются exception

ldapwiki.willeke.com/wiki/LDAP

MS AD. Настройка атрибутов учетных записей пользователей

coding/java/jndi.txt · Востаннє змінено: 2014/09/10 19:20 (зовнішнє редагування)