【问题标题】:Assign unique UID value dynamically based upon 3 character of sn and givenname根据 sn 和 givenname 的 3 个字符动态分配唯一的 UID 值
【发布时间】:2013-05-15 12:11:10
【问题描述】:

如何检查 UID 是否已经存在。如果存在,则为具有 3 个名字和姓氏字母的新用户增加一个值。如果 UId 不存在分配 UID 的值 ...并存储在 eDirectory 中..

public class searchattribute
{
   public static void main (String[] args)
{

    Hashtable env = new Hashtable();
    String adminName = "cn=admin,o=novell";
    String adminPassword = "Happiest1";
    String ldapURL = "ldaps://10.18.26.192:636";

    //Access the keystore, this is where the Root CA public key cert was installed
    //Could also do this via the command line option java -Djavax.net.ssl.trustStore....
    //No need to specifiy the keystore password for read operations
    String keystore = "C:\\Users\\durga.tiruvakkad\\Desktop\\cert.keystore";
    System.setProperty("javax.net.ssl.trustStore",keystore);

    env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");

    //set security credentials
    env.put(Context.SECURITY_AUTHENTICATION,"simple");
    env.put(Context.SECURITY_PRINCIPAL,adminName);
    env.put(Context.SECURITY_CREDENTIALS,adminPassword);

    //specify use of ssl
    env.put(Context.SECURITY_PROTOCOL,"ssl");

    //connect to my domain controller
    env.put(Context.PROVIDER_URL,ldapURL);
    try {

        // Create the initial directory context
        DirContext ctx = new InitialLdapContext(env,null);

        //Create the search controls        
        SearchControls searchCtls = new SearchControls();

        //Specify the attributes to return
        String returnedAtts[]={"sn","givenName","UID"};
        searchCtls.setReturningAttributes(returnedAtts);

        //Specify the search scope
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        //specify the LDAP search filter
        String searchFilter = "(&(objectClass=user)(UID=*))";

        //Specify the Base for the search
        String searchBase = "ou=FWCMS,o=novell";

        //initialize counter to total the results
        int totalResults = 0;


        // Search for objects using the filter
        NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);

        //Loop through the search results
        while (answer.hasMoreElements()) {
                SearchResult sr = (SearchResult)answer.next();

            totalResults++;

            System.out.println(">>>" + sr.getName());

            // Print out some of the attributes, catch the exception if the attributes have no values
            Attributes attrs = sr.getAttributes();
            if (attrs != null) {
                try {
                System.out.println("   surname: " + attrs.get("sn").get());
                System.out.println("   firstname: " + attrs.get("givenName").get());
                System.out.println("   UID: " + attrs.get("UID").get());
                                    } 
                catch (NullPointerException e)  {
                System.out.println("Errors listing attributes: " + e);
                }
            }

        }

        System.out.println("Total results: " + totalResults);
        ctx.close();

    } 
    catch (NamingException e) {
        System.err.println("Problem searching directory: " + e);
    }
    }
}

}

如果用户的名字相同,则必须检查搜索,如果用户存在,则必须增加其值

【问题讨论】:

    标签: java ldap jndi edirectory


    【解决方案1】:
    public class searchattribute {
    public static void main(String[] args) {
    
        Hashtable env = new Hashtable();
        String adminName = "cn=admin,o=novell";
        String adminPassword = "Happiest1";
        String ldapURL = "ldaps://10.18.26.192:636";
    
        // Access the keystore, this is where the Root CA public key cert was
        // installed
        // Could also do this via the command line option java
        // -Djavax.net.ssl.trustStore....
        // No need to specifiy the keystore password for read operations
        String keystore = "C:\\Users\\durga.tiruvakkad\\Desktop\\cert.keystore";
        System.setProperty("javax.net.ssl.trustStore", keystore);
    
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.ldap.LdapCtxFactory");
    
        // set security credentials
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, adminName);
        env.put(Context.SECURITY_CREDENTIALS, adminPassword);
    
        // specify use of ssl
        env.put(Context.SECURITY_PROTOCOL, "ssl");
    
        // connect to my domain controller
        env.put(Context.PROVIDER_URL, ldapURL);
        try {
    
            // Create the initial directory context
            DirContext ctx = new InitialLdapContext(env, null);
    
            Attribute attr = ctx.getAttributes("sn").get("UID");
            String uid = (String) attr.get();
    
            // Create the search controls
            SearchControls searchCtls = new SearchControls();
    
            // Specify the attributes to return
            String returnedAtts[] = { "sn", "givenName", "UID" };
            searchCtls.setReturningAttributes(returnedAtts);
    
            // Specify the search scope
            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    
            // specify the LDAP search filter
            String searchFilter = "(&(objectClass=user)(UID=*))";
    
            // Specify the Base for the search
            String searchBase = "ou=FWCMS,o=novell";
    
            // initialize counter to total the results
            int totalResults = 0;
    
            // Search for objects using the filter
            NamingEnumeration answer = ctx.search(searchBase, searchFilter,
                    searchCtls);
    
            // Loop through the search results
            while (answer.hasMoreElements()) {
                SearchResult sr = (SearchResult) answer.next();
    
                totalResults++;
    
                System.out.println(">>>" + sr.getName());
    
                // Print out some of the attributes, catch the exception if the
                // attributes have no values
                Attributes attrs = sr.getAttributes();
                if (attrs != null) {
                    try {
                        int count = 0;
                        System.out.println("   surname: "
                                + attrs.get("sn").get());
    
                        String lastName = (String) attrs.get("sn").get();
    
                        System.out.println("   firstname: "
                                + attrs.get("givenName").get());
    
                        String firstName = (String) attrs.get("givenName")
                                .get();
    
                        if (attrs.get("UID").toString().contains("GIVENUID")) {
                            count++;
                            attrs.get("UID").get().toString()
                                    .concat(firstName.substring(0, 3))
                                    .concat(lastName.substring(0, 3))
                                    .concat(String.valueOf(count));
                        }
                        System.out.println("   UID: " + attrs.get("UID").get());
    
                    } catch (NullPointerException e) {
                        System.out.println("Errors listing attributes: " + e);
                    }
                }
    
            }
    
            System.out.println("Total results: " + totalResults);
            ctx.close();
    
        } catch (NamingException e) {
            System.err.println("Problem searching directory: " + e);
        }
    }
    

    }

    【讨论】:

    • 我对 jndi 的概念不太了解,但后来也尝试解决您的问题 :)
    • 我收到此错误 搜索目录时出现问题:javax.naming.InvalidNameException: sn: [LDAP: error code 34 - Invalid DN Syntax];剩下的名字'sn'
    • 可能有一些语法错误,您没有通过,我是 jndi 的新手,无法告诉您太多相关信息
    • 好的朋友,谢谢我需要更多帮助,我会说你可以在这方面帮助我的概念 1.我必须从 xml 中获取 fn 和 LN 2.根据 fn 和 Ln 生成基本 uid 3.我们必须从baseid生成uniqueid,我们必须在eDirectory中搜索UID =BaseID 4.搜索结果必须存储在列表obj中 5.检查列表对象中是否存在唯一uiD 6.如果存在我们必须添加+1 否则使用 UId
    • 我可以从 xml 文件中获取 fn 和 ln。我可以使用 fn 和 ln 生成 UID
    【解决方案2】:
    public class searchattribute {
    public static void main(String[] args) {
    
        Hashtable env = new Hashtable();
        String adminName = "cn=admin,o=novell";
        String adminPassword = "Happiest1";
        String ldapURL = "ldaps://10.18.26.192:636";
    
        // Access the keystore, this is where the Root CA public key cert was
        // installed
        // Could also do this via the command line option java
        // -Djavax.net.ssl.trustStore....
        // No need to specifiy the keystore password for read operations
        String keystore = "C:\\Users\\durga.tiruvakkad\\Desktop\\cert.keystore";
        System.setProperty("javax.net.ssl.trustStore", keystore);
    
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.ldap.LdapCtxFactory");
    
        // set security credentials
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, adminName);
        env.put(Context.SECURITY_CREDENTIALS, adminPassword);
    
        // specify use of ssl
        env.put(Context.SECURITY_PROTOCOL, "ssl");
    
        // connect to my domain controller
        env.put(Context.PROVIDER_URL, ldapURL);
        try {
    
            // Create the initial directory context
            DirContext ctx = new InitialLdapContext(env, null);
    
            Attribute attr = ctx.getAttributes("sn").get("UID");
            String uid = (String) attr.get();
    
            // Create the search controls
            SearchControls searchCtls = new SearchControls();
    
            // Specify the attributes to return
            String returnedAtts[] = { "sn", "givenName", "UID" };
            searchCtls.setReturningAttributes(returnedAtts);
    
            // Specify the search scope
            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    
            // specify the LDAP search filter
            String searchFilter = "(&(objectClass=user)(UID=*))";
    
            // Specify the Base for the search
            String searchBase = "ou=FWCMS,o=novell";
    
            // initialize counter to total the results
            int totalResults = 0;
    
            // Search for objects using the filter
            NamingEnumeration answer = ctx.search(searchBase, searchFilter,
                    searchCtls);
    
            // Loop through the search results
            while (answer.hasMoreElements()) {
                SearchResult sr = (SearchResult) answer.next();
    
                totalResults++;
    
                System.out.println(">>>" + sr.getName());
    
                List<Set<String>> getUIDs = new ArrayList<Set<String>>();
    
                Set<String> getString = new HashSet<String>();
                // Print out some of the attributes, catch the exception if the
                // attributes have no values
                Attributes attrs = sr.getAttributes();
                if (attrs != null) {
                    try {
    
                        System.out.println("   surname: "
                                + attrs.get("sn").get());
    
                        String lastName = (String) attrs.get("sn").get();
    
                        System.out.println("   firstname: "
                                + attrs.get("givenName").get());
    
                        String firstName = (String) attrs.get("givenName")
                                .get();
    
                        if (attrs.get("UID").toString().contains("GIVENUID")) {
    
                            String uidString = attrs.get("UID").get()
                                    .toString()
                                    .concat(firstName.substring(0, 3))
                                    .concat(lastName.substring(0, 3));
                            getString.add(uidString);  //changes
                            getUIDs.add(getString);    // changes
                        }
                        System.out.println("   UID: " + attrs.get("UID").get());
    
                    } catch (NullPointerException e) {
                        System.out.println("Errors listing attributes: " + e);
                    }
                }
    
            }
    
            System.out.println("Total results: " + totalResults);
            ctx.close();
    
        } catch (NamingException e) {
            System.err.println("Problem searching directory: " + e);
        }
    }
    
    public static String searchInList(List<Set<String>> list, String uidStr) {  // new function
        int counter = 0;
        if (list != null) {
            if (list.equals(uidStr)) {
                counter++;
                uidStr.concat(String.valueOf(counter));
                return uidStr;
            }
        }
        return uidStr;
    }
    

    }

    【讨论】:

    • 你可以使用 searchInList 函数来搜索列表是否包含对象,我已经尝试解决你的问题,因为我不是专家:)
    猜你喜欢
    • 1970-01-01
    • 2021-08-05
    • 2020-02-13
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多