本文整理匯總了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();
}
示例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();
}
}
示例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;
}
}
示例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;
}
}
示例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);
}
示例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);
}
示例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();
}
}
示例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));
}