private DirContext doGetContext(String principal, String credentials, boolean explicitlyDisablePooling) { Hashtable<String, Object> env = getAuthenticatedEnv(principal, credentials); if(explicitlyDisablePooling) { env.remove(SUN_LDAP_POOLING_FLAG); } DirContext ctx = createContext(env); try { DirContext processedDirContext = authenticationStrategy.processContextAfterCreation(ctx, principal, credentials); return processedDirContext; } catch (NamingException e) { closeContext(ctx); throw LdapUtils.convertLdapException(e); } }
public DirContext getReadOnlyContext() { if (!anonymousReadOnly) { return doGetContext( authenticationSource.getPrincipal(), authenticationSource.getCredentials(), DONT_DISABLE_POOLING); } else { return createContext(getAnonymousEnv()); } }
protected Hashtable<String, Object> getAuthenticatedEnv(String principal, String credentials) { // The authenticated environment should always be rebuilt. Hashtable<String, Object> env = new Hashtable<String, Object>(getAnonymousEnv()); setupAuthenticatedEnvironment(env, principal, credentials); return env; }
/** * Create a DirContext using the supplied environment. * * @param environment the LDAP environment to use when creating the * <code>DirContext</code>. * @return a new DirContext implementation initialized with the supplied * environment. */ protected DirContext createContext(Hashtable<String, Object> environment) { DirContext ctx = null; try { ctx = getDirContextInstance(environment); if (LOG.isInfoEnabled()) { Hashtable<?, ?> ctxEnv = ctx.getEnvironment(); String ldapUrl = (String) ctxEnv.get(Context.PROVIDER_URL); LOG.debug("Got Ldap context on server '" + ldapUrl + "'"); } return ctx; } catch (NamingException e) { closeContext(ctx); throw LdapUtils.convertLdapException(e); } }
/** * Assemble a valid url String from all registered urls to add as * <code>PROVIDER_URL</code> to the environment. * * @param ldapUrls all individual url Strings. * @return the full url String */ public String assembleProviderUrlString(String[] ldapUrls) { StringBuilder providerUrlBuffer = new StringBuilder(DEFAULT_BUFFER_SIZE); for (String ldapUrl : ldapUrls) { providerUrlBuffer.append(ldapUrl); if (!base.isEmpty()) { if (!ldapUrl.endsWith("/")) { providerUrlBuffer.append("/"); } } providerUrlBuffer.append(formatForUrl(base)); providerUrlBuffer.append(' '); } return providerUrlBuffer.toString().trim(); }
public DirContext getContext(String principal, String credentials) { // This method is typically called for authentication purposes, which means that we // should explicitly disable pooling in case passwords are changed (LDAP-183). return doGetContext(principal, credentials, EXPLICITLY_DISABLE_POOLING); }
@Override public String getBaseLdapPathAsString() { return getBaseLdapName().toString(); }
@SuppressWarnings("deprecation") private Hashtable<String, Object> setupAnonymousEnv() { if (pooled) { baseEnv.put(SUN_LDAP_POOLING_FLAG, "true"); LOG.debug("Using LDAP pooling."); } else { baseEnv.remove(SUN_LDAP_POOLING_FLAG); LOG.debug("Not using LDAP pooling"); } Hashtable<String, Object> env = new Hashtable<String, Object>(baseEnv); env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory.getName()); env.put(Context.PROVIDER_URL, assembleProviderUrlString(urls)); if (dirObjectFactory != null) { env.put(Context.OBJECT_FACTORIES, dirObjectFactory.getName()); } if (StringUtils.hasText(referral)) { env.put(Context.REFERRAL, referral); } if (!base.isEmpty()) { // Save the base path for use in the DefaultDirObjectFactory. env.put(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY, base); } LOG.debug("Trying provider Urls: " + assembleProviderUrlString(urls)); return env; }
/** * Set the ContextSource to work on. Even though the actual ContextSource * sent to the LdapTemplate instance should be a * {@link TransactionAwareContextSourceProxy}, the one sent to this method * should be the target of that proxy. If it is not, the target will be * extracted and used instead. * * @param contextSource * the ContextSource to work on. */ public void setContextSource(ContextSource contextSource) { if (contextSource instanceof TransactionAwareContextSourceProxy) { TransactionAwareContextSourceProxy proxy = (TransactionAwareContextSourceProxy) contextSource; this.contextSource = proxy.getTarget(); } else { this.contextSource = contextSource; } if (contextSource instanceof AbstractContextSource) { AbstractContextSource abstractContextSource = (AbstractContextSource) contextSource; if(abstractContextSource.isAnonymousReadOnly()) { throw new IllegalArgumentException( "Compensating LDAP transactions cannot be used when context-source is anonymous-read-only"); } } }
protected Hashtable<String, Object> getAuthenticatedEnv(String principal, String credentials) { // The authenticated environment should always be rebuilt. Hashtable<String, Object> env = new Hashtable<String, Object>(getAnonymousEnv()); setupAuthenticatedEnvironment(env, principal, credentials); return env; }
/** * Create a DirContext using the supplied environment. * * @param environment the LDAP environment to use when creating the * <code>DirContext</code>. * @return a new DirContext implementation initialized with the supplied * environment. */ protected DirContext createContext(Hashtable<String, Object> environment) { DirContext ctx = null; try { ctx = getDirContextInstance(environment); if (LOG.isInfoEnabled()) { Hashtable<?, ?> ctxEnv = ctx.getEnvironment(); String ldapUrl = (String) ctxEnv.get(Context.PROVIDER_URL); LOG.debug("Got Ldap context on server '" + ldapUrl + "'"); } return ctx; } catch (NamingException e) { closeContext(ctx); throw LdapUtils.convertLdapException(e); } }
/** * Assemble a valid url String from all registered urls to add as * <code>PROVIDER_URL</code> to the environment. * * @param ldapUrls all individual url Strings. * @return the full url String */ public String assembleProviderUrlString(String[] ldapUrls) { StringBuilder providerUrlBuffer = new StringBuilder(DEFAULT_BUFFER_SIZE); for (String ldapUrl : ldapUrls) { providerUrlBuffer.append(ldapUrl); if (!base.isEmpty()) { if (!ldapUrl.endsWith("/")) { providerUrlBuffer.append("/"); } } providerUrlBuffer.append(formatForUrl(base)); providerUrlBuffer.append(' '); } return providerUrlBuffer.toString().trim(); }
public DirContext getContext(String principal, String credentials) { // This method is typically called for authentication purposes, which means that we // should explicitly disable pooling in case passwords are changed (LDAP-183). return doGetContext(principal, credentials, EXPLICITLY_DISABLE_POOLING); }
@Override public String getBaseLdapPathAsString() { return getBaseLdapName().toString(); }
@SuppressWarnings("deprecation") private Hashtable<String, Object> setupAnonymousEnv() { if (pooled) { baseEnv.put(SUN_LDAP_POOLING_FLAG, "true"); LOG.debug("Using LDAP pooling."); } else { baseEnv.remove(SUN_LDAP_POOLING_FLAG); LOG.debug("Not using LDAP pooling"); } Hashtable<String, Object> env = new Hashtable<String, Object>(baseEnv); env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory.getName()); env.put(Context.PROVIDER_URL, assembleProviderUrlString(urls)); if (dirObjectFactory != null) { env.put(Context.OBJECT_FACTORIES, dirObjectFactory.getName()); } if (StringUtils.hasText(referral)) { env.put(Context.REFERRAL, referral); } if (!base.isEmpty()) { // Save the base path for use in the DefaultDirObjectFactory. env.put(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY, base); } LOG.debug("Trying provider Urls: " + assembleProviderUrlString(urls)); return env; }
/** * Set the ContextSource to work on. Even though the actual ContextSource * sent to the LdapTemplate instance should be a * {@link TransactionAwareContextSourceProxy}, the one sent to this method * should be the target of that proxy. If it is not, the target will be * extracted and used instead. * * @param contextSource * the ContextSource to work on. */ public void setContextSource(ContextSource contextSource) { if (contextSource instanceof TransactionAwareContextSourceProxy) { TransactionAwareContextSourceProxy proxy = (TransactionAwareContextSourceProxy) contextSource; this.contextSource = proxy.getTarget(); } else { this.contextSource = contextSource; } if (contextSource instanceof AbstractContextSource) { AbstractContextSource abstractContextSource = (AbstractContextSource) contextSource; if(abstractContextSource.isAnonymousReadOnly()) { throw new IllegalArgumentException( "Compensating LDAP transactions cannot be used when context-source is anonymous-read-only"); } } }
public DirContext getReadOnlyContext() { if (!anonymousReadOnly) { return doGetContext( authenticationSource.getPrincipal(), authenticationSource.getCredentials(), DONT_DISABLE_POOLING); } else { return createContext(getAnonymousEnv()); } }
private DirContext doGetContext(String principal, String credentials, boolean explicitlyDisablePooling) { Hashtable<String, Object> env = getAuthenticatedEnv(principal, credentials); if(explicitlyDisablePooling) { env.remove(SUN_LDAP_POOLING_FLAG); } DirContext ctx = createContext(env); try { DirContext processedDirContext = authenticationStrategy.processContextAfterCreation(ctx, principal, credentials); return processedDirContext; } catch (NamingException e) { closeContext(ctx); throw LdapUtils.convertLdapException(e); } }
protected Hashtable<String, Object> getAuthenticatedEnv(String principal, String credentials) { // The authenticated environment should always be rebuilt. Hashtable<String, Object> env = new Hashtable<String, Object>(getAnonymousEnv()); setupAuthenticatedEnvironment(env, principal, credentials); return env; }
/** * Create a DirContext using the supplied environment. * * @param environment the LDAP environment to use when creating the * <code>DirContext</code>. * @return a new DirContext implementation initialized with the supplied * environment. */ protected DirContext createContext(Hashtable<String, Object> environment) { DirContext ctx = null; try { ctx = getDirContextInstance(environment); if (LOG.isInfoEnabled()) { Hashtable<?, ?> ctxEnv = ctx.getEnvironment(); String ldapUrl = (String) ctxEnv.get(Context.PROVIDER_URL); LOG.debug("Got Ldap context on server '" + ldapUrl + "'"); } return ctx; } catch (NamingException e) { closeContext(ctx); throw LdapUtils.convertLdapException(e); } }