当前位置: 首页>>代码示例>>Java>>正文


Java InitialDirContext.search方法代码示例

本文整理汇总了Java中javax.naming.directory.InitialDirContext.search方法的典型用法代码示例。如果您正苦于以下问题:Java InitialDirContext.search方法的具体用法?Java InitialDirContext.search怎么用?Java InitialDirContext.search使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.naming.directory.InitialDirContext的用法示例。


在下文中一共展示了InitialDirContext.search方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: dnFromUser

import javax.naming.directory.InitialDirContext; //导入方法依赖的package包/类
private static String dnFromUser(String username) throws NamingException {
    Properties props = new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    props.put(Context.PROVIDER_URL, "ldap://ldap.example.com");
    props.put(Context.REFERRAL, "ignore");

    InitialDirContext context = new InitialDirContext(props);

    SearchControls ctrls = new SearchControls();
    ctrls.setReturningAttributes(new String[]{"givenName", "sn"});
    ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    NamingEnumeration<SearchResult> answers = context.search("dc=People,dc=example,dc=com", "(uid=" + username + ")", ctrls);
    SearchResult result = answers.next();

    return result.getNameInNamespace();
}
 
开发者ID:blackarbiter,项目名称:Android_Code_Arbiter,代码行数:18,代码来源:JndiLdap.java

示例2: getAll

import javax.naming.directory.InitialDirContext; //导入方法依赖的package包/类
private void getAll(InitialDirContext ctx, String uid) {

			try {
				Attributes matchAttrs = new BasicAttributes(true);
				matchAttrs.put(new BasicAttribute("member", user));

				NamingEnumeration<SearchResult> answer = ctx.search(
						"ou=permissions", matchAttrs, new String[] { "cn" });

				while (answer.hasMore()) {
					SearchResult searchResult = answer.next();
					Attributes attributes = searchResult.getAttributes();
					attributes.get("cn");
				}

			} catch (NamingException e) {
				e.printStackTrace();
			}
		}
 
开发者ID:fpuna-cia,项目名称:karaku,代码行数:20,代码来源:LdapStressTest.java

示例3: lookupExistence

import javax.naming.directory.InitialDirContext; //导入方法依赖的package包/类
/**
 * Looks up an LDAP object by its DN and returns <tt>true</tt> if
 * the search was successful.
 *
 * @param ctx the Context to use for the lookup.
 * @param dn the object's dn to lookup.
 * @return true if the lookup was successful.
 * @throws NamingException if login credentials were wrong.
 */
private Boolean lookupExistence(InitialDirContext ctx, String dn, String[] returnattrs) throws NamingException {
    boolean debug = Log.isDebugEnabled();

    if (debug) {
        Log.debug("LdapManager: In lookupExistence(ctx, dn, returnattrs), searchdn is: " + dn);
    }

    // Bind to the object's DN
    ctx.addToEnvironment(Context.PROVIDER_URL, getProviderURL(dn));

    String filter = "(&(objectClass=*))";
    SearchControls srcnt = new SearchControls();
    srcnt.setSearchScope(SearchControls.OBJECT_SCOPE);
    srcnt.setReturningAttributes(returnattrs);

    NamingEnumeration<SearchResult> answer = null;

    try {
        answer = ctx.search(
                "",
                filter,
                srcnt);
    } catch (javax.naming.NameNotFoundException nex) {
        // DN not found
    } catch (NamingException ex){
        throw ex;
    }

    if (answer == null || !answer.hasMoreElements())
    {
        Log.debug("LdapManager: .... lookupExistence: DN not found.");
        return false;
    }
    else
    {
        Log.debug("LdapManager: .... lookupExistence: DN found.");
        return true;
    }
}
 
开发者ID:igniterealtime,项目名称:Openfire,代码行数:49,代码来源:LdapManager.java

示例4: lookupExistence

import javax.naming.directory.InitialDirContext; //导入方法依赖的package包/类
/** 
   * Looks up an LDAP object by its DN and returns <tt>true</tt> if
   * the search was successful.
   * 
   * @param ctx the Context to use for the lookup.
   * @param dn the object's dn to lookup.
   * @return true if the lookup was successful.
   * @throws NamingException if login credentials were wrong.
   */
  private Boolean lookupExistence(InitialDirContext ctx, String dn, String[] returnattrs) throws NamingException {
  	boolean debug = Log.isDebugEnabled();
  	
      if (debug) {
          Log.debug("LdapManager: In lookupExistence(ctx, dn, returnattrs), searchdn is: " + dn); 
      }
      
      // Bind to the object's DN
      ctx.addToEnvironment(Context.PROVIDER_URL, getProviderURL(dn));

  	String filter = "(&(objectClass=*))";
SearchControls srcnt = new SearchControls();
srcnt.setSearchScope(SearchControls.OBJECT_SCOPE);
srcnt.setReturningAttributes(returnattrs);

NamingEnumeration<SearchResult> answer = null;

try {
	answer = ctx.search(
			"",
			filter,
			srcnt);
} catch (javax.naming.NameNotFoundException nex) {
	// DN not found
} catch (NamingException ex){
	throw ex;
}
  	
if (answer == null || !answer.hasMoreElements())
{
          Log.debug("LdapManager: .... lookupExistence: DN not found.");
	return false;
}
else
{
	Log.debug("LdapManager: .... lookupExistence: DN found.");
	return true;
}
  }
 
开发者ID:coodeer,项目名称:g3server,代码行数:49,代码来源:LdapManager.java

示例5: getLDAPInformation

import javax.naming.directory.InitialDirContext; //导入方法依赖的package包/类
private NamingEnumeration<SearchResult> getLDAPInformation(InitialDirContext context, String login) throws NamingException {
	SearchControls cons = new SearchControls();
	cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
	cons.setDerefLinkFlag(false);
	return context.search(ldapValue.getUserSearchBase(), MessageFormat.format(ldapValue.getUserSearchFilter(), login), cons);
}
 
开发者ID:adessoAG,项目名称:JenkinsHue,代码行数:7,代码来源:LDAPManager.java

示例6: moreLdapInjections

import javax.naming.directory.InitialDirContext; //导入方法依赖的package包/类
public static void moreLdapInjections(String input) throws NamingException {
    //Stub instances
    Properties props = new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    props.put(Context.PROVIDER_URL, "ldap://ldap.example.com");
    props.put(Context.REFERRAL, "ignore");

    SearchControls ctrls = new SearchControls();
    ctrls.setReturningAttributes(new String[]{"givenName", "sn"});
    ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    //Various context instance store in various type (class or interface)
    DirContext         context1 = new InitialDirContext(props);
    InitialDirContext  context2 = new InitialDirContext(props);
    InitialLdapContext context3 = new InitialLdapContext();
    LdapContext        context4 = new InitialLdapContext();

    NamingEnumeration<SearchResult> answers;
    answers = context1.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=" + input + ")", ctrls);
    answers = context1.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=" + input + ")", new Object[0], ctrls);
    answers = context1.search("dc=People,dc=example,dc=com", "(uid=" + input + ")", ctrls);
    answers = context1.search("dc=People,dc=example,dc=com", "(uid=" + input + ")", new Object[0], ctrls);

    answers = context2.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=" + input + ")", ctrls);
    answers = context2.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=" + input + ")", new Object[0], ctrls);
    answers = context2.search("dc=People,dc=example,dc=com", "(uid=" + input + ")", ctrls);
    answers = context2.search("dc=People,dc=example,dc=com", "(uid=" + input + ")", new Object[0], ctrls);

    answers = context3.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=" + input + ")", ctrls);
    answers = context3.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=" + input + ")", new Object[0], ctrls);
    answers = context3.search("dc=People,dc=example,dc=com", "(uid=" + input + ")", ctrls);
    answers = context3.search("dc=People,dc=example,dc=com", "(uid=" + input + ")", new Object[0], ctrls);

    answers = context4.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=" + input + ")", ctrls);
    answers = context4.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=" + input + ")", new Object[0], ctrls);
    answers = context4.search("dc=People,dc=example,dc=com", "(uid=" + input + ")", ctrls);
    answers = context4.search("dc=People,dc=example,dc=com", "(uid=" + input + ")", new Object[0], ctrls);


    //False positive
    answers = context1.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=bob)", ctrls);
    answers = context1.search(new LdapName("dc=People,dc=example,dc=com"), "(uid=bob)", new Object[0], ctrls);
    answers = context1.search("dc=People,dc=example,dc=com", "(uid=bob)", ctrls);
    answers = context1.search("dc=People,dc=example,dc=com", "(uid=bob)", new Object[0], ctrls);
}
 
开发者ID:blackarbiter,项目名称:Android_Code_Arbiter,代码行数:46,代码来源:JndiLdapAdditionalSignature.java

示例7: start

import javax.naming.directory.InitialDirContext; //导入方法依赖的package包/类
/**
 * start the connector
 */
public void start() throws Exception {
    LOG.info("connecting...");
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    this.ldapURI = getUri();
    LOG.debug("    URI [{}]", this.ldapURI);
    env.put(Context.PROVIDER_URL, this.ldapURI.toString());
    if (anonymousAuthentication) {
        LOG.debug("    login credentials [anonymous]");
        env.put(Context.SECURITY_AUTHENTICATION, "none");
    } else {
        LOG.debug("    login credentials [{}:******]", user);
        env.put(Context.SECURITY_PRINCIPAL, user);
        env.put(Context.SECURITY_CREDENTIALS, password);
    }
    boolean isConnected = false;
    while (!isConnected) {
        try {
            context = new InitialDirContext(env);
            isConnected = true;
        } catch (CommunicationException err) {
            if (failover) {
                this.ldapURI = getUri();
                LOG.error("connection error [{}], failover connection to [{}]", env.get(Context.PROVIDER_URL), this.ldapURI.toString());
                env.put(Context.PROVIDER_URL, this.ldapURI.toString());
                Thread.sleep(curReconnectDelay);
                curReconnectDelay = Math.min(curReconnectDelay * 2, maxReconnectDelay);
            } else {
                throw err;
            }
        }
    }

    // add connectors from search results
    LOG.info("searching for network connectors...");
    LOG.debug("    base   [{}]", base);
    LOG.debug("    filter [{}]", searchFilter);
    LOG.debug("    scope  [{}]", searchControls.getSearchScope());
    NamingEnumeration<SearchResult> results = context.search(base, searchFilter, searchControls);
    while (results.hasMore()) {
        addConnector(results.next());
    }

    // register persistent search event listener
    if (searchEventListener) {
        LOG.info("registering persistent search listener...");
        EventDirContext eventContext = (EventDirContext) context.lookup("");
        eventContext.addNamingListener(base, searchFilter, searchControls, this);
    } else { // otherwise close context (i.e. connection as it is no longer needed)
        context.close();
    }
}
 
开发者ID:DiamondLightSource,项目名称:daq-eclipse,代码行数:56,代码来源:LdapNetworkConnector.java

示例8: search

import javax.naming.directory.InitialDirContext; //导入方法依赖的package包/类
/**
 * Perform an LDAP search, utilizing a User provided InitalDirContext object.
 * The InitialDirContext is not closed after the search, allowing it
 * to be reused for multiple searches.
 * 
 * The default max number of matches returned is 1000
 * 
 * @param context		{@link InitalDirContext}
 * @param searchContext	The fully qualified base to start the search
 * 						Usually in the form <code>CN=,OU=,OU=,DC=,DC=</code>
 * @param filter		@see <a href="http://docs.oracle.com/cd/E19528-01/819-0997/gdxpd/index.html">Examples</a>
 * @return				An enumeration that doesn't pre-load all results into memory.
 * @throws NamingException
 */
public NamingEnumeration<SearchResult> search(final InitialDirContext context,
											  final String searchContext,
											  final String filter
											 ) throws NamingException
{
	return context.search(searchContext, filter, new SearchControls(SearchControls.SUBTREE_SCOPE, 1000, (5*60*1000),  null, true, true));
}
 
开发者ID:ZacWolf,项目名称:com.zacwolf.commons.webex,代码行数:22,代码来源:ManageDomain.java


注:本文中的javax.naming.directory.InitialDirContext.search方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。