本文整理汇总了Java中com.unboundid.ldap.sdk.LDAPConnection类的典型用法代码示例。如果您正苦于以下问题:Java LDAPConnection类的具体用法?Java LDAPConnection怎么用?Java LDAPConnection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LDAPConnection类属于com.unboundid.ldap.sdk包,在下文中一共展示了LDAPConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: rolesFromDN
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
private Set<String> rolesFromDN(String userDN) throws LDAPException, GeneralSecurityException {
SearchRequest searchRequest = new SearchRequest(config.getRoleBaseDN(),
SearchScope.SUB, Filter.createEqualityFilter("uniqueMember", userDN));
Set<String> roles = Sets.newLinkedHashSet();
LDAPConnection connection = connectionFactory.getLDAPConnection();
try {
SearchResult sr = connection.search(searchRequest);
for (SearchResultEntry sre : sr.getSearchEntries()) {
X500Name x500Name = new X500Name(sre.getDN());
RDN[] rdns = x500Name.getRDNs(BCStyle.CN);
if (rdns.length == 0) {
logger.error("Could not create X500 Name for role:" + sre.getDN());
} else {
String commonName = IETFUtils.valueToString(rdns[0].getFirst().getValue());
roles.add(commonName);
}
}
} finally {
connection.close();
}
return roles;
}
示例2: modifyLdapEntry
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
/**
* Modify ldap entry.
*
* @param serverCon the server con
* @param dn the dn
* @param attr the attr
* @param add the add
*/
public static void modifyLdapEntry(final LDAPConnection serverCon, final String dn, final LdapAttribute attr,
final AttributeModificationType add) {
try {
final String address = "ldap://" + serverCon.getConnectedAddress() + ':' + serverCon.getConnectedPort();
try (Connection conn = DefaultConnectionFactory.getConnection(address)) {
try {
conn.open();
final ModifyOperation modify = new ModifyOperation(conn);
modify.execute(new ModifyRequest(dn, new AttributeModification(add, attr)));
} catch (final Exception e) {
LOGGER.debug(e.getMessage(), e);
}
}
} finally {
serverCon.close();
}
}
示例3: doSearch
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
private SearchResult doSearch(LDAPConnection ldapConnection, String base, boolean dereferenceAliases, String filter, List<String> attributes) {
try {
SearchRequest searchRequest = new SearchRequest(base, SearchScope.SUB, filter);
if (dereferenceAliases) {
searchRequest.setDerefPolicy(DereferencePolicy.SEARCHING);
}
if (attributes != null) {
searchRequest.setAttributes(attributes);
}
return ldapConnection.search(searchRequest);
} catch (LDAPException e) {
if (!Strings.isNullOrEmpty(e.getDiagnosticMessage())) {
log.error(e.getDiagnosticMessage());
} else {
log.error("Problem searching LDAP", e);
}
return null;
}
}
示例4: setAccountRoles
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
private void setAccountRoles(LDAPConnection ldapConnection, SearchResultEntry accountSearchResult, Account account) {
String accountDN = accountSearchResult.getDN();
String groupMemberPattern = this.groupMemberPattern.replace("${dn}", escapeLDAPSearchFilter(accountDN));
groupMemberPattern = groupMemberPattern.replace("${username}", escapeLDAPSearchFilter(account.getUsername()));
// Fill in attributes into groupMemberPattern
for (Attribute attribute : accountSearchResult.getAttributes()) {
groupMemberPattern = groupMemberPattern.replace("${" + attribute.getName() + "}", escapeLDAPSearchFilter(attribute.getValue()));
}
SearchResult groupsSearchResult = doSearch(ldapConnection, groupBase, true, groupMemberPattern, Arrays.asList("cn"));
if (groupsSearchResult != null && groupsSearchResult.getEntryCount() > 0) {
for (int i = 0; i < groupsSearchResult.getEntryCount(); i++) {
SearchResultEntry groupEntry = groupsSearchResult.getSearchEntries().get(i);
String roleName = groupEntry.getAttribute("cn").getValue();
account.getAuthorizations().addRole(roleName);
}
}
}
示例5: importLdifFileInLdap
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
public ResultCode importLdifFileInLdap(InputStream is) throws LDAPException {
ResultCode result = ResultCode.UNAVAILABLE;
LDAPConnection connection = ldapEntryManager.getOperationService().getConnection();
try {
LdifDataUtility ldifDataUtility = LdifDataUtility.instance();
LDIFReader importLdifReader = new LDIFReader(is);
result = ldifDataUtility.importLdifFile(connection, importLdifReader);
importLdifReader.close();
} catch (Exception ex) {
log.error("Failed to import ldif file: ", ex);
} finally {
ldapEntryManager.getOperationService().releaseConnection(connection);
}
return result;
}
示例6: getAttributeResultEntryLDIF
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
public List<SearchResultEntry> getAttributeResultEntryLDIF(LDAPConnection connection, List<String> patterns, String baseDN) {
List<SearchResultEntry> searchResultEntryList = new ArrayList<SearchResultEntry>();
try {
for (String pattern : patterns) {
String[] targetArray = new String[] { pattern };
Filter inumFilter = Filter.createSubstringFilter("inum", null,targetArray, null);
Filter searchFilter = Filter.createORFilter(inumFilter);
SearchResultEntry sr = connection.searchForEntry(baseDN,SearchScope.SUB, searchFilter, null);
searchResultEntryList.add(sr);
}
return searchResultEntryList;
} catch (LDAPException le) {
if (le.getResultCode() != ResultCode.NO_SUCH_OBJECT) {
log.error("Failed to search ldif record", le);
return null;
}
}
return null;
}
示例7: runSearch
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
private void runSearch(LDAPConnection connection, SearchRequest searchRequest, CRUDOperationContext ctx, SearchResultProcessor searchRunner) {
execute(ctx, new ExecutionHandler() {
@Override
void onSuccess(LDAPResult searchResult) {
for (SearchResultEntry entry : ((SearchResult) searchResult).getSearchEntries()) {
searchRunner.process(entry);
}
}
@Override
SearchResult execute() throws LDAPException {
return connection.search(searchRequest);
}
});
}
示例8: authenticate
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
public boolean authenticate(String username, String password) {
boolean success = false;
LDAPConnection ldapConnection = null;
String loginDn = getLoginDn(username);
if (loginDn != null) {
try {
ldapConnection = getUserConnection(loginDn, password);
if (ldapConnection != null) {
success = true;
}
}
catch (Exception e) {
LOG.error(e.getMessage(), e);
}
finally {
if (ldapConnection != null) {
ldapConnection.close();
}
}
}
return success;
}
示例9: serverDegradedTest
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
@Test
public void serverDegradedTest() throws Exception
{
ds.clear();
// Server is degraded, but 'available'.
addBaseEntry(new String[0],
new String[]{"low-disk-space-error"});
LDAPConnection connection = ds.getConnection();
try
{
StatusClient client = new StatusClient(connection);
Status status = client.getStatus();
assertFalse(status.isOK());
assertEquals(status.getServerStatus(), "degraded");
assertTrue(status.getServerAlerts().contains("low-disk-space-error"));
assertFalse(status.isOK());
}
finally
{
connection.close();
}
}
示例10: dnFromUsername
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
private String dnFromUsername(String username) throws LDAPException, GeneralSecurityException {
String baseDN = config.getUserBaseDN();
String lookup = String.format("(%s=%s)", config.getUserAttribute(), username);
SearchRequest searchRequest = new SearchRequest(baseDN, SearchScope.SUB, lookup);
LDAPConnection connection = connectionFactory.getLDAPConnection();
try {
SearchResult sr = connection.search(searchRequest);
if (sr.getEntryCount() == 0) {
throw new LDAPException(ResultCode.INVALID_CREDENTIALS);
}
return sr.getSearchEntries().get(0).getDN();
} finally {
connection.close();
}
}
示例11: authenticateConnectionPoolImpl
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
private boolean authenticateConnectionPoolImpl(final String bindDn, final String password) throws LDAPException, ConnectionException {
boolean loggedIn = false;
if (bindDn == null) {
return loggedIn;
}
boolean closeConnection = false;
LDAPConnection connection = connectionProvider.getConnection();
try {
closeConnection = true;
BindResult r = connection.bind(bindDn, password);
if (r.getResultCode() == ResultCode.SUCCESS) {
loggedIn = true;
}
} finally {
connectionProvider.releaseConnection(connection);
// We can't use connection which binded as ordinary user
if (closeConnection) {
connectionProvider.closeDefunctConnection(connection);
}
}
return loggedIn;
}
示例12: scrollSimplePagedResultsControl
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
private ASN1OctetString scrollSimplePagedResultsControl(LDAPConnection ldapConnection, String dn, Filter filter, SearchScope scope, Control[] controls, int startIndex) throws LDAPException, InvalidSimplePageControlException {
SearchRequest searchRequest = new SearchRequest(dn, scope, filter, "dn");
int currentStartIndex = startIndex;
ASN1OctetString cookie = null;
do {
int pageSize = Math.min(currentStartIndex, 100);
searchRequest.setControls(new Control[]{new SimplePagedResultsControl(pageSize, cookie, true)});
setControls(searchRequest, controls);
SearchResult searchResult = ldapConnection.search(searchRequest);
currentStartIndex -= searchResult.getEntryCount();
try {
SimplePagedResultsControl c = SimplePagedResultsControl.get(searchResult);
if (c != null) {
cookie = c.getCookie();
}
} catch (LDAPException ex) {
log.error("Error while accessing cookie", ex);
throw new InvalidSimplePageControlException(ex.getResultCode(), "Error while accessing cookie");
}
} while ((cookie != null) && (cookie.getValueLength() > 0) && (currentStartIndex > 0));
return cookie;
}
示例13: getLDAPConnectionsStatus
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
@Override
public Map<String, Object> getLDAPConnectionsStatus() {
Map<String, Object> connectionsStatus = new HashMap<>();
LDAPConnection connection = null;
for (LdapDataSourceConfiguration ldapDS : ldapDataSources) {
try {
connection = ldapDS.getLdapConnection();
// If a problem is detected that suggests that the provided
// connection is not suitable for use, LDAPException would be
// thrown.
ldapDS.getLdapConnectionPool().getHealthCheck().ensureConnectionValidForContinuedUse(connection);
connectionsStatus.put(ldapDS.getDatabaseName(), connection.isConnected());
} catch (LDAPException e) {
connectionsStatus.put(ldapDS.getDatabaseName(), e);
}
}
return connectionsStatus;
}
示例14: getTree
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
private String getTree() throws LDAPException, ConfigurationException,
SenderException {
LDAPConnection connection = inMemoryDirectoryServer.getConnection();
LdapSender ldapSender = null;
try {
ldapSender = new LdapSender();
ldapSender.setLdapProviderURL("ldap://"
+ connection.getConnectedAddress() + ":"
+ connection.getConnectedPort());
ldapSender.setOperation("getTree");
Parameter parameter = new Parameter();
parameter.setName("entryName");
parameter.setValue(baseDNs);
ldapSender.addParameter(parameter);
ldapSender.configure();
ldapSender.open();
return ldapSender.sendMessage("dummy", "dummy");
} finally {
if (ldapSender != null) {
ldapSender.close();
}
if (connection != null) {
connection.close();
}
}
}
示例15: createLdapEntries
import com.unboundid.ldap.sdk.LDAPConnection; //导入依赖的package包/类
/**
* Creates the given LDAP entries.
*
* @param connection Open LDAP connection used to connect to directory.
* @param entries Collection of LDAP entries.
*
* @throws Exception On LDAP errors.
*/
public static void createLdapEntries(final LDAPConnection connection, final Collection<LdapEntry> entries) throws Exception {
for (final LdapEntry entry : entries) {
final Collection<Attribute> attrs = new ArrayList<>(entry.getAttributeNames().length);
for (final LdapAttribute a : entry.getAttributes()) {
attrs.add(new Attribute(a.getName(), a.getStringValues()));
}
connection.add(new AddRequest(entry.getDn(), attrs));
}
}