List of usage examples for javax.naming Context REFERRAL
String REFERRAL
To view the source code for javax.naming Context REFERRAL.
Click Source Link
From source file:ManageReferral.java
public static void main(String[] args) { // Set up environment for creating initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:489/o=JNDITutorial"); // env.put(Context.REFERRAL, "follow"); env.put(Context.REFERRAL, "ignore"); try {/*from ww w .j a va 2 s . com*/ // Create initial context LdapContext ctx = (LdapContext) new InitialLdapContext(env, null); // ctx.setRequestControls(new Control[] { // new ManageReferralControl() }); // Set controls for performing subtree search SearchControls ctls = new SearchControls(); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Perform search NamingEnumeration answer = ctx.search("", "(objectclass=*)", ctls); // Print the answer while (answer.hasMore()) { System.out.println(">>>" + ((SearchResult) answer.next()).getName()); } // Close the context when we're done ctx.close(); } catch (NamingException e) { e.printStackTrace(); } }
From source file:com.constellio.model.services.users.sync.FastBindConnectionControl.java
@SuppressWarnings("unchecked") public LDAPFastBind(String ldapurl, Boolean followReferences, boolean activeDirectory) { env = new Hashtable(); //This can make LDAP search slow : http://stackoverflow.com/questions/16412236/how-to-resolve-javax-naming-partialresultexception //env.put(Context.REFERRAL, "follow"); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.PROVIDER_URL, ldapurl); env.put("java.naming.ldap.attributes.binary", "tokenGroups objectSid"); if (followReferences) { env.put(Context.REFERRAL, "follow"); }/*from www . jav a 2 s . c o m*/ if (StringUtils.startsWith(ldapurl, "ldaps")) { //env.put(Context.SECURITY_PROTOCOL, "ssl"); env.put("java.naming.ldap.factory.socket", "com.constellio.model.services.users.sync.ldaps.DummySSLSocketFactory"); } if (activeDirectory) { connCtls = new Control[] { new FastBindConnectionControl() }; } else { connCtls = new Control[] {}; } //first time we initialize the context, no credentials are supplied //therefore it is an anonymous bind. /*try { ctx = new InitialLdapContext(env, connCtls); } catch (NamingException e) { throw new RuntimeNamingException(e.getMessage()); }*/ //FIX de Vincent pour o a q try { ctx = new InitialLdapContext(env, connCtls); } catch (NamingException e) { if (activeDirectory) { connCtls = new Control[] {}; try { ctx = new InitialLdapContext(env, connCtls); } catch (NamingException e2) { throw new RuntimeException(e); } } else { throw new RuntimeException(e); } } }
From source file:com.jaeksoft.searchlib.util.ActiveDirectory.java
public ActiveDirectory(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"); properties.put(Context.PROVIDER_URL, StringUtils.fastConcat("LDAP://", domain)); properties.put(Context.SECURITY_PRINCIPAL, StringUtils.fastConcat(username, "@", domain)); properties.put(Context.SECURITY_CREDENTIALS, password); properties.put("java.naming.ldap.attributes.binary", "objectSID"); properties.put(Context.REFERRAL, "follow"); dirContext = new InitialDirContext(properties); domainSearchName = getDomainSearch(domain); }
From source file:CreateJavaSchema.java
/** * Signs on to directory server using parameters supplied to program. * @return The initial context to the server. *///w ww .j a va2 s . co m private DirContext signOn() throws NamingException { if (dn != null && auth == null) { auth = "simple"; // use simple for Netscape } Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.REFERRAL, "follow"); if (auth != null) { env.put(Context.SECURITY_AUTHENTICATION, auth); env.put(Context.SECURITY_PRINCIPAL, dn); env.put(Context.SECURITY_CREDENTIALS, passwd); } // Workaround for Netscape schema bugs if (netscapebug) { env.put("com.sun.naming.netscape.schemaBugs", "true"); } // LDAP protocol tracing if (traceLdap) { env.put("com.sun.jndi.ldap.trace.ber", System.err); } return new InitialDirContext(env); }
From source file:ldap.SearchUtility.java
/** * open the directory connection.//w w w. j a v a 2s . c o m * * @param url * @param tracing * @return * @throws javax.naming.NamingException */ private DirContext setupJNDIConnection(String url, String userDN, String password, boolean tracing) throws NamingException { /* * First, set up a large number of environment variables to sensible default valuse */ Hashtable env = new Hashtable(); // sanity check if (url == null) throw new NamingException("URL not specified in openContext()!"); // set the tracing level now, since it can't be set once the connection is open. if (tracing) env.put("com.sun.jndi.ldap.trace.ber", System.err); // echo trace to standard error output env.put("java.naming.ldap.version", "3"); // always use ldap v3 - v2 too limited env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // use default jndi provider env.put("java.naming.ldap.deleteRDN", "false"); // usually what we want env.put(Context.REFERRAL, "ignore"); //could be: follow, ignore, throw env.put("java.naming.ldap.derefAliases", "finding"); // could be: finding, searching, etc. env.put(Context.SECURITY_AUTHENTICATION, "simple"); // 'simple' = username + password env.put(Context.SECURITY_PRINCIPAL, userDN); // add the full user dn env.put(Context.SECURITY_CREDENTIALS, password); // stupid jndi requires us to cast this to a string- env.put(Context.PROVIDER_URL, url); // the ldap url to connect to; e.g. "ldap://ca.com:389" /* * Open the actual LDAP session using the above environment variables */ DirContext newContext = new InitialDirContext(env); if (newContext == null) throw new NamingException( "Internal Error with jndi connection: No Context was returned, however no exception was reported by jndi."); return newContext; }
From source file:com.nridge.core.app.ldap.ADQuery.java
/** * Opens a connection to Active Directory by establishing an initial LDAP * context. The security principal and credentials are assigned the * account name and password parameters. * * @param anAcountDN Active Directory account name (DN format). * @param anAccountPassword Active Directory account password. * * @throws NSException Thrown if an LDAP naming exception is occurs. *//*w ww . j a va 2s. c o m*/ @SuppressWarnings("unchecked") public void open(String anAcountDN, String anAccountPassword) throws NSException { Logger appLogger = mAppMgr.getLogger(this, "open"); appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER); // LDAP Reference - http://docs.oracle.com/javase/1.5.0/docs/guide/jndi/jndi-ldap-gl.html Hashtable<String, String> environmentalVariables = new Hashtable<String, String>(); environmentalVariables.put("com.sun.jndi.ldap.connect.pool", StrUtl.STRING_TRUE); environmentalVariables.put(Context.PROVIDER_URL, getPropertyValue("domain_url", null)); environmentalVariables.put("java.naming.ldap.attributes.binary", "tokenGroups objectSid"); environmentalVariables.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); environmentalVariables.put(Context.SECURITY_PRINCIPAL, anAcountDN); environmentalVariables.put(Context.SECURITY_CREDENTIALS, anAccountPassword); // Referral options: follow, throw, ignore (default) environmentalVariables.put(Context.REFERRAL, getPropertyValue("referral_handling", "ignore")); // Authentication options: simple, DIGEST-MD5 CRAM-MD5 environmentalVariables.put(Context.SECURITY_AUTHENTICATION, getPropertyValue("authentication", "simple")); try { mLdapContext = new InitialLdapContext(environmentalVariables, null); } catch (NamingException e) { String msgStr = String.format("LDAP Context Error: %s", e.getMessage()); appLogger.error(msgStr, e); throw new NSException(msgStr); } appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART); }
From source file:ldap.LdapApi.java
/** * open the directory connection./*from ww w .j a va 2s. c o m*/ * @param url * @param dn * @param password * @param tracing * @return DirContext - context * @throws NamingException */ private DirContext setupJNDIConnection(String url, String userDN, String password, boolean tracing) throws NamingException { /* * setup environment variables to sensible default valuse */ Hashtable env = new Hashtable(); // sanity check if (url == null) { throw new LdapException("URL not specified in openContext()!"); } // tracing on/off, since it can't be set once the connection is open. if (tracing) { env.put("com.sun.jndi.ldap.trace.ber", System.err); // echo trace to standard error output } //env.put("java.naming.ldap.version", "3"); // always use ldap v3 - v2 too limited env.put(LdapConstants.ldapVersionStr, LdapConstants.ldapVersion); // always use ldap v3 - v2 too limited env.put(Context.INITIAL_CONTEXT_FACTORY, LdapConstants.ldapContext); // use default jndi provider env.put(LdapConstants.ldapDeleteRdn, LdapConstants.ldapDeleteRdnValue); // usually what we want //env.put(Context.REFERRAL, "ignore"); //could be: follow, ignore, throw env.put(Context.REFERRAL, LdapConstants.ldapIgnore); //could be: follow, ignore, throw // env.put("java.naming.ldap.derefAliases", "finding"); // could be: finding, searching, etc. env.put(LdapConstants.ldapFindingAliases, LdapConstants.ldapFindingStr); // could be: finding, searching, etc. //env.put(Context.SECURITY_AUTHENTICATION, "simple"); // 'simple' = username + password env.put(Context.SECURITY_AUTHENTICATION, LdapConstants.ldapSecurityAuth); // 'simple' = username + password env.put(Context.SECURITY_PRINCIPAL, userDN); // add the full user dn env.put(Context.SECURITY_CREDENTIALS, password); // stupid jndi requires us to cast this to a string- env.put(Context.PROVIDER_URL, url); // the ldap url to connect to; e.g. "ldap://ca.com:389" /* * Open the actual LDAP session using the above environment variables */ context = new InitialDirContext(env); if (context == null) { throw new NamingException( "Internal Error with jndi connection: No Context was returned, however no exception was reported by jndi."); } else { logger.info("context is not null"); } return context; }
From source file:com.nridge.core.app.ldap.ADQuery.java
/** * Returns <i>true</i> if the Active Directory account and password are * valid (e.g. a context can be successfully established) or <i>false</i> * otherwise.//from w ww . j a va2 s. c o m * * @param anAccountName An Active Directory account name. * @param anAccountPassword An Active Directory account passowrd. * * @return <i>true</i> or <i>false</i> */ @SuppressWarnings("unchecked") public boolean isAccountValid(String anAccountName, String anAccountPassword) { boolean isValid = false; Logger appLogger = mAppMgr.getLogger(this, "isAccountValid"); appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER); DataBag userBag = schemaUserBag(); userBag.setValueByName(LDAP_ACCOUNT_NAME, anAccountName); try { loadUserByAccountName(userBag); Hashtable<String, String> environmentalVariables = new Hashtable<String, String>(); environmentalVariables.put("com.sun.jndi.ldap.connect.pool", StrUtl.STRING_TRUE); environmentalVariables.put(Context.PROVIDER_URL, getPropertyValue("domain_url", null)); environmentalVariables.put("java.naming.ldap.attributes.binary", "tokenGroups objectSid"); environmentalVariables.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); environmentalVariables.put(Context.SECURITY_PRINCIPAL, userBag.getValueAsString(LDAP_DISTINGUISHED_NAME)); environmentalVariables.put(Context.SECURITY_CREDENTIALS, anAccountPassword); environmentalVariables.put(Context.REFERRAL, getPropertyValue("referral_handling", "ignore")); environmentalVariables.put(Context.SECURITY_AUTHENTICATION, getPropertyValue("authentication", "simple")); LdapContext ldapContext = new InitialLdapContext(environmentalVariables, null); ldapContext.close(); isValid = true; } catch (Exception ignored) { } appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART); return isValid; }
From source file:eu.uqasar.util.ldap.LdapManager.java
private LdapContext getConnection(LdapSettings settings, final String userName, final String password) throws CommunicationException, NamingException { Validate.notEmpty(settings.getAuthUserDN()); // bind by using the specified username/password Properties props = new Properties(); props.put(Context.SECURITY_PRINCIPAL, userName == null ? settings.getAuthUserDN() : userName); if (settings.getAuthUserPassword() != null || password != null) { props.put(Context.SECURITY_CREDENTIALS, password == null ? settings.getAuthUserPassword() : password); }/* w w w.j a v a2 s . c o m*/ // ensures that objectSID attribute values // will be returned as a byte[] instead of a String props.put("java.naming.ldap.attributes.binary", "objectSID"); // the following is helpful in debugging errors // props.put("com.sun.jndi.ldap.trace.ber", System.err); String ldapURL = String.format("ldap://%s:%s", settings.getHost(), settings.getPort()); props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); props.put(Context.PROVIDER_URL, ldapURL); props.put(Context.REFERRAL, "follow"); try { return new InitialLdapContext(props, null); } catch (CommunicationException e) { logger.warn(String.format("Failed to connect to %s:%s", settings.getHost(), settings.getPort()), e); throw e; } catch (NamingException e) { logger.warn(String.format("Failed to authenticate %s:%s", settings.getHost(), settings.getPort()), e); throw e; } }
From source file:com.openkm.principal.LdapPrincipalAdapter.java
/** * Create static LDAP configuration environment. *///from ww w . jav a 2 s .c om private static Hashtable<String, String> getEnvironment() { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.PROVIDER_URL, Config.PRINCIPAL_LDAP_SERVER); // Enable connection pooling // @see http://docs.oracle.com/javase/jndi/tutorial/ldap/connect/pool.html env.put("com.sun.jndi.ldap.connect.pool", "true"); /** * Referral values: ignore, follow or throw. * * @see http://docs.oracle.com/javase/jndi/tutorial/ldap/referral/jndi.html * @see http://java.sun.com/products/jndi/jndi-ldap-gl.html */ if (!"".equals(Config.PRINCIPAL_LDAP_REFERRAL)) { env.put(Context.REFERRAL, Config.PRINCIPAL_LDAP_REFERRAL); } // Optional is some cases (Max OS/X) if (!Config.PRINCIPAL_LDAP_SECURITY_PRINCIPAL.equals("")) { env.put(Context.SECURITY_PRINCIPAL, Config.PRINCIPAL_LDAP_SECURITY_PRINCIPAL); } if (!Config.PRINCIPAL_LDAP_SECURITY_CREDENTIALS.equals("")) { env.put(Context.SECURITY_CREDENTIALS, Config.PRINCIPAL_LDAP_SECURITY_CREDENTIALS); } return env; }