Search in sources :

Example 26 with DefaultSpringSecurityContextSource

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;
}
Also used : DefaultSpringSecurityContextSource(org.springframework.security.ldap.DefaultSpringSecurityContextSource)

Example 27 with DefaultSpringSecurityContextSource

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");
}
Also used : DefaultSpringSecurityContextSource(org.springframework.security.ldap.DefaultSpringSecurityContextSource) LdapTemplate(org.springframework.ldap.core.LdapTemplate) InMemoryXmlApplicationContext(org.springframework.security.config.util.InMemoryXmlApplicationContext) Test(org.junit.jupiter.api.Test)

Example 28 with DefaultSpringSecurityContextSource

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);
}
Also used : DefaultSpringSecurityContextSource(org.springframework.security.ldap.DefaultSpringSecurityContextSource) HashSet(java.util.HashSet) Set(java.util.Set) LdapContextSource(org.springframework.ldap.core.support.LdapContextSource) DefaultLdapAuthoritiesPopulator(org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator) CustomAuthoritiesPopulatorConfig(org.springframework.security.config.annotation.authentication.ldap.NamespaceLdapAuthenticationProviderTestsConfigs.CustomAuthoritiesPopulatorConfig) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) DirContextOperations(org.springframework.ldap.core.DirContextOperations) SecurityMockMvcRequestBuilders(org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders) SecurityMockMvcResultMatchers(org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers) Test(org.junit.jupiter.api.Test)

Example 29 with DefaultSpringSecurityContextSource

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);
}
Also used : NullLdapAuthoritiesPopulator(org.springframework.security.ldap.authentication.NullLdapAuthoritiesPopulator) LdapAuthoritiesPopulator(org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator) DefaultSpringSecurityContextSource(org.springframework.security.ldap.DefaultSpringSecurityContextSource) Test(org.junit.jupiter.api.Test)

Example 30 with DefaultSpringSecurityContextSource

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);
    }
}
Also used : DefaultSpringSecurityContextSource(org.springframework.security.ldap.DefaultSpringSecurityContextSource) ExternalTlsDirContextAuthenticationStrategy(org.springframework.ldap.core.support.ExternalTlsDirContextAuthenticationStrategy) AuthenticationManagerBuilder(org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder) LdapAuthenticationProviderConfigurer(org.springframework.security.config.annotation.authentication.configurers.ldap.LdapAuthenticationProviderConfigurer) DefaultTlsDirContextAuthenticationStrategy(org.springframework.ldap.core.support.DefaultTlsDirContextAuthenticationStrategy)

Aggregations

DefaultSpringSecurityContextSource (org.springframework.security.ldap.DefaultSpringSecurityContextSource)31 LdapContextSource (org.springframework.ldap.core.support.LdapContextSource)12 BindAuthenticator (org.springframework.security.ldap.authentication.BindAuthenticator)11 LdapAuthenticationProvider (org.springframework.security.ldap.authentication.LdapAuthenticationProvider)10 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)9 Authentication (org.springframework.security.core.Authentication)9 GrantedAuthority (org.springframework.security.core.GrantedAuthority)9 UserDetails (org.springframework.security.core.userdetails.UserDetails)9 FilterBasedLdapUserSearch (org.springframework.security.ldap.search.FilterBasedLdapUserSearch)8 Test (org.junit.jupiter.api.Test)7 DefaultLdapAuthoritiesPopulator (org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator)7 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)6 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)6 ArrayList (java.util.ArrayList)5 AuthenticationException (org.springframework.security.core.AuthenticationException)5 User (org.springframework.security.core.userdetails.User)5 ActiveDirectoryLdapAuthenticationProvider (org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider)5 User (org.apache.atlas.web.model.User)4 DefaultTlsDirContextAuthenticationStrategy (org.springframework.ldap.core.support.DefaultTlsDirContextAuthenticationStrategy)4 Bean (org.springframework.context.annotation.Bean)3