Java Code Examples for org.springframework.ldap.core.support.LdapContextSource#setBase()
The following examples show how to use
org.springframework.ldap.core.support.LdapContextSource#setBase() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LdapLoginProvider.java From cuba with Apache License 2.0 | 7 votes |
@PostConstruct protected void init() { if (webLdapConfig.getLdapEnabled()) { ldapContextSource = new LdapContextSource(); checkRequiredConfigProperties(webLdapConfig); ldapContextSource.setBase(webLdapConfig.getLdapBase()); List<String> ldapUrls = webLdapConfig.getLdapUrls(); ldapContextSource.setUrls(ldapUrls.toArray(new String[ldapUrls.size()])); ldapContextSource.setUserDn(webLdapConfig.getLdapUser()); ldapContextSource.setPassword(webLdapConfig.getLdapPassword()); ldapContextSource.afterPropertiesSet(); ldapTemplate = new LdapTemplate(ldapContextSource); ldapTemplate.setIgnorePartialResultException(true); } }
Example 2
Source File: LdapCredentialsAuthenticator.java From ob1k with Apache License 2.0 | 6 votes |
/** * 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); }
Example 3
Source File: LdapCredentialsAuthenticator.java From ob1k with Apache License 2.0 | 6 votes |
/** * This constructor creates a LdapCredentialsAuthenticator that authenticates against an LDAP server * that does not support 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 * @param userDn the distinguished name for the connection * @param password the password for the connection */ public LdapCredentialsAuthenticator(final String ldapHost, final int ldapPort, final String usersOuPath, final String userDn, final String password) { Assert.hasText(ldapHost, "Invalid ldapHost"); Assert.isTrue(ldapPort > 0); Assert.hasText(usersOuPath, "Invalid usersOuPath"); Assert.hasText(userDn, "Invalid userDn"); Assert.hasText(password, "Invalid password"); final LdapContextSource contextSource = new LdapContextSource(); contextSource.setUrl("ldap://" + ldapHost + ":" + ldapPort); contextSource.setBase(usersOuPath); contextSource.setUserDn(userDn); contextSource.setPassword(password); contextSource.afterPropertiesSet(); ldapTemplate = new LdapTemplate(contextSource); this.id = calculateId(ldapHost, ldapPort, usersOuPath); }
Example 4
Source File: LdapDataConfig.java From Spring-5.0-Projects with MIT License | 5 votes |
@Bean public ContextSource getLdapContextSrc() { LdapContextSource ldapContextSrc = new LdapContextSource(); ldapContextSrc.setUrl(ldapUrls); ldapContextSrc.setUserDn(ldapManagerUserName); ldapContextSrc.setPassword(ldapManagerPwd); ldapContextSrc.setBase(ldapBase); ldapContextSrc.afterPropertiesSet(); return ldapContextSrc; }
Example 5
Source File: GatekeeperCommonConfig.java From Gatekeeper with Apache License 2.0 | 5 votes |
@Bean @ConfigurationProperties(prefix = "spring.ldap.context-source") public LdapContextSource authContextSource() { LdapContextSource contextSource = new LdapContextSource(); contextSource.setBase(userBase); return contextSource; }
Example 6
Source File: LdapConfiguration.java From taskana with Apache License 2.0 | 5 votes |
@Bean @ConditionalOnMissingBean(LdapContextSource.class) public LdapContextSource ldapContextSource() { LdapContextSource contextSource = new LdapContextSource(); contextSource.setUrl(ldapServerUrl); contextSource.setBase(ldapBaseDn); contextSource.setUserDn(ldapBindDn); contextSource.setPassword(ldapBindPassword); return contextSource; }
Example 7
Source File: AuthConfiguration.java From apollo with Apache License 2.0 | 5 votes |
@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; }
Example 8
Source File: LdapOperationsTest.java From herd with Apache License 2.0 | 5 votes |
@Test public void testSearch() { // Create and initialize an LDAP context source. LdapContextSource contextSource = new LdapContextSource(); contextSource.setUrl(LDAP_URL); contextSource.setBase(LDAP_BASE); contextSource.setUserDn(LDAP_USER_DN); contextSource.setPassword(PASSWORD); contextSource.afterPropertiesSet(); // Create an LDAP template. LdapTemplate ldapTemplate = new LdapTemplate(contextSource); // Create an LDAP query. LdapQuery ldapQuery = query().where((String) ConfigurationValue.LDAP_ATTRIBUTE_USER_ID.getDefaultValue()).is(USER_ID); // Create a subject matter expert contact details mapper. SubjectMatterExpertDaoImpl.SubjectMatterExpertContactDetailsMapper subjectMatterExpertContactDetailsMapper = new SubjectMatterExpertDaoImpl.SubjectMatterExpertContactDetailsMapper((String) ConfigurationValue.LDAP_ATTRIBUTE_USER_FULL_NAME.getDefaultValue(), (String) ConfigurationValue.LDAP_ATTRIBUTE_USER_JOB_TITLE.getDefaultValue(), (String) ConfigurationValue.LDAP_ATTRIBUTE_USER_EMAIL_ADDRESS.getDefaultValue(), (String) ConfigurationValue.LDAP_ATTRIBUTE_USER_TELEPHONE_NUMBER.getDefaultValue()); // Gets information for the specified subject matter expert. List<SubjectMatterExpertContactDetails> result = ldapOperations.search(ldapTemplate, ldapQuery, subjectMatterExpertContactDetailsMapper); // Validate the results. assertEquals( Collections.singletonList(new SubjectMatterExpertContactDetails(USER_FULL_NAME, USER_JOB_TITLE, USER_EMAIL_ADDRESS, USER_TELEPHONE_NUMBER)), result); }
Example 9
Source File: TestContextSourceFactoryBean.java From spring-ldap with Apache License 2.0 | 5 votes |
protected ContextSource createInstance() throws Exception { LdapTestUtils.startEmbeddedServer(port, defaultPartitionSuffix, defaultPartitionName); if (contextSource == null) { // If not explicitly configured, create a new instance. 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); if (authenticationSource != null) { targetContextSource.setAuthenticationSource(authenticationSource); } targetContextSource.afterPropertiesSet(); contextSource = targetContextSource; } Thread.sleep(1000); if (baseOnTarget) { LdapTestUtils.clearSubContexts(contextSource, LdapUtils.emptyLdapName()); } else { LdapTestUtils.clearSubContexts(contextSource, LdapUtils.newLdapName(defaultPartitionSuffix)); } if (ldifFile != null) { LdapTestUtils.loadLdif(contextSource, ldifFile); } return contextSource; }
Example 10
Source File: TestContextSourceFactoryBean.java From spring-ldap with Apache License 2.0 | 5 votes |
protected Object createInstance() throws Exception { LdapTestUtils.startEmbeddedServer(port, defaultPartitionSuffix, defaultPartitionName); if (contextSource == null) { // If not explicitly configured, create a new instance. 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); if (authenticationSource != null) { targetContextSource.setAuthenticationSource(authenticationSource); } targetContextSource.afterPropertiesSet(); contextSource = targetContextSource; } Thread.sleep(1000); if (baseOnTarget) { LdapTestUtils.clearSubContexts(contextSource, LdapUtils.emptyLdapName()); } else { LdapTestUtils.clearSubContexts(contextSource, LdapUtils.newLdapName(defaultPartitionSuffix)); } if (ldifFile != null) { LdapTestUtils.loadLdif(contextSource, ldifFile); } return contextSource; }
Example 11
Source File: ContextSourceEc2InstanceLaunchingFactoryBean.java From spring-ldap with Apache License 2.0 | 5 votes |
@Override protected final Object doCreateInstance(final String dnsName) throws Exception { Assert.hasText(userDn); LdapContextSource instance = new LdapContextSource(); instance.setUrl("ldap://" + dnsName); instance.setUserDn(userDn); instance.setPassword(password); instance.setBase(base); instance.setPooled(pooled); setAdditionalContextSourceProperties(instance, dnsName); instance.afterPropertiesSet(); return instance; }
Example 12
Source File: ChoerodonAuthenticationProvider.java From oauth-server with Apache License 2.0 | 4 votes |
private boolean ldapAuthentication(Long organizationId, String loginName, String credentials) { LdapE ldap = ldapService.queryByOrgId(organizationId); if (ldap != null && ldap.getEnabled()) { LdapContextSource contextSource = new LdapContextSource(); String url = ldap.getServerAddress() + ":" + ldap.getPort(); int connectionTimeout = ldap.getConnectionTimeout(); contextSource.setUrl(url); contextSource.setBase(ldap.getBaseDn()); setConnectionTimeout(contextSource, connectionTimeout); contextSource.afterPropertiesSet(); LdapTemplate ldapTemplate = new LdapTemplate(contextSource); //ad目录不设置会报错 if (DirectoryType.MICROSOFT_ACTIVE_DIRECTORY.value().equals(ldap.getDirectoryType())) { ldapTemplate.setIgnorePartialResultException(true); } String userDn = null; boolean anonymousFetchFailed = false; AndFilter filter = getLoginFilter(ldap, loginName); try { List<String> names = ldapTemplate.search( query() .searchScope(SearchScope.SUBTREE) .filter(filter), new AbstractContextMapper() { @Override protected Object doMapFromContext(DirContextOperations ctx) { return ctx.getNameInNamespace(); } }); userDn = getUserDn(names, ldap.getLoginNameField(), loginName); } catch (Exception e) { anonymousFetchFailed = true; LOG.error("ldap anonymous search failed, filter {}, exception {}", filter, e); } if (anonymousFetchFailed) { userDn = accountAsUserDn2Authentication(loginName, ldap, contextSource, filter); } if (userDn == null) { LOG.error("can not get userDn by filter {}, login failed", filter); return false; } return authentication(credentials, contextSource, userDn); } else { throw new AuthenticationServiceException(LoginException.LDAP_IS_DISABLE.value()); } }
Example 13
Source File: SubjectMatterExpertDaoImpl.java From herd with Apache License 2.0 | 4 votes |
@Override public SubjectMatterExpertContactDetails getSubjectMatterExpertByKey(SubjectMatterExpertKey subjectMatterExpertKey) { // Get LDAP specific configuration settings. final String ldapUrl = configurationHelper.getProperty(ConfigurationValue.LDAP_URL); final String ldapBase = configurationHelper.getProperty(ConfigurationValue.LDAP_BASE); final String ldapUserDn = configurationHelper.getProperty(ConfigurationValue.LDAP_USER_DN); final String credStashEncryptionContext = configurationHelper.getProperty(ConfigurationValue.CREDSTASH_HERD_ENCRYPTION_CONTEXT); final String ldapUserCredentialName = configurationHelper.getProperty(ConfigurationValue.LDAP_USER_CREDENTIAL_NAME); // Log configuration values being used to create LDAP context source. LOGGER.info("Creating LDAP context source using the following parameters: {}=\"{}\" {}=\"{}\" {}=\"{}\" {}=\"{}\" {}=\"{}\"...", ConfigurationValue.LDAP_URL.getKey(), ldapUrl, ConfigurationValue.LDAP_BASE.getKey(), ldapBase, ConfigurationValue.LDAP_USER_DN.getKey(), ldapUserDn, ConfigurationValue.CREDSTASH_HERD_ENCRYPTION_CONTEXT.getKey(), credStashEncryptionContext, ConfigurationValue.LDAP_USER_CREDENTIAL_NAME.getKey(), ldapUserCredentialName); // Retrieve LDAP user password from the credstash. String ldapUserPassword; try { ldapUserPassword = credStashHelper.getCredentialFromCredStash(credStashEncryptionContext, ldapUserCredentialName); } catch (CredStashGetCredentialFailedException e) { throw new IllegalStateException(e); } // Create and initialize an LDAP context source. LdapContextSource contextSource = new LdapContextSource(); contextSource.setUrl(ldapUrl); contextSource.setBase(ldapBase); contextSource.setUserDn(ldapUserDn); contextSource.setPassword(ldapUserPassword); contextSource.afterPropertiesSet(); // Create an LDAP template. LdapTemplate ldapTemplate = new LdapTemplate(contextSource); // Create an LDAP query. LdapQuery ldapQuery = query().where(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_ID)).is(subjectMatterExpertKey.getUserId()); // Create a subject matter expert contact details mapper. SubjectMatterExpertContactDetailsMapper subjectMatterExpertContactDetailsMapper = new SubjectMatterExpertContactDetailsMapper(configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_FULL_NAME), configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_JOB_TITLE), configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_EMAIL_ADDRESS), configurationHelper.getProperty(ConfigurationValue.LDAP_ATTRIBUTE_USER_TELEPHONE_NUMBER)); // Gets information for the specified subject matter expert. List<SubjectMatterExpertContactDetails> subjectMatterExpertContactDetailsList = ldapOperations.search(ldapTemplate, ldapQuery, subjectMatterExpertContactDetailsMapper); // Return the results. return CollectionUtils.isNotEmpty(subjectMatterExpertContactDetailsList) ? subjectMatterExpertContactDetailsList.get(0) : null; }
Example 14
Source File: SchemaToJavaAdITest.java From spring-ldap with Apache License 2.0 | 4 votes |
@Before public void setUp() throws Exception { // Create some basic converters and a converter manager converterManager = new ConverterManagerImpl(); Converter ptc = new FromStringConverter(); converterManager.addConverter(String.class, "", Byte.class, ptc); converterManager.addConverter(String.class, "", Short.class, ptc); converterManager.addConverter(String.class, "", Integer.class, ptc); converterManager.addConverter(String.class, "", Long.class, ptc); converterManager.addConverter(String.class, "", Double.class, ptc); converterManager.addConverter(String.class, "", Float.class, ptc); converterManager.addConverter(String.class, "", Boolean.class, ptc); Converter tsc = new ToStringConverter(); converterManager.addConverter(Byte.class, "", String.class, tsc); converterManager.addConverter(Short.class, "", String.class, tsc); converterManager.addConverter(Integer.class, "", String.class, tsc); converterManager.addConverter(Long.class, "", String.class, tsc); converterManager.addConverter(Double.class, "", String.class, tsc); converterManager.addConverter(Float.class, "", String.class, tsc); converterManager.addConverter(Boolean.class, "", String.class, tsc); // Bind to the directory contextSource = new LdapContextSource(); contextSource.setUrl("ldaps://127.0.0.1:" + port); contextSource.setUserDn(USER_DN); contextSource.setPassword(PASSWORD); contextSource.setPooled(false); contextSource.setBase("dc=261consulting,dc=local"); HashMap<String, Object> baseEnvironment = new HashMap<String, Object>() {{ put("java.naming.ldap.attributes.binary", "thumbnailLogo replPropertyMetaData partialAttributeSet registeredAddress userPassword telexNumber partialAttributeDeletionList mS-DS-ConsistencyGuid attributeCertificateAttribute thumbnailPhoto teletexTerminalIdentifier replUpToDateVector dSASignature objectGUID"); }}; contextSource.setBaseEnvironmentProperties(baseEnvironment); contextSource.afterPropertiesSet(); ldapTemplate = new LdapTemplate(contextSource); cleanup(); DirContextAdapter ctx = new DirContextAdapter("cn=William Hartnell,cn=Users"); ctx.setAttributeValues("objectclass", new String[]{"person","inetorgperson","organizationalperson","top"}); ctx.setAttributeValue("cn", "William Hartnell"); ctx.addAttributeValue("description", "First Doctor"); ctx.addAttributeValue("description", "Grumpy"); ctx.addAttributeValue("sn", "Hartnell"); ctx.addAttributeValue("telephonenumber", "1"); ldapTemplate.bind(ctx); }