命令方式对查询进行排序
D:\utrustsystem\installed\OpenLDAP>ldapsearch -x -h localhost -D "uid=admin,ou=p
erson,ou=utrust,dc=chinautrust,dc=com" -b "ou=person,ou=utrust,dc=chinautrust,dc
=com" -w "admin" -S "uidaleph"
LDAP分页和排序(JDK自带)
tualListViewControl;
import java.io.IOException;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.Control;
import javax.naming.ldap.HasControls;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.SortControl;
public class SortLDAP {
    public static void main(String[] args) throws NamingException, IOException {
        Hashtable m_env = new Hashtable(5, );
        m_env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.ldap.LdapCtxFactory");
        //m_env.put("java.naming.ldap.version", "3");
        m_env.put(Context.PROVIDER_URL, "ldap://192.168.0.111:389");
        m_env.put(Context.SECURITY_AUTHENTICATION, "simple");
        m_env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");
        m_env.put(Context.SECURITY_CREDENTIALS, "password12345");
        // Open an LDAP association
        LdapContext ctx = new InitialLdapContext(m_env, null);
        // Activate sorting
        String sortKey = "uid";
     
        int beforeCount = 0;
        int afterCount = 0;
        int nTargetOffset = 0;
        int nPageSize = 5;
        VirtualListViewControl vctl = new VirtualListViewControl(nTargetOffset, beforeCount, afterCount, nPageSize, Control.CRITICAL);
        ctx.setRequestControls(new Control[]{new SortControl(sortKey, Control.CRITICAL),vctl});
       
        String[] retAtt = {"uid"};
        SearchControls constraints = new SearchControls();
        constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
        constraints.setReturningAttributes(retAtt);
        // ctx.setRequestControls(new Control[]{new PagedResultsControl(2)});
        // Perform a search
        NamingEnumeration results =
                ctx.search("ou=person,ou=utrust,dc=chinautrust,dc=com", "(objectclass=*)", constraints);
        // Iterate over search results
        while (results != null && results.hasMore()) {
            // Display an entry
            SearchResult entry = (SearchResult) ();
            System.out.Attributes());
            // Handle the entry's response controls (if any)
            if (entry instanceof HasControls) {
                // ((HasControls)entry).getControls();
            }
        }
        // Close the LDAP association
        ctx.close();
    }
}
LDAP数据排序(netscape包)
import netscape.ldap.LDAPCompareAttrNames;
import netscape.ldap.LDAPConnection;
import netscape.ldap.LDAPEntry;
import netscape.ldap.LDAPException;
import netscape.ldap.LDAPReferralException;
import netscape.ldap.LDAPSearchResults;
import netscape.ldap.util.ConnectionPool;
public class LdapUtils {
    private static final String HOST = "localhost";
    private static final int PORT = 389;
    private static final int MIN_CONN_SIZE = 1;
    private static final int MAX_CONN_SIZE = 5;
    private static final String USER_ID = "cn=Manager,dc=chinautrust,dc=com";
    private static final String PASSWORD = "password12345";
    private static ConnectionPool connPool;
   
    static {
        try {
            connPool = new ConnectionPool(MIN_CONN_SIZE, MAX_CONN_SIZE, HOST, PORT);
        } catch (LDAPException e) {
            String message = "Failed to init Ldap connection pool";
            System.out.println(message);
        }
    }
    public static ConnectionPool getConnectionPool() {
        Pool;
sort命令排序
    }
    public static LDAPConnection getConnection() {
        LDAPConnection conn = ConnectionPool().getConnection();
        try {
            conn.authenticate(USER_ID, PASSWORD);
        } catch (LDAPException e) {
            String message = "Failed to authen the system ldap user";
            System.out.println(message);
        }
        return conn;
    }
    public static void closeConnection(LDAPConnection conn) {

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。