本文整理匯總了Java中javax.naming.ldap.LdapContext.search方法的典型用法代碼示例。如果您正苦於以下問題:Java LdapContext.search方法的具體用法?Java LdapContext.search怎麽用?Java LdapContext.search使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.naming.ldap.LdapContext
的用法示例。
在下文中一共展示了LdapContext.search方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getUsersInGroup
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
@Override
public List<LdapUser> getUsersInGroup(final String groupName, final LdapContext context) throws NamingException {
if (StringUtils.isBlank(groupName)) {
throw new IllegalArgumentException("ldap group name cannot be blank");
}
final String basedn = _ldapConfiguration.getBaseDn();
if (StringUtils.isBlank(basedn)) {
throw new IllegalArgumentException("ldap basedn is not configured");
}
final SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(_ldapConfiguration.getScope());
searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());
final NamingEnumeration<SearchResult> results = context.search(basedn, generateADGroupSearchFilter(groupName), searchControls);
final List<LdapUser> users = new ArrayList<>();
while (results.hasMoreElements()) {
final SearchResult result = results.nextElement();
users.add(createUser(result));
}
return users;
}
示例2: searchUser
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
public LdapUser searchUser(final String basedn, final String searchString, final LdapContext context) throws NamingException, IOException {
final SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(_ldapConfiguration.getScope());
searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());
final NamingEnumeration<SearchResult> results = context.search(basedn, searchString, searchControls);
final List<LdapUser> users = new ArrayList<>();
while (results.hasMoreElements()) {
final SearchResult result = results.nextElement();
users.add(createUser(result));
}
if (users.size() == 1) {
return users.get(0);
} else {
throw new NamingException("No user found for basedn " + basedn + " and searchString " + searchString);
}
}
示例3: getRoleNamesForUser
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
public Set<String> getRoleNamesForUser(String username,
LdapContext ldapContext,
String userDnTemplate) throws NamingException {
try {
Set<String> roleNames = new LinkedHashSet<>();
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String searchFilter = "(&(objectClass=groupOfNames)(member=" + userDnTemplate + "))";
Object[] searchArguments = new Object[]{username};
NamingEnumeration<?> answer = ldapContext.search(
String.valueOf(ldapContext.getEnvironment().get("ldap.searchBase")),
searchFilter,
searchArguments,
searchCtls);
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
if (attrs != null) {
NamingEnumeration<?> ae = attrs.getAll();
while (ae.hasMore()) {
Attribute attr = (Attribute) ae.next();
if (attr.getID().equals("cn")) {
roleNames.add((String) attr.get());
}
}
}
}
return roleNames;
} catch (Exception e) {
LOG.error("Error", e);
}
return new HashSet<>();
}
示例4: deleteEntityByDn
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
public void deleteEntityByDn(String dn) {
try {
LdapContext context = connectionService.getContext();
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> children = context.search(dn, "(objectclass=*)", searchControls);
TreeSet<String> dnsToDelete = new TreeSet<>(new DnComparator(true));
while (children.hasMoreElements()) {
SearchResult childResult = children.nextElement();
String childDn = childResult.getNameInNamespace();
dnsToDelete.add(childDn);
}
for (String s : dnsToDelete) {
context.destroySubcontext(s);
}
} catch (NamingException e) {
throw new CukesRuntimeException("Cannot delete entity by dn " + dn, e);
} finally {
connectionService.close();
}
}
示例5: searchByFilter
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
public List<Attributes> searchByFilter(String dn, String filter){
try {
LdapContext context = connectionService.getContext();
NamingEnumeration<SearchResult> searchResults = context.search(dn, filter, new SearchControls());
List<Attributes> attributesList = new ArrayList<>();
while (searchResults.hasMore()) {
SearchResult searchResult = searchResults.next();
attributesList.add(searchResult.getAttributes());
}
return attributesList;
} catch (NamingException ex) {
throw new CukesRuntimeException(ex);
} finally {
connectionService.close();
}
}
示例6: getUserDn
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
@Instrumentation.TraceEntry(message = "get ldap user DN for username: {{1}}", timer = "ldap")
private static @Nullable String getUserDn(LdapContext ldapContext, String username,
LdapConfig ldapConfig) throws NamingException {
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<?> namingEnum = ldapContext.search(ldapConfig.userBaseDn(),
ldapConfig.userSearchFilter(), new String[] {username}, searchCtls);
try {
if (!namingEnum.hasMore()) {
return null;
}
SearchResult result = (SearchResult) checkNotNull(namingEnum.next());
String userDn = result.getNameInNamespace();
if (namingEnum.hasMore()) {
throw new IllegalStateException("More than matching user: " + username);
}
return userDn;
} finally {
namingEnum.close();
}
}
示例7: getGroupDnsForUserDn
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
@Instrumentation.TraceEntry(message = "get ldap group DNs for user DN: {{1}}", timer = "ldap")
private static Set<String> getGroupDnsForUserDn(LdapContext ldapContext, String userDn,
LdapConfig ldapConfig) throws NamingException {
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<?> namingEnum = ldapContext.search(ldapConfig.groupBaseDn(),
ldapConfig.groupSearchFilter(), new String[] {userDn}, searchCtls);
try {
Set<String> ldapGroups = Sets.newHashSet();
while (namingEnum.hasMore()) {
SearchResult result = (SearchResult) checkNotNull(namingEnum.next());
ldapGroups.add(result.getNameInNamespace());
}
return ldapGroups;
} finally {
namingEnum.close();
}
}
示例8: getUsersInGroup
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
@Override
public List<LdapUser> getUsersInGroup(String groupName, LdapContext context, Long domainId) throws NamingException {
if (StringUtils.isBlank(groupName)) {
throw new IllegalArgumentException("ldap group name cannot be blank");
}
String basedn = _ldapConfiguration.getBaseDn(domainId);
if (StringUtils.isBlank(basedn)) {
throw new IllegalArgumentException("ldap basedn is not configured");
}
final SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(_ldapConfiguration.getScope());
searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes(domainId));
NamingEnumeration<SearchResult> results = context.search(basedn, generateADGroupSearchFilter(groupName, domainId), searchControls);
final List<LdapUser> users = new ArrayList<LdapUser>();
while (results.hasMoreElements()) {
final SearchResult result = results.nextElement();
users.add(createUser(result, domainId));
}
return users;
}
示例9: searchUser
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
public LdapUser searchUser(final String basedn, final String searchString, final LdapContext context, Long domainId) throws NamingException, IOException {
final SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(_ldapConfiguration.getScope());
searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes(domainId));
NamingEnumeration<SearchResult> results = context.search(basedn, searchString, searchControls);
if(s_logger.isDebugEnabled()) {
s_logger.debug("searching user(s) with filter: \"" + searchString + "\"");
}
final List<LdapUser> users = new ArrayList<LdapUser>();
while (results.hasMoreElements()) {
final SearchResult result = results.nextElement();
users.add(createUser(result, domainId));
}
if (users.size() == 1) {
return users.get(0);
} else {
throw new NamingException("No user found for basedn " + basedn + " and searchString " + searchString);
}
}
示例10: getFullyQualifiedNameByUserId
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
protected String getFullyQualifiedNameByUserId(String userId) throws UnauthorizedOperationException {
env.put(Context.SECURITY_PRINCIPAL, ctxPrinciplePattern.replaceAll(userNamePattern, delegatedUserName));
env.put(Context.SECURITY_CREDENTIALS, delegatedUserPassword);
try {
LdapContext ctx = new InitialLdapContext(env,null);
String searchFilter = searchFilterPattern.replaceAll(userNamePattern, userId);
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = ctx.search(ldapSearchBase,
searchFilter, searchControls);
SearchResult searchResult = null;
if (results.hasMoreElements()) {
searchResult = results.nextElement();
return searchResult.getNameInNamespace();
}
return null;
} catch (NamingException e) {
throw new UnauthorizedOperationException(e.getMessage());
}
}
示例11: findUserDn
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
/**
* Finds a distinguished name(DN) of a user by querying the active directory LDAP context for the
* specified username.
*/
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()) {
throw new AuthenticationException("No username: " + username);
}
return result.next().getNameInNamespace();
} finally {
result.close();
}
} finally {
LdapUtils.closeContext(ctx);
}
}
示例12: userList
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
public static ArrayList<String> userList()
{
loadAdmindetails();
System.out.println("Details Loaded");
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
props.put(Context.PROVIDER_URL, url);
props.put(Context.SECURITY_PRINCIPAL, "cn="+ldapadminuname+",dc=serverless,dc=com");//adminuser - User with special priviledge, dn user
props.put(Context.SECURITY_CREDENTIALS, ldapadminpassword);//dn user password
ArrayList<String> resp = new ArrayList<String>();
try {
LdapContext ctx = new InitialLdapContext(props, null);
ctx.setRequestControls(null);
NamingEnumeration<?> namingEnum = ctx.search("ou=ias,dc=serverless,dc=com", "(objectclass=posixAccount)", AuthenticationService.getSimpleSearchControls());
while (namingEnum.hasMore ()) {
SearchResult result = (SearchResult) namingEnum.next ();
Attributes attrs = result.getAttributes ();
resp.add(attrs.get("cn").toString().substring(4));
}
namingEnum.close();
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}
示例13: ldapApiQuery
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
private List<SearchResult> ldapApiQuery(String action, String name, String filter) {
String logMsg = action + " " + filter;
List<SearchResult> result = new ArrayList<SearchResult>();
try {
initLdapContext(action);
LdapContext ldapCtx = ldapContexts.get(action);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> en = ldapCtx.search(name, filter, constraints);
// means all nodes
if (en == null) {
loggerInfo("LDAP信息", "獲取", "結果為空", logMsg);
return Collections.emptyList();
}
if (!en.hasMoreElements()) {
loggerInfo("LDAP信息", "獲取", "結果為空", logMsg);
return Collections.emptyList();
}
while (en != null && en.hasMoreElements()) {// maybe more than one element
Object obj = en.nextElement();
if (obj instanceof SearchResult) {
SearchResult si = (SearchResult) obj;
result.add(si);
}
}
}
catch (Exception e) {
loggerError("LDAP用戶信息獲取", logMsg, e);
clearLdapContext(action);
}
if (!result.isEmpty()) {
loggerInfo("LDAP信息", "獲取", "成功", logMsg);
}
return result;
}
示例14: search
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
public NamingEnumeration<SearchResult> search(final String baseDN, final String filter) throws NamingException {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
LdapContext ctx = new InitialLdapContext(env, null);
NamingEnumeration<SearchResult> result = ctx.search(baseDN, filter, searchControls);
ctx.close();
return result;
}
示例15: getUsersInGroup
import javax.naming.ldap.LdapContext; //導入方法依賴的package包/類
@Override
public List<LdapUser> getUsersInGroup(final String groupName, final LdapContext context) throws NamingException {
final String attributeName = _ldapConfiguration.getGroupUniqueMemeberAttribute();
final SearchControls controls = new SearchControls();
controls.setSearchScope(_ldapConfiguration.getScope());
controls.setReturningAttributes(new String[]{attributeName});
final NamingEnumeration<SearchResult> result = context.search(_ldapConfiguration.getBaseDn(), generateGroupSearchFilter(groupName), controls);
final List<LdapUser> users = new ArrayList<>();
//Expecting only one result which has all the users
if (result.hasMoreElements()) {
final Attribute attribute = result.nextElement().getAttributes().get(attributeName);
final NamingEnumeration<?> values = attribute.getAll();
while (values.hasMoreElements()) {
final String userdn = String.valueOf(values.nextElement());
try {
users.add(getUserForDn(userdn, context));
} catch (final NamingException e) {
s_logger.info("Userdn: " + userdn + " Not Found:: Exception message: " + e.getMessage());
}
}
}
Collections.sort(users);
return users;
}