@Bean public RememberMeServices rememberMeServices(String internalSecretKey) { LdapContextSource ldapContext = getLdapContext(); String searchBase = "OU=Users,DC=test,DC=company,DC=com"; String searchFilter = "(&(objectClass=user)(sAMAccountName={0}))"; FilterBasedLdapUserSearch search = new FilterBasedLdapUserSearch(searchBase, searchFilter, ldapContext); search.setSearchSubtree(true); LdapUserDetailsService rememberMeUserDetailsService = new LdapUserDetailsService(search); rememberMeUserDetailsService.setUserDetailsMapper(new CustomUserDetailsServiceImpl()); InMemoryTokenRepositoryImpl rememberMeTokenRepository = new InMemoryTokenRepositoryImpl(); PersistentTokenBasedRememberMeServices services = new PersistentTokenBasedRememberMeServices(internalSecretKey, rememberMeUserDetailsService, rememberMeTokenRepository); services.setAlwaysRemember(true); return services; } @Bean public LdapContextSource getLdapContext() { LdapContextSource source = new LdapContextSource(); source.setUserDn("user@"+DOMAIN); source.setPassword("password"); source.setUrl(URL); return source; }
final LdapContextSource context = new LdapContextSource(); context.setAnonymousReadOnly(true); break; default: final String password = configurationContext.getProperty("Manager Password"); context.setUserDn(userDn); context.setPassword(password); context.setAuthenticationStrategy(new SimpleDirContextAuthenticationStrategy()); break; case LDAPS: context.setAuthenticationStrategy(new SimpleDirContextAuthenticationStrategy()); context.setAuthenticationStrategy(tlsAuthenticationStrategy); break; context.setReferral(referralStrategy.getValue()); context.setUrls(StringUtils.split(urls)); context.setBaseEnvironmentProperties(baseEnvironment); context.afterPropertiesSet(); authenticator.afterPropertiesSet(); } catch (final Exception e) {
LdapContextSource buildLdapContextSource(LdapServerInventory inv, Map<String, Object> baseEnvironmentProperties) { LdapContextSource ldapContextSource; ldapContextSource = new LdapContextSource(); ldapContextSource.setUrl(inv.getUrl()); ldapContextSource.setBase(inv.getBase()); ldapContextSource.setUserDn(inv.getUsername()); ldapContextSource.setPassword(inv.getPassword()); ldapContextSource.setDirObjectFactory(DefaultDirObjectFactory.class); if (inv.getEncryption().equals(LdapEncryptionType.TLS.toString())) { setTls(ldapContextSource); } ldapContextSource.setCacheEnvironmentProperties(false); ldapContextSource.setPooled(false); ldapContextSource.setReferral("follow"); if (baseEnvironmentProperties != null && !baseEnvironmentProperties.isEmpty()) { ldapContextSource.setBaseEnvironmentProperties(baseEnvironmentProperties); } try { ldapContextSource.afterPropertiesSet(); logger.info("Test LDAP Context Source loaded "); } catch (Exception e) { logger.error("Test LDAP Context Source not loaded ", e); throw new CloudRuntimeException("Test LDAP Context Source not loaded", e); } return ldapContextSource; }
@Bean @ConditionalOnMissingBean public ContextSource ldapContextSource() { LdapContextSource source = new LdapContextSource(); source.setUserDn(this.properties.getUsername()); source.setPassword(this.properties.getPassword()); source.setAnonymousReadOnly(this.properties.getAnonymousReadOnly()); source.setBase(this.properties.getBase()); source.setUrls(this.properties.determineUrls(this.environment)); source.setBaseEnvironmentProperties( Collections.unmodifiableMap(this.properties.getBaseEnvironment())); return source; }
protected LdapContextSource createLdapContextSource(RestLdapConfig ldapConfig) { LdapContextSource ldapContextSource = new LdapContextSource(); ldapContextSource.setBase(ldapConfig.getLdapBase()); List<String> ldapUrls = ldapConfig.getLdapUrls(); ldapContextSource.setUrls(ldapUrls.toArray(new String[ldapUrls.size()])); ldapContextSource.setUserDn(ldapConfig.getLdapUser()); ldapContextSource.setPassword(ldapConfig.getLdapPassword()); ldapContextSource.afterPropertiesSet(); return ldapContextSource; }
LdapContextSource targetContextSource = new LdapContextSource(); if (baseOnTarget) { targetContextSource.setBase(defaultPartitionSuffix); targetContextSource.setUrl("ldap://localhost:" + port); targetContextSource.setUserDn(principal); targetContextSource.setPassword(password); targetContextSource.setDirObjectFactory(dirObjectFactory); targetContextSource.setPooled(pooled); targetContextSource.setAuthenticationSource(authenticationSource); targetContextSource.afterPropertiesSet();
@Test public void testGetAnonymousEnvWhenCacheIsOff() throws Exception { tested.setBase("dc=example,dc=se"); tested.setUrl("ldap://ldap.example.com:389"); tested.setPooled(true); tested.setUserDn("cn=Some User"); tested.setPassword("secret"); tested.setCacheEnvironmentProperties(false); tested.afterPropertiesSet(); Hashtable env = tested.getAnonymousEnv(); assertThat(env.get(Context.PROVIDER_URL)).isEqualTo("ldap://ldap.example.com:389/dc=example,dc=se"); assertThat(env.get(LdapContextSource.SUN_LDAP_POOLING_FLAG)).isEqualTo("true"); assertThat(env.get(Context.SECURITY_PRINCIPAL)).isNull(); assertThat(env.get(Context.SECURITY_CREDENTIALS)).isNull(); tested.setUrl("ldap://ldap2.example.com:389"); env = tested.getAnonymousEnv(); assertThat(env.get(Context.PROVIDER_URL)).isEqualTo("ldap://ldap2.example.com:389/dc=example,dc=se"); } }
final LdapContextSource ctxSource = new LdapContextSource(); ctxSource.setPooled(config.isPooled()); ctxSource.setBaseEnvironmentProperties(config.getBaseEnvironmentSettings()); ctxSource.setPassword(config.getPassword()); ctxSource.setUrls(config.getUrls().toArray(new String[]{})); ctxSource.setUserDn(config.getUserDN()); ctxSource.setCacheEnvironmentProperties(config.isCacheEnvironmentProperties()); ctxSource.setReferral(config.getReferral());
private LdapContextSource getLdapContextSource() throws Exception { LdapContextSource ldapContextSource = new DefaultSpringSecurityContextSource( ldapURL); ldapContextSource.setUserDn(ldapBindDN); ldapContextSource.setPassword(ldapBindPassword); ldapContextSource.setReferral(ldapReferral); ldapContextSource.setCacheEnvironmentProperties(false); ldapContextSource.setAnonymousReadOnly(false); ldapContextSource.setPooled(true); ldapContextSource.afterPropertiesSet(); return ldapContextSource; }
/** * Spring context source for ldap connection * * @return Ldap context */ @Bean @ConditionalOnMissingBean public LdapContextSource contextSource() { LdapContextSource ldapContextSource = new LdapContextSource(); ldapContextSource.setUrls(ldapUrl); ldapContextSource.setBase(ldapBaseDN); ldapContextSource.setUserDn(ldapUserDN); ldapContextSource.setPassword(ldapPassword); ldapContextSource.setPooled(true); return ldapContextSource; }
@Test public void testGetAuthenticatedEnv() throws Exception { tested.setBase("dc=example,dc=se"); tested.setUrl("ldap://ldap.example.com:389"); tested.setPooled(true); tested.setUserDn("cn=Some User"); tested.setPassword("secret"); tested.afterPropertiesSet(); Hashtable env = tested.getAuthenticatedEnv("cn=Some User", "secret"); assertThat(env.get(Context.PROVIDER_URL)).isEqualTo("ldap://ldap.example.com:389/dc=example,dc=se"); assertThat(env.get(LdapContextSource.SUN_LDAP_POOLING_FLAG)).isEqualTo("true"); assertThat(env.get(Context.SECURITY_PRINCIPAL)).isEqualTo("cn=Some User"); assertThat(env.get(Context.SECURITY_CREDENTIALS)).isEqualTo("secret"); // check that base was added to environment assertThat(env.get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY)).isEqualTo(LdapUtils.newLdapName("dc=example,dc=se")); }
@Bean @DependsOn("directoryServer") @ConditionalOnMissingBean public ContextSource contextSource() { LdapContextSource contextSource = new LdapContextSource(); EmbeddedLdapProperties.Credential credential = this.embeddedProperties .getCredential(); if (StringUtils.hasText(credential.getUsername()) && StringUtils.hasText(credential.getPassword())) { contextSource.setUserDn(credential.getUsername()); contextSource.setPassword(credential.getPassword()); } contextSource.setUrls(this.properties.determineUrls(this.environment)); return contextSource; }
/** * Get authenticate LDAP template. * * @return LDAP template */ @Bean(name="authenticateLdapTemplate") public LdapTemplate getAuthenticateLdapTemplate() { LdapContextSource source = new LdapContextSource(); source.setUrl(System.getProperty("ldap.url")); source.setPooled(false); Map<String, Object> baseEnvironmentProperties = new HashMap<String, Object>(); baseEnvironmentProperties.put("com.sun.jndi.ldap.connect.timeout", System.getProperty("ldap.timeout")); source.setBaseEnvironmentProperties(baseEnvironmentProperties); source.afterPropertiesSet(); return new LdapTemplate(source); }
/** * This constructor creates a LdapCredentialsAuthenticator that authenticates against an LDAP server * that supports anonymous requests * * @param ldapHost the LDAP server host * @param ldapPort the LDAP server port * @param usersOuPath the path for the organizational unit under which users are found */ public LdapCredentialsAuthenticator(final String ldapHost, final int ldapPort, final String usersOuPath) { Assert.hasText(ldapHost, "Invalid ldapHost"); Assert.isTrue(ldapPort > 0); Assert.hasText(usersOuPath, "Invalid usersOuPath"); final LdapContextSource contextSource = new LdapContextSource(); contextSource.setAnonymousReadOnly(true); contextSource.setUrl("ldap://" + ldapHost + ":" + ldapPort); contextSource.setBase(usersOuPath); contextSource.afterPropertiesSet(); ldapTemplate = new LdapTemplate(contextSource); this.id = calculateId(ldapHost, ldapPort, usersOuPath); }
@Test public void testGetAnonymousEnvWithPoolingInBaseEnvironmentAndPoolingOff() throws Exception { tested.setUrl("ldap://ldap.example.com:389"); HashMap map = new HashMap(); map.put(LdapContextSource.SUN_LDAP_POOLING_FLAG, "true"); tested.setBaseEnvironmentProperties(map); tested.setPooled(false); tested.afterPropertiesSet(); Hashtable env = tested.getAnonymousEnv(); assertThat(env.get(Context.PROVIDER_URL)).isEqualTo("ldap://ldap.example.com:389"); assertThat(env.get(LdapContextSource.SUN_LDAP_POOLING_FLAG)).isNull(); }
@Test public void testPostProcessBeforeInitializationWithLdapPathAwareNoBasePathSet() throws Exception { final LdapContextSource expectedContextSource = new LdapContextSource(); String expectedPath = "dc=example, dc=com"; expectedContextSource.setBase(expectedPath); tested = new BaseLdapPathBeanPostProcessor() { BaseLdapPathSource getBaseLdapPathSourceFromApplicationContext() { return expectedContextSource; } }; Object result = tested.postProcessBeforeInitialization(ldapPathAwareMock, "someName"); verify(ldapPathAwareMock).setBaseLdapPath(new DistinguishedName(expectedPath)); assertThat(result).isSameAs(ldapPathAwareMock); }
@Test public void testGetAnonymousEnvWithEmptyBaseSet() throws Exception { tested.setUrl("ldap://ldap.example.com:389"); tested.setBase(null); tested.afterPropertiesSet(); Hashtable env = tested.getAnonymousEnv(); assertThat(env.get(Context.PROVIDER_URL)).isEqualTo("ldap://ldap.example.com:389"); // check that base was not added to environment assertThat(env.get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY)).isNull(); }
@Bean public LdapTemplate ldapTemplate() { if (!ObjectUtils.isEmpty(contextSource())) { return new LdapTemplate(contextSource()); } return new LdapTemplate(new LdapContextSource()); }
@Test public void testGetAnonymousEnvWithNoBaseSet() throws Exception { tested.setUrl("ldap://ldap.example.com:389"); tested.afterPropertiesSet(); Hashtable env = tested.getAnonymousEnv(); assertThat(env.get(Context.PROVIDER_URL)).isEqualTo("ldap://ldap.example.com:389"); // check that base was not added to environment assertThat(env.get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY)).isNull(); }
@Override public void afterPropertiesSet() throws Exception { super.afterPropertiesSet(); ldapContextSource.afterPropertiesSet(); if (apacheDsContainer != null) { apacheDsContainer.afterPropertiesSet(); } }