不要理解这个Java LDAP错误

时间:2016-02-29 17:52:23

标签: java ldap

这里有什么问题?我不明白?

/**
 * @param args the command line arguments
 */
public class HsAppLDAP {
@SuppressWarnings("rawtypes")
public static void main(String[] args) {

    // Déclaration des variables
    ArrayList<String> values = new ArrayList<String>();
    Connection connection;
    Statement statement;

    // Récupération des données depuis le fichier excel
    try {

        InputStream input = new FileInputStream("student.xls");
        POIFSFileSystem fs = new POIFSFileSystem(input);
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        Iterator rows = sheet.rowIterator();

        while (rows.hasNext()) {

            values.clear();

            HSSFRow row = (HSSFRow) rows.next();

            Iterator cells = row.cellIterator();

            while (cells.hasNext()) {

                HSSFCell cell = (HSSFCell) cells.next();

                if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType())
                    values.add(Integer.toString((int) cell.getNumericCellValue()));
                else if (HSSFCell.CELL_TYPE_STRING == cell.getCellType())
                    values.add(cell.getStringCellValue());

            }

            // Insertion en BDD
                            String url = "ldap://192.168.0.244:389";
                            Hashtable env = new Hashtable();
                            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
                            env.put(Context.PROVIDER_URL, url);
                            env.put(Context.SECURITY_AUTHENTICATION, "none");
                            env.put(Context.SECURITY_PRINCIPAL, "uid=admin, o=hs-fulda");
                            env.put(Context.SECURITY_CREDENTIALS, "ToCHange!");

            try {
                DirContext ctx = new InitialDirContext(env);
                                    System.out.println("connected");
                                    System.out.println(ctx.getEnvironment());

                                    try {

                                        Attribute cn = new BasicAttribute("cn", values.get(0));
                                        System.out.println(cn);
                                        Attribute sn = new BasicAttribute("sn", values.get(1));
                                        System.out.println(sn);
                                        Attribute fullName = new BasicAttribute("fullName", values.get(2) +" "+ values.get(1));
                                        System.out.println(fullName);
                                        Attribute givenName = new BasicAttribute("givenName", values.get(2));
                                        System.out.println(givenName);
                                        Attribute uid = new BasicAttribute("uid", values.get(0));
                                        System.out.println(uid);
                                        Attribute ou = new BasicAttribute("ou", values.get(3));
                                        System.out.println(ou);

                                        Attribute oc = new BasicAttribute("objectClass");
                                        System.out.println(oc);
                                        oc.add("Top");
                                        oc.add("Person");
                                        oc.add("organizationalPerson");
                                        oc.add("inetOrgPerson");

                                        BasicAttributes entry = new BasicAttributes();

                                        entry.put(cn);
                                        entry.put(sn);
                                        entry.put(fullName);
                                        entry.put(givenName);
                                        entry.put(uid);
                                        entry.put(ou);
                                        entry.put(oc);

                                        String entryDN = values.get(0);

                                        ctx.createSubcontext(entryDN, entry);

                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        System.out.println("Insertion nicht möglich: ECHEC");
                                    }

                /**String sql = String
                        .format("INSERT INTO users(first_name, last_name, username, password, age) VALUES ('%s', '%s', '%s', '%s', %s)",
                                values.get(0), values.get(1),
                                values.get(2), values.get(3), values.get(4));**/

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

我有这个输出:

    connected
{java.naming.provider.url=ldap://192.168.0.244:389, java.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory, java.naming.security.principal=uid=admin, o=hs-fulda, java.naming.security.authentication=none, java.naming.security.credentials=ToCHange!}
cn: fdai0000
sn: Buehler
fullName: Mark Buehler
givenName: Mark
uid: fdai0000
ou: AI
objectClass: No values
javax.naming.InvalidNameException: Invalid name: fdai0000; remaining name 'fdai0000'
Insertion nicht möglich: ECHEC
connected
    at javax.naming.ldap.Rfc2253Parser.doParse(Rfc2253Parser.java:111)
    at javax.naming.ldap.Rfc2253Parser.parseDn(Rfc2253Parser.java:70)
    at javax.naming.ldap.LdapName.parse(LdapName.java:785)
    at javax.naming.ldap.LdapName.<init>(LdapName.java:123)
{java.naming.provider.url=ldap://192.168.0.244:389, java.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory, java.naming.security.principal=uid=admin, o=hs-fulda, java.naming.security.authentication=none, java.naming.security.credentials=ToCHange!}
cn: fdai0001
    at com.sun.jndi.ldap.LdapCtx.addRdnAttributes(LdapCtx.java:922)
sn: Wagner
    at com.sun.jndi.ldap.LdapCtx.c_createSubcontext(LdapCtx.java:804)
fullName: Toma Wagner
givenName: Toma
    at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_createSubcontext(ComponentDirContext.java:341)
uid: fdai0001
ou: AI
    at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.createSubcontext(PartialCompositeDirContext.java:268)
    at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.createSubcontext(PartialCompositeDirContext.java:256)
objectClass: No values
    at javax.naming.directory.InitialDirContext.createSubcontext(InitialDirContext.java:197)
    at hsappldap.HsAppLDAP.main(HsAppLDAP.java:122)
javax.naming.InvalidNameException: Invalid name: fdai0001; remaining name 'fdai0001'
Insertion nicht möglich: ECHEC
    at javax.naming.ldap.Rfc2253Parser.doParse(Rfc2253Parser.java:111)
    at javax.naming.ldap.Rfc2253Parser.parseDn(Rfc2253Parser.java:70)
    at javax.naming.ldap.LdapName.parse(LdapName.java:785)
    at javax.naming.ldap.LdapName.<init>(LdapName.java:123)
    at com.sun.jndi.ldap.LdapCtx.addRdnAttributes(LdapCtx.java:922)
    at com.sun.jndi.ldap.LdapCtx.c_createSubcontext(LdapCtx.java:804)
    at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_createSubcontext(ComponentDirContext.java:341)
    at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.createSubcontext(PartialCompositeDirContext.java:268)
    at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.createSubcontext(PartialCompositeDirContext.java:256)
    at javax.naming.directory.InitialDirContext.createSubcontext(InitialDirContext.java:197)
    at hsappldap.HsAppLDAP.main(HsAppLDAP.java:122)

1 个答案:

答案 0 :(得分:1)

就像异常消息所说:

  

javax.naming.InvalidNameException:名称无效:fdai0000;剩下的名字&#39; fdai0000&#39;

您正在尝试使用> nsolid-cli ls {"pid":13143,"hostname":"rainworld","app":"example","address":"10.99.46.63:39682","id":"ec04fa6e98e08961bdba4f757d1cd2297563e4a6"} {"pid":13908,"hostname":"rainworld","app":"example","address":"10.99.46.63:46188","id":"1fc5132d6b4f779a073b0ca8952862e165eb546a"} {"pid":13095,"hostname":"rainworld","app":"example","address":"10.99.46.63:1111","id":"c2bddf9a505a490bab333e8b93627171586dbf6a"} {"pid":13130,"hostname":"rainworld","app":"example","address":"10.99.46.63:42300","id":"b6b2a5a5c0da40ececc9ef0a0eed5fd4cd0927eb"} 作为专有名称在ldap中创建一个条目 - 但这不是有效的专有名称。