本文整理匯總了Java中javax.naming.ldap.InitialLdapContext類的典型用法代碼示例。如果您正苦於以下問題:Java InitialLdapContext類的具體用法?Java InitialLdapContext怎麽用?Java InitialLdapContext使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
InitialLdapContext類屬於javax.naming.ldap包,在下文中一共展示了InitialLdapContext類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getContext
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
private LdapContext getContext() throws Exception {
Hashtable<String, String> envDC = new Hashtable<String, String>();
envDC.put(
Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
envDC.put(
Context.PROVIDER_URL,
GlobalProperties.getInstance().getProperty("app.persones.plugin.ldap.url"));
envDC.put(
Context.SECURITY_AUTHENTICATION,
"simple");
envDC.put(
Context.SECURITY_PRINCIPAL,
GlobalProperties.getInstance().getProperty("app.persones.plugin.ldap.principal"));
envDC.put(
Context.SECURITY_CREDENTIALS,
GlobalProperties.getInstance().getProperty("app.persones.plugin.ldap.credentials"));
return new InitialLdapContext(envDC, null);
}
示例2: init
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
private void init()
throws Exception
{
@SuppressWarnings("UseOfObsoleteCollectionType")
Hashtable<String,String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, sdsUrl);
if (ConditionalCompilationControls.LDAPS && !ConditionalCompilationControls.OPENTEST) {
env.put(Context.SECURITY_PROTOCOL, "ssl");
env.put(Context.SECURITY_AUTHENTICATION, "none");
if (ConditionalCompilationControls.LDAPOVERTLS && !ConditionalCompilationControls.OPENTEST) {
env.put("java.naming.ldap.factory.socket", "org.warlock.spine.connection.SpineSecurityContext");
}
}
ldapContext = new InitialLdapContext(env, null);
}
示例3: setSchemaContext
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
private void setSchemaContext(KdcConfiguration configuration, DirectoryService service,
String connectionUser)
throws DirectoryServerException {
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(DirectoryService.JNDI_KEY, service);
env.put(Context.SECURITY_PRINCIPAL, connectionUser);
env.put(Context.SECURITY_CREDENTIALS, configuration.getSystemAdminPassword());
env.put(Context.SECURITY_AUTHENTICATION, ConfigurationConstants.SIMPLE_AUTHENTICATION);
env.put(Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName());
env.put(Context.PROVIDER_URL, SchemaConstants.OU_SCHEMA);
try {
schemaRoot = new InitialLdapContext(env, null);
} catch (NamingException e) {
throw new DirectoryServerException(
"Unable to create Schema context with user " + connectionUser, e);
}
}
示例4: bindDNReferralAuthentication
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
/**
* This method validates absoluteName and credential against referral LDAP and returns used user DN.
*
* <ol>
* <li> Parses given absoluteName to URL and DN
* <li> creates initial LDAP context of referral LDAP to validate credential
* <li> closes the initial context
* </ol>
*
* It uses all options from login module setup except of ProviderURL.
*
* @param userDN - userDN which has to be used instead of parsed absoluteName (if is null, use absoluteName) - value is gained using distinguishedNameAttribute
* @param absoluteName - absolute user DN
* @param credential
* @return used user DN for validation
* @throws NamingException
*/
private String bindDNReferralAuthentication(final String userDN, String absoluteName, Object credential)
throws NamingException
{
URI uri;
try {
uri = new URI(absoluteName);
}
catch (URISyntaxException e)
{
throw PicketBoxMessages.MESSAGES.unableToParseReferralAbsoluteName(e, absoluteName);
}
String name = (userDN != null ? userDN : uri.getPath().substring(1));
String namingProviderURL = uri.getScheme() + "://" + uri.getAuthority();
Properties refEnv = constructLdapContextEnvironment(namingProviderURL, name, credential);
InitialLdapContext refCtx = new InitialLdapContext(refEnv, null);
refCtx.close();
return name;
}
示例5: testGetControlInstanceControl
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
/**
* <p>Test method for 'javax.naming.ldap.ControlFactory.getControlInstance(Control)'</p>
* <p>Here we are going to test if we can get an instance with the controls sended.</p>
*/
public void testGetControlInstanceControl() {
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.harmony.jndi.tests.javax.naming.spi.mock.ldap.MockContextFactory");
MockControl[] cs = { new MockControl("c1", false, new byte[] { 1, 2, 3, 4 }),
new MockControl("c1", true, new byte[] { 'a', 'b', 'c', 'd' }), };
MockControl cs2 = new MockControl("c1", false, new byte[] { 1, 2, 3, 4 });
InitialLdapContext ilc=new InitialLdapContext(env, cs);
assertEquals(cs2,ControlFactory.getControlInstance(cs2,ilc,env));
} catch (NamingException e) {
}
}
示例6: testReconnect003
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
/**
* <p>
* Test method for
* 'javax.naming.ldap.InitialLdapContext.reconnect(Control[])'
* </p>
* <p>
* Here we are testing if this method correctly reconnects to the LDAP
* server. In this case we are using a different set of controls for the
* reconnection.
* </p>
* <p>
* The expected result is a reconection with the new set of controls.
* </p>
*/
public void testReconnect003() throws Exception {
System
.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.harmony.jndi.tests.javax.naming.spi.mock.ldap.MockContextFactory");
Control[] cs = {
new MockControl("c1", false, new byte[] { 1, 2, 3, 4 }),
new MockControl("c1", true, new byte[] { 'a', 'b', 'c', 'd' }), };
Control[] cs2 = {
new MockControl("c2", false, new byte[] { 1, 2, 3, 4 }),
new MockControl("c2", true, new byte[] { 'a', 'b', 'c', 'd' }), };
InitialLdapContext ilc = new InitialLdapContext(null, cs);
ilc.reconnect(cs2);
assertEquals(cs2, ilc.getConnectControls());
ilc.close();
}
示例7: testConnectControls3
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
public void testConnectControls3() throws Exception {
// set connect controls by InitialLdapContext
server.setResponseSeq(new LdapMessage[] { new LdapMessage(
LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
InitialLdapContext initialDirContext = new InitialLdapContext(env,
new Control[] { new SortControl("", Control.NONCRITICAL) });
server.setResponseSeq(new LdapMessage[] { new LdapMessage(
LdapASN1Constant.OP_SEARCH_RESULT_DONE,
new EncodableLdapResult(), null) });
LdapContext context = (LdapContext) initialDirContext.lookup("");
Control[] controls = context.getConnectControls();
assertNotNull(controls);
assertEquals(1, controls.length);
Control c = controls[0];
assertTrue(c instanceof SortControl);
assertEquals(Control.NONCRITICAL, c.isCritical());
}
示例8: findAccountByAccountName
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
/**
* Find account by account name.
*
* @param accountName the account name
* @return the search result
* @throws NamingException the naming exception
*/
protected SearchResult findAccountByAccountName(String accountName) throws NamingException {
String searchFilter = String.format(searchFilterPattern, accountName);
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
InitialLdapContext ctx = new InitialLdapContext(env, null);
try {
NamingEnumeration<SearchResult> results = ctx.search(searchBase, searchFilter, searchControls);
if (!results.hasMoreElements()) {
throw new UserConfigLoaderException("LDAP Search returned no accounts");
}
SearchResult searchResult = results.nextElement();
if (results.hasMoreElements()) {
throw new UserConfigLoaderException("More than one account found in ldap search");
}
return searchResult;
} finally {
ctx.close();
}
}
示例9: testGetControlInstanceControl
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
/**
* <p>Test method for 'javax.naming.ldap.ControlFactory.getControlInstance(Control)'</p>
* <p>Here we are gonna test if we can get an instance with the controls sended.</p>
*/
public void testGetControlInstanceControl() {
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.harmony.jndi.tests.javax.naming.spi.mock.ldap.MockContextFactory");
MockControl[] cs = { new MockControl("c1", false, new byte[] { 1, 2, 3, 4 }),
new MockControl("c1", true, new byte[] { 'a', 'b', 'c', 'd' }), };
MockControl cs2 = new MockControl("c1", false, new byte[] { 1, 2, 3, 4 });
InitialLdapContext ilc=new InitialLdapContext(env, cs);
assertEquals(cs2,ControlFactory.getControlInstance(cs2,ilc,env));
} catch (NamingException e) {
}
}
示例10: testReconnect003
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
/**
* <p>
* Test method for
* 'javax.naming.ldap.InitialLdapContext.reconnect(Control[])'
* </p>
* <p>
* Here we are testing if this method reconnects to the LDAP server using
* the supplied controls and this context's environment. In this case we are
* sending a new set of controls to reconection.
* </p>
* <p>
* The expected result is a reconection with the new set of controls.
* </p>
*/
public void testReconnect003() throws Exception {
System
.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.harmony.jndi.tests.javax.naming.spi.mock.ldap.MockContextFactory");
Control[] cs = {
new MockControl("c1", false, new byte[] { 1, 2, 3, 4 }),
new MockControl("c1", true, new byte[] { 'a', 'b', 'c', 'd' }), };
Control[] cs2 = {
new MockControl("c2", false, new byte[] { 1, 2, 3, 4 }),
new MockControl("c2", true, new byte[] { 'a', 'b', 'c', 'd' }), };
InitialLdapContext ilc = new InitialLdapContext(null, cs);
ilc.reconnect(cs2);
assertEquals(cs2, ilc.getConnectControls());
ilc.close();
}
示例11: setUp
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
protected void setUp() throws Exception {
super.setUp();
configuration = new MutableServerStartupConfiguration();
configuration.setWorkingDirectory(new File(workingDir));
cleanWorkingDir(configuration.getWorkingDirectory());
port = AvailablePortFinder.getNextAvailable(1024);
configuration.setLdapPort(port);
// configuration.setShutdownHookEnabled(false);
serverEnv = new Hashtable<String, Object>(configuration
.toJndiEnvironment());
initialAuth();
serverEnv.put(Context.INITIAL_CONTEXT_FACTORY,
ServerContextFactory.class.getName());
serverEnv.put(Context.PROVIDER_URL, "");
rootDSE = new InitialLdapContext(serverEnv, null);
}
示例12: getFullyQualifiedNameByUserId
import javax.naming.ldap.InitialLdapContext; //導入依賴的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());
}
}
示例13: createContext
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
public LdapContext createContext()
{
init();
final Hashtable<String, String> env;
final String authentication = ldapConfig.getAuthentication();
if ("none".equals(authentication) == false) {
env = createEnv(ldapConfig.getManagerUser(), ldapConfig.getManagerPassword());
} else {
env = createEnv(null, null);
}
try {
final LdapContext ctx = new InitialLdapContext(env, null);
return ctx;
} catch (final NamingException ex) {
log.error("While trying to connect LDAP initally: " + ex.getMessage(), ex);
throw new RuntimeException(ex);
}
}
示例14: ActiveDirectory
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
public ActiveDirectory(String serverName, String username, String password,
String domain) throws NamingException {
if (StringUtils.isEmpty(domain))
throw new NamingException("The domain is empty");
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
domainSearchName = getDomainSearch(domain);
String login = StringUtils.fastConcat(username, "@", domain);
if (serverName != null) {
properties.put(Context.PROVIDER_URL,
StringUtils.fastConcat("ldap://", serverName, ":389"));
}
properties.put(Context.SECURITY_PRINCIPAL, login);
properties.put(Context.SECURITY_CREDENTIALS, password);
properties.put(Context.REFERRAL, "follow");
properties.put("java.naming.ldap.attributes.binary", "objectSID");
dirContext = new InitialLdapContext(properties, null);
}
示例15: getCurrentSession
import javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類
/**
* Return the session which the current thread holds. If not exist, a new
* session will be created and bind to current thread.
*
* @return
*/
@Override
public Session getCurrentSession() {
Map.Entry<Session, InitialLdapContext> current = sessions.get();
if (null == current) {
try {
InitialLdapContext ldapConnection = getContext();
Session session = new SessionImpl(this, ldapConnection, true);
current = new AbstractMap.SimpleEntry<Session, InitialLdapContext>(
session, ldapConnection);
} catch (NamingException e) {
throw new ODMException("Cannot instantiate a session", e);
}
}
return current.getKey();
}