use of org.springframework.security.ldap.DefaultSpringSecurityContextSource in project spring-security by spring-projects.
the class EmbeddedLdapServerContextSourceFactoryBean method getObject.
@Override
public DefaultSpringSecurityContextSource getObject() throws Exception {
if (!ClassUtils.isPresent(UNBOUNDID_CLASSNAME, getClass().getClassLoader())) {
throw new IllegalStateException("Embedded LDAP server is not provided");
}
this.container = getContainer();
this.port = this.container.getPort();
DefaultSpringSecurityContextSource contextSourceFromProviderUrl = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:" + this.port + "/" + this.root);
if (this.managerDn != null) {
contextSourceFromProviderUrl.setUserDn(this.managerDn);
if (this.managerPassword == null) {
throw new IllegalStateException("managerPassword is required if managerDn is supplied");
}
contextSourceFromProviderUrl.setPassword(this.managerPassword);
}
contextSourceFromProviderUrl.afterPropertiesSet();
return contextSourceFromProviderUrl;
}
use of org.springframework.security.ldap.DefaultSpringSecurityContextSource in project spring-security by spring-projects.
the class LdapServerBeanDefinitionParserTests method embeddedServerCreationContainsExpectedContextSourceAndData.
@Test
public void embeddedServerCreationContainsExpectedContextSourceAndData() {
this.appCtx = new InMemoryXmlApplicationContext("<ldap-server ldif='classpath:test-server.ldif' port='0'/>");
DefaultSpringSecurityContextSource contextSource = (DefaultSpringSecurityContextSource) this.appCtx.getBean(BeanIds.CONTEXT_SOURCE);
// Check data is loaded
LdapTemplate template = new LdapTemplate(contextSource);
template.lookup("uid=ben,ou=people");
}
use of org.springframework.security.ldap.DefaultSpringSecurityContextSource in project spring-security by spring-projects.
the class NamespaceLdapAuthenticationProviderTests method ldapAuthenticationProviderCustomLdapAuthoritiesPopulator.
// SEC-2490
@Test
public void ldapAuthenticationProviderCustomLdapAuthoritiesPopulator() throws Exception {
LdapContextSource contextSource = new DefaultSpringSecurityContextSource("ldap://blah.example.com:789/dc=springframework,dc=org");
CustomAuthoritiesPopulatorConfig.LAP = new DefaultLdapAuthoritiesPopulator(contextSource, null) {
@Override
protected Set<GrantedAuthority> getAdditionalRoles(DirContextOperations user, String username) {
return new HashSet<>(AuthorityUtils.createAuthorityList("ROLE_EXTRA"));
}
};
this.spring.register(CustomAuthoritiesPopulatorConfig.class).autowire();
// @formatter:off
SecurityMockMvcRequestBuilders.FormLoginRequestBuilder request = formLogin().user("bob").password("bobspassword");
SecurityMockMvcResultMatchers.AuthenticatedMatcher user = authenticated().withAuthorities(Collections.singleton(new SimpleGrantedAuthority("ROLE_EXTRA")));
// @formatter:on
this.mockMvc.perform(request).andExpect(user);
}
use of org.springframework.security.ldap.DefaultSpringSecurityContextSource in project spring-security by spring-projects.
the class LdapAuthenticationProviderConfigurerTests method configureWhenObjectPostProcessorThenAuthoritiesPopulatorIsPostProcessed.
@Test
public void configureWhenObjectPostProcessorThenAuthoritiesPopulatorIsPostProcessed() {
LdapAuthoritiesPopulator populator = mock(LdapAuthoritiesPopulator.class);
assertThat(ReflectionTestUtils.getField(this.configurer, "ldapAuthoritiesPopulator")).isNull();
this.configurer.contextSource(new DefaultSpringSecurityContextSource("ldap://localhost:389"));
this.configurer.addObjectPostProcessor(new ObjectPostProcessor<LdapAuthoritiesPopulator>() {
@Override
public <O extends LdapAuthoritiesPopulator> O postProcess(O object) {
return (O) populator;
}
});
ReflectionTestUtils.invokeMethod(this.configurer, "getLdapAuthoritiesPopulator");
assertThat(ReflectionTestUtils.getField(this.configurer, "ldapAuthoritiesPopulator")).isSameAs(populator);
}
use of org.springframework.security.ldap.DefaultSpringSecurityContextSource in project shinyproxy by openanalytics.
the class LDAPAuthenticationBackend method configureAuthenticationManagerBuilder.
@Override
public void configureAuthenticationManagerBuilder(AuthenticationManagerBuilder auth) throws Exception {
LDAPProviderConfig[] configs = LDAPProviderConfig.loadAll(environment);
for (LDAPProviderConfig cfg : configs) {
LdapAuthenticationProviderConfigurer<AuthenticationManagerBuilder> configurer = new LdapAuthenticationProviderConfigurer<>();
String[] userDnPatterns = { cfg.userDnPattern };
if (userDnPatterns[0] == null || userDnPatterns[0].isEmpty())
userDnPatterns = new String[0];
if (cfg.managerDn != null && cfg.managerDn.isEmpty())
cfg.managerDn = null;
// Manually instantiate contextSource so it can be passed into authoritiesPopulator below.
DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(cfg.url);
if (cfg.managerDn != null) {
contextSource.setUserDn(cfg.managerDn);
contextSource.setPassword(cfg.managerPassword);
}
if (Boolean.valueOf(cfg.startTLS) || STARTTLS_SIMPLE.equalsIgnoreCase(cfg.startTLS)) {
// Explicitly disable connection pooling, or Spring may attempt to StartTLS twice on the same connection.
contextSource.setPooled(false);
contextSource.setAuthenticationStrategy(new DefaultTlsDirContextAuthenticationStrategy());
} else if (STARTTLS_EXTERNAL.equalsIgnoreCase(cfg.startTLS)) {
contextSource.setAuthenticationStrategy(new ExternalTlsDirContextAuthenticationStrategy());
}
contextSource.afterPropertiesSet();
// Manually instantiate authoritiesPopulator because it uses a customized class.
CNLdapAuthoritiesPopulator authoritiesPopulator = new CNLdapAuthoritiesPopulator(contextSource, cfg.groupSearchBase);
authoritiesPopulator.setGroupRoleAttribute("cn");
authoritiesPopulator.setGroupSearchFilter(cfg.groupSearchFilter);
configurer.userDnPatterns(userDnPatterns).userSearchBase(cfg.userSearchBase).userSearchFilter(cfg.userSearchFilter).ldapAuthoritiesPopulator(authoritiesPopulator).contextSource(contextSource).configure(auth);
}
}
Aggregations