下面列出了javax.naming.directory.SearchControls#setTimeLimit ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private SearchControls searchControlsForQuery(LdapQuery query, boolean returnObjFlag) {
SearchControls searchControls = getDefaultSearchControls(
defaultSearchScope,
returnObjFlag,
query.attributes());
if(query.searchScope() != null) {
searchControls.setSearchScope(query.searchScope().getId());
}
if(query.countLimit() != null) {
searchControls.setCountLimit(query.countLimit());
}
if(query.timeLimit() != null) {
searchControls.setTimeLimit(query.timeLimit());
}
return searchControls;
}
/**
* Finds a distinguished name(DN) of a user by querying the active directory LDAP context for the
* specified username.
*
* @return the DN of the user, or {@code null} if there's no such user
*/
@Nullable
protected String findUserDn(LdapContextFactory ldapContextFactory, String username) throws NamingException {
LdapContext ctx = null;
try {
// Binds using the system username and password.
ctx = ldapContextFactory.getSystemLdapContext();
final SearchControls ctrl = new SearchControls();
ctrl.setCountLimit(1);
ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
ctrl.setTimeLimit(searchTimeoutMillis);
final String filter =
searchFilter != null ? USERNAME_PLACEHOLDER.matcher(searchFilter)
.replaceAll(username)
: username;
final NamingEnumeration<SearchResult> result = ctx.search(searchBase, filter, ctrl);
try {
if (!result.hasMore()) {
return null;
}
return result.next().getNameInNamespace();
} finally {
result.close();
}
} finally {
LdapUtils.closeContext(ctx);
}
}
private SearchControls makeSearchControls()
{
final SearchControls searchControls = new SearchControls();
searchControls.setReturningObjFlag( false );
searchControls.setReturningAttributes( new String[0] );
searchControls.setSearchScope( searchHelper.getSearchScope().getJndiScopeInt() );
final String[] returnAttributes = searchHelper.getAttributes() == null
? null
: searchHelper.getAttributes().toArray( new String[searchHelper.getAttributes().size()] );
searchControls.setReturningAttributes( returnAttributes );
searchControls.setTimeLimit( searchHelper.getTimeLimit() );
searchControls.setCountLimit( searchHelper.getMaxResults() );
return searchControls;
}
/**
* Method that calls the actual search on the jndi context.
*
* @param searchbase the domain name (relative to initial context in ldap) to search from.
* @param filter the non-null filter to use for the search
* @param scope the scope level of the search, one off "SearchControls.ONELEVEL_SCOPE
* @param limit the maximum number of results to return
* @param timeout the maximum time to wait before abandoning the search
* @param returnAttributes an array of strings containing the names of attributes to search. (null = all, empty array = none)
* @return
* @throws NamingException
*/
public NamingEnumeration search(String searchbase, String filter,
String[] returnAttributes, int scope, int limit, int timeout)
throws NamingException {
SearchControls constraints = new SearchControls();
if (SearchControls.ONELEVEL_SCOPE == scope) {
constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
} else if (SearchControls.SUBTREE_SCOPE == scope) {
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
} else if (SearchControls.OBJECT_SCOPE == scope) {
constraints.setSearchScope(SearchControls.OBJECT_SCOPE);
} else {
throw new NamingException("Unknown search scope: " + scope);
}
if (returnAttributes != null && returnAttributes.length == 0)
returnAttributes = new String[] { "objectClass" };
constraints.setCountLimit(limit);
constraints.setTimeLimit(timeout);
constraints.setReturningAttributes(returnAttributes);
Name n = new CompositeName().add(searchbase);
NamingEnumeration results = ctx.search(n, filter, constraints);
return results;
}
private static SearchControls createSearchControl(final boolean recursive, final String[] attributes) {
if (SECURITY_LOGGER.isTraceEnabled()) {
SECURITY_LOGGER.tracef("createSearchControl recursive=%b, attributes=%s", recursive, Arrays.toString(attributes));
}
// 2 - Search to identify the DN of the user connecting
SearchControls searchControls = new SearchControls();
if (recursive) {
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
} else {
searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
}
searchControls.setReturningAttributes(attributes);
searchControls.setTimeLimit(searchTimeLimit);
return searchControls;
}
private SearchControls getDefaultSearchControls(int searchScope, boolean returningObjFlag, String[] attrs) {
SearchControls controls = new SearchControls();
controls.setSearchScope(searchScope);
controls.setTimeLimit(defaultTimeLimit);
controls.setCountLimit(defaultCountLimit);
controls.setReturningObjFlag(returningObjFlag);
controls.setReturningAttributes(attrs);
return controls;
}
@Test
public void verifyThatDefaultSearchControlParametersAreAutomaticallyAppliedInSearch() throws Exception {
tested.setDefaultSearchScope(SearchControls.ONELEVEL_SCOPE);
tested.setDefaultCountLimit(5000);
tested.setDefaultTimeLimit(500);
expectGetReadOnlyContext();
SearchControls controls = new SearchControls();
controls.setReturningObjFlag(false);
controls.setCountLimit(5000);
controls.setTimeLimit(500);
controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
BasicAttributes expectedAttributes = new BasicAttributes();
SearchResult searchResult = new SearchResult("", null, expectedAttributes);
singleSearchResult(controls, searchResult);
Object expectedResult = new Object();
when(attributesMapperMock.mapFromAttributes(expectedAttributes)).thenReturn(expectedResult);
List list = tested.search(nameMock, "(ou=somevalue)", attributesMapperMock);
verify(namingEnumerationMock).close();
verify(dirContextMock).close();
assertThat(list).isNotNull();
assertThat(list).hasSize(1);
assertThat(list.get(0)).isSameAs(expectedResult);
}
protected SearchControls createSearchControls() {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
return searchControls;
}
protected SearchControls createSearchControls() {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
return searchControls;
}
protected SearchControls createSearchControls(LDAPConfigurator ldapConfigurator) {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
return searchControls;
}
protected String bindDNAuthentication(InitialLdapContext ctx, String user, Object credential, String baseDN,
String filter) throws NamingException
{
SearchControls constraints = new SearchControls();
constraints.setSearchScope(searchScope);
constraints.setTimeLimit(searchTimeLimit);
String attrList[] = {distinguishedNameAttribute};
constraints.setReturningAttributes(attrList);
NamingEnumeration<SearchResult> results = null;
Object[] filterArgs = {user};
results = ctx.search(baseDN, filter, filterArgs, constraints);
if (!results.hasMore())
{
results.close();
throw PicketBoxMessages.MESSAGES.failedToFindBaseContextDN(baseDN);
}
SearchResult sr = results.next();
String name = sr.getName();
String userDN = null;
Attributes attrs = sr.getAttributes();
if (attrs != null)
{
Attribute dn = attrs.get(distinguishedNameAttribute);
if (dn != null)
{
userDN = (String) dn.get();
}
}
if (userDN == null)
{
if (sr.isRelative())
userDN = name + ("".equals(baseDN) ? "" : "," + baseDN);
else
throw PicketBoxMessages.MESSAGES.unableToFollowReferralForAuth(name);
}
results.close();
results = null;
// Bind as the user dn to authenticate the user
InitialLdapContext userCtx = constructInitialLdapContext(userDN, credential);
userCtx.close();
return userDN;
}
/**
@param ctx - the context to search from
@param user - the input username
@param credential - the bind credential
@param baseDN - base DN to search the ctx from
@param filter - the search filter string
@return the userDN string for the successful authentication
@throws NamingException
*/
@SuppressWarnings("rawtypes")
protected String bindDNAuthentication(InitialLdapContext ctx, String user, Object credential, String baseDN,
String filter) throws NamingException
{
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
constraints.setTimeLimit(searchTimeLimit);
String attrList[] = {distinguishedNameAttribute};
constraints.setReturningAttributes(attrList);
NamingEnumeration results = null;
Object[] filterArgs = {user};
results = ctx.search(baseDN, filter, filterArgs, constraints);
if (results.hasMore() == false)
{
results.close();
throw PicketBoxMessages.MESSAGES.failedToFindBaseContextDN(baseDN);
}
SearchResult sr = (SearchResult) results.next();
String name = sr.getName();
String userDN = null;
Attributes attrs = sr.getAttributes();
if (attrs != null)
{
Attribute dn = attrs.get(distinguishedNameAttribute);
if (dn != null)
{
userDN = (String) dn.get();
}
}
if (userDN == null)
{
if (sr.isRelative() == true)
userDN = name + ("".equals(baseDN) ? "" : "," + baseDN);
else
throw PicketBoxMessages.MESSAGES.unableToFollowReferralForAuth(name);
}
safeClose(results);
results = null;
InitialLdapContext userCtx = constructInitialLdapContext(userDN, credential);
safeClose(userCtx);
return userDN;
}
protected SearchControls createSearchControls() {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
return searchControls;
}
protected SearchControls createSearchControls() {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
return searchControls;
}
protected SearchControls createSearchControls() {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
return searchControls;
}
protected SearchControls createSearchControls(LDAPConfiguration ldapConfigurator) {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
return searchControls;
}
/**
* Creates a connnection to a directory.
*
* @param parameters parameters to establish connection.
*/
public LdapConnection(ConnectionParameters parameters) {
super(Driver.DIALECT, parameters);
Hashtable<String, Object> env = new Hashtable<String, Object>();
//Put default settings
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
//Put connection settings
if (parameters.getUrl() == null) {
throw new LdapProviderException("Connection URL is required");
}
env.put(Context.PROVIDER_URL, parameters.getUrl());
if (parameters.getUser() != null) {
env.put(Context.SECURITY_PRINCIPAL, parameters.getUser());
}
if (parameters.getPassword() != null) {
env.put(Context.SECURITY_CREDENTIALS, parameters.getPassword());
}
//Override env with user specified connection properties
env.putAll(parameters.getProperties());
//Set the search controls used for queries
searchControls = new SearchControls();
String scope = parameters.getStringProperty(SEARCH_SCOPE_KEY);
if (scope != null) {
if ("object".equalsIgnoreCase(scope)) {
searchControls.setSearchScope(SearchControls.OBJECT_SCOPE);
} else if ("onelevel".equalsIgnoreCase(scope)) {
searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
} else if ("subtree".equalsIgnoreCase(scope)) {
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
} else {
throw new LdapProviderException("Unsupported " + SEARCH_SCOPE_KEY + "=" + scope);
}
}
String baseDn = parameters.getStringProperty(SEARCH_BASEDN_KEY);
this.baseDn = baseDn == null ? "" : baseDn;
Integer tl = parameters.getIntegerProperty(SEARCH_TIMELIMIT_KEY);
if (tl != null) {
searchControls.setTimeLimit(tl);
}
Integer cl = parameters.getIntegerProperty(SEARCH_COUNTLIMIT_KEY);
if (cl != null) {
searchControls.setCountLimit(cl);
}
Number mfl = parameters.getNumberProperty(FILE_MAXLENGTH_KEY, null);
maxFileLength = mfl == null ? null : mfl.longValue();
driverContext = parameters.getContext();
initializeContext(env); //Initializing context
}
public SearchControls getSearchControls() {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setTimeLimit(30000);
return searchControls;
}