|
發表者 |
討論內容 |
冷日 (冷日) |
發表時間:2021/8/11 8:23 |
- Webmaster

- 註冊日: 2008/2/19
- 來自:
- 發表數: 15771
|
- [轉貼]Java InitialLdapContext 類代碼示例
本文整理匯總了Java中javax.naming.ldap.InitialLdapContext類的典型用法代碼示例。如果您正苦於以下問題:Java InitialLdapContext類的具體用法?Java InitialLdapContext怎麽用?Java InitialLdapContext使用的例子?那麽恭喜您, 這裏精選的類代碼示例或許可以為您提供幫助。 InitialLdapContext類屬於javax.naming.ldap包,在下文中一共展示了InitialLdapContext類的20個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於我們的係統推薦出更棒的Java代碼示例。 示例1: getContextimport 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: initimport 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: setSchemaContextimport 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: bindDNReferralAuthenticationimport 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: testGetControlInstanceControlimport 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: testReconnect003import 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: testConnectControls3import 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: findAccountByAccountNameimport 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: testGetControlInstanceControlimport 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: testReconnect003import 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: setUpimport 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: getFullyQualifiedNameByUserIdimport 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: createContextimport 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: ActiveDirectoryimport 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: getCurrentSessionimport 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(); }
示例16: getDirContextInstanceimport javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類 @Override protected DirContext getDirContextInstance(Hashtable<String, Object> env) throws NamingException { String url = (String) env.get(Context.PROVIDER_URL); try { if (new URI(url).getScheme().equalsIgnoreCase("ldaps")) { env.put("java.naming.ldap.factory.socket", "cz.inqool.uas.security.ldap.ssl.DummySSLSocketFactory"); } } catch (URISyntaxException e) { log.error("LDAP URL {} is wrong", url, e); } return new InitialLdapContext(env, null); }
示例17: userListimport javax.naming.ldap.InitialLdapContext; //導入依賴的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; }
示例18: getByDnimport javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類 public Attributes getByDn(final String dn) throws NamingException { LdapContext ctx = new InitialLdapContext(env, null); Attributes result = ctx.getAttributes(dn); ctx.close(); return result; }
示例19: searchimport javax.naming.ldap.InitialLdapContext; //導入依賴的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; }
示例20: startKerberosimport javax.naming.ldap.InitialLdapContext; //導入依賴的package包/類 @SuppressWarnings("unused") private ApacheDS startKerberos() throws Exception { Preconditions.checkState(ldapServer.isStarted()); kdcServer.setDirectoryService(directoryService); // FIXME hard-coded ports kdcServer.setTransports(new TcpTransport(6088), new UdpTransport(6088)); kdcServer.setEnabled(true); kdcServer.setPrimaryRealm(realm); kdcServer.setSearchBaseDn(baseDn); kdcServer.setKdcPrincipal("krbtgt/" + realm + "@" + baseDn); kdcServer.start(); // ------------------------------------------------------------------- // Enable the krb5kdc schema // ------------------------------------------------------------------- Hashtable<String, Object> env = new Hashtable<String, Object>(); env.put(DirectoryService.JNDI_KEY, directoryService); env.put(Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName()); env.put(Context.PROVIDER_URL, ServerDNConstants.OU_SCHEMA_DN); InitialLdapContext schemaRoot = new InitialLdapContext(env, null); // check if krb5kdc is disabled Attributes krb5kdcAttrs = schemaRoot.getAttributes("cn=Krb5kdc"); boolean isKrb5KdcDisabled = false; if (krb5kdcAttrs.get("m-disabled") != null) { isKrb5KdcDisabled = ((String) krb5kdcAttrs.get("m-disabled").get()).equalsIgnoreCase("TRUE"); } // if krb5kdc is disabled then enable it if (isKrb5KdcDisabled) { Attribute disabled = new BasicAttribute("m-disabled"); ModificationItem[] mods = new ModificationItem[] {new ModificationItem(DirContext.REMOVE_ATTRIBUTE, disabled)}; schemaRoot.modifyAttributes("cn=Krb5kdc", mods); } return this; }
開發者ID:SonarQubeCommunity, 項目名稱:sonar-activedirectory, 代碼行數:39, 代碼來源: ApacheDS.java
注: 本文中的javax.naming.ldap.InitialLdapContext類示例整理自Github/MSDocs等源碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。 原文出處:Java InitialLdapContext類代碼示例 - 純淨天空
|
|
討論串
|