简体   繁体   English

在Web服务中具有多个目录上下文

[英]having more than one dircontext in webservice

I wish to know may I have more than one DirContext in my method? 我想知道我的方法中可以有多个DirContext吗? These are the code that I currently have now. 这些是我目前拥有的代码。

//first connection to get what I want.
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, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=100001,ou=users,o=data");
    env.put(Context.SECURITY_CREDENTIALS, "P@ssw0rd");

    try{
    DirContext context = new InitialDirContext(env);
    SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
    NamingEnumeration result = context.search("", cnName, constraints);
    while(result.hasMore())
    {
        SearchResult searchResult = (SearchResult) result.next();
        Attributes attrs = searchResult.getAttributes();
        //request.setEmail(attrs.get("mail").toString());
        //request.setPhoneNumber(attrs.get("personalMobile").toString());
        //Attribute ldapattr = attrs.get("photo");
        //if(ldapattr != null){
          //  byte[] photo = (byte[])ldapattr.get();
            //request.setPhoto(photo);
        //}
        request.setCn(attrs.get("cn").toString());
        userDN = request.getCn(); //this is what i want so that i assign the value.
        System.out.println("DN here");
        System.out.println(userDN);
        context.close();
        result.close();
    }
    }catch(Exception e){
        System.out.println("can't initialized");
    }

//I wish to have my second DirContext here

I am trying to use other security_principle where I get from the first connection and try to open a new connection to connect to LDAP, so that I can get other connection after I get what i want from the first connection. 我正在尝试使用从第一个连接获得的其他security_principle,并尝试打开一个新连接以连接到LDAP,以便在从第一个连接获得所需的内容后可以获得其他连接。

How about: DirContext context2 = new InitialDirContext(env); 怎么样:DirContext context2 = new InitialDirContext(env); ? Or also with a new env... 或者也有一个新的环境...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM