Search in sources :

Example 61 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project spring-security by spring-projects.

the class FilterBasedLdapUserSearchTests method extraFilterPartToExcludeBob.

// Try some funny business with filters.
@Test
public void extraFilterPartToExcludeBob() {
    FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(&(cn=*)(!(|(uid={0})(uid=rod)(uid=jerry)(uid=slashguy)(uid=javadude)(uid=groovydude)(uid=closuredude)(uid=scaladude))))", this.contextSource);
    // Search for bob, get back ben...
    DirContextOperations ben = locator.searchForUser("bob");
    assertThat(ben.getStringAttribute("cn")).isEqualTo("Ben Alex");
}
Also used : DirContextOperations(org.springframework.ldap.core.DirContextOperations) Test(org.junit.jupiter.api.Test)

Example 62 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project spring-security by spring-projects.

the class FilterBasedLdapUserSearchTests method subTreeSearchSucceeds.

@Test
public void subTreeSearchSucceeds() throws Exception {
    // Don't set the searchBase, so search from the root.
    FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("", "(cn={0})", this.contextSource);
    locator.setSearchSubtree(true);
    DirContextOperations ben = locator.searchForUser("Ben Alex");
    assertThat(ben.getStringAttribute("uid")).isEqualTo("ben");
    assertThat(ben.getDn()).isEqualTo(new LdapName("uid=ben,ou=people"));
}
Also used : DirContextOperations(org.springframework.ldap.core.DirContextOperations) LdapName(javax.naming.ldap.LdapName) Test(org.junit.jupiter.api.Test)

Example 63 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project spring-security by spring-projects.

the class FilterBasedLdapUserSearchTests method basicSearchSucceeds.

@Test
public void basicSearchSucceeds() throws Exception {
    FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(uid={0})", this.contextSource);
    locator.setSearchSubtree(false);
    locator.setSearchTimeLimit(0);
    locator.setDerefLinkFlag(false);
    DirContextOperations bob = locator.searchForUser("bob");
    assertThat(bob.getStringAttribute("uid")).isEqualTo("bob");
    assertThat(bob.getDn()).isEqualTo(new LdapName("uid=bob,ou=people"));
}
Also used : DirContextOperations(org.springframework.ldap.core.DirContextOperations) LdapName(javax.naming.ldap.LdapName) Test(org.junit.jupiter.api.Test)

Example 64 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project spring-security by spring-projects.

the class FilterBasedLdapUserSearchTests method searchForNameWithCommaSucceeds.

@Test
public void searchForNameWithCommaSucceeds() throws Exception {
    FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(uid={0})", this.contextSource);
    locator.setSearchSubtree(false);
    DirContextOperations jerry = locator.searchForUser("jerry");
    assertThat(jerry.getStringAttribute("uid")).isEqualTo("jerry");
    assertThat(jerry.getDn()).isEqualTo(new LdapName("cn=mouse\\, jerry,ou=people"));
}
Also used : DirContextOperations(org.springframework.ldap.core.DirContextOperations) LdapName(javax.naming.ldap.LdapName) Test(org.junit.jupiter.api.Test)

Example 65 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project spring-security by spring-projects.

the class LdapBindAuthenticationManagerFactoryITests method authenticationManagerFactoryWhenCustomUserDetailsContextMapperThenUsed.

@Test
public void authenticationManagerFactoryWhenCustomUserDetailsContextMapperThenUsed() throws Exception {
    CustomUserDetailsContextMapperConfig.CONTEXT_MAPPER = new UserDetailsContextMapper() {

        @Override
        public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
            return User.withUsername("other").password("password").roles("USER").build();
        }

        @Override
        public void mapUserToContext(UserDetails user, DirContextAdapter ctx) {
        }
    };
    this.spring.register(CustomUserDetailsContextMapperConfig.class).autowire();
    this.mockMvc.perform(formLogin().user("bob").password("bobspassword")).andExpect(authenticated().withUsername("other"));
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UserDetailsContextMapper(org.springframework.security.ldap.userdetails.UserDetailsContextMapper) DirContextOperations(org.springframework.ldap.core.DirContextOperations) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.jupiter.api.Test)

Aggregations

DirContextOperations (org.springframework.ldap.core.DirContextOperations)89 Name (javax.naming.Name)20 Test (org.junit.jupiter.api.Test)13 Test (org.junit.Test)9 UserDetails (org.springframework.security.core.userdetails.UserDetails)9 HashSet (java.util.HashSet)8 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)8 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)8 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)7 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)7 FilterBasedLdapUserSearch (org.springframework.security.ldap.search.FilterBasedLdapUserSearch)7 LdapConfig (com.thoughtworks.go.config.LdapConfig)6 BaseConfig (com.thoughtworks.go.config.server.security.ldap.BaseConfig)6 BasesConfig (com.thoughtworks.go.config.server.security.ldap.BasesConfig)6 ArrayList (java.util.ArrayList)6 Set (java.util.Set)6 NameNotFoundException (org.springframework.ldap.NameNotFoundException)6 GrantedAuthority (org.springframework.security.core.GrantedAuthority)5 PerunAttribute (cz.metacentrum.perun.ldapc.model.PerunAttribute)4 List (java.util.List)4