java - 根据 sn 和给定名称的 3 个字符动态分配唯一的 UID 值

标签 java ldap jndi edirectory

如何检查 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);
    }
    }
}

}

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

最佳答案

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);
    }
}

}

关于java - 根据 sn 和给定名称的 3 个字符动态分配唯一的 UID 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16564672/

相关文章:

java - 将方法转换为 java 8 lambda

java - 如何通过 API 从 JasperReport 对象(.jasper 编译文件)获取子报表名称?

java - 检测拳击课

ssl - LDAP 登录模块的 keystore

PHP LDAP 连接

java - tomcat的JNDI分享

java - 控制台程序的 JNDI 连接池?

java - 如何使用我现有的 Web 项目实现数字签名

python-ldap : what encoding should I use to check password against Microsoft AD?

Amazon RDS 和 HikariCp 数据源配置上的 MySQL SSL 连接出现 javax.net.ssl.SSLException : Unsupported record version Unknown-0. 0 错误