Search in sources :

Example 31 with DirContextAdapter

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

the class LdapUserDetailsManager method loadUserByUsername.

public UserDetails loadUserByUsername(String username) {
    DistinguishedName dn = usernameMapper.buildDn(username);
    List<GrantedAuthority> authorities = getUserAuthorities(dn, username);
    logger.debug("Loading user '" + username + "' with DN '" + dn + "'");
    DirContextAdapter userCtx = loadUserAsContext(dn, username);
    return userDetailsMapper.mapUserFromContext(userCtx, username, authorities);
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 32 with DirContextAdapter

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

the class LdapUserDetailsManager method createUser.

public void createUser(UserDetails user) {
    DirContextAdapter ctx = new DirContextAdapter();
    copyToContext(user, ctx);
    DistinguishedName dn = usernameMapper.buildDn(user.getUsername());
    logger.debug("Creating new user '" + user.getUsername() + "' with DN '" + dn + "'");
    template.bind(dn, ctx, null);
    // Check for any existing authorities which might be set for this DN and remove
    // them
    List<GrantedAuthority> authorities = getUserAuthorities(dn, user.getUsername());
    if (authorities.size() > 0) {
        removeAuthorities(dn, authorities);
    }
    addAuthorities(dn, user.getAuthorities());
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 33 with DirContextAdapter

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

the class ActiveDirectoryLdapAuthenticationProviderTests method customSearchFilterIsUsedForSuccessfulAuthentication.

// SEC-1915
@Test
public void customSearchFilterIsUsedForSuccessfulAuthentication() throws Exception {
    // given
    String customSearchFilter = "(&(objectClass=user)(sAMAccountName={0}))";
    DirContext ctx = mock(DirContext.class);
    when(ctx.getNameInNamespace()).thenReturn("");
    DirContextAdapter dca = new DirContextAdapter();
    SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
    when(ctx.search(any(Name.class), eq(customSearchFilter), any(Object[].class), any(SearchControls.class))).thenReturn(new MockNamingEnumeration(sr));
    ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider("mydomain.eu", "ldap://192.168.1.200/");
    customProvider.contextFactory = createContextFactoryReturning(ctx);
    // when
    customProvider.setSearchFilter(customSearchFilter);
    Authentication result = customProvider.authenticate(joe);
    // then
    assertThat(result.isAuthenticated()).isTrue();
}
Also used : Authentication(org.springframework.security.core.Authentication) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) SearchResult(javax.naming.directory.SearchResult) SearchControls(javax.naming.directory.SearchControls) DirContext(javax.naming.directory.DirContext) Name(javax.naming.Name) DistinguishedName(org.springframework.ldap.core.DistinguishedName) Test(org.junit.Test)

Example 34 with DirContextAdapter

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

the class ActiveDirectoryLdapAuthenticationProviderTests method nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal.

@Test
public void nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal() throws Exception {
    provider = new ActiveDirectoryLdapAuthenticationProvider(null, "ldap://192.168.1.200/");
    DirContext ctx = mock(DirContext.class);
    when(ctx.getNameInNamespace()).thenReturn("");
    DirContextAdapter dca = new DirContextAdapter();
    SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
    when(ctx.search(eq(new DistinguishedName("DC=mydomain,DC=eu")), any(String.class), any(Object[].class), any(SearchControls.class))).thenReturn(new MockNamingEnumeration(sr));
    provider.contextFactory = createContextFactoryReturning(ctx);
    try {
        provider.authenticate(joe);
        fail("Expected BadCredentialsException for user with no domain information");
    } catch (BadCredentialsException expected) {
    }
    provider.authenticate(new UsernamePasswordAuthenticationToken("joe@mydomain.eu", "password"));
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) SearchResult(javax.naming.directory.SearchResult) SearchControls(javax.naming.directory.SearchControls) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) DirContext(javax.naming.directory.DirContext) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.Test)

Example 35 with DirContextAdapter

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

the class ActiveDirectoryLdapAuthenticationProviderTests method checkAuthentication.

private void checkAuthentication(String rootDn, ActiveDirectoryLdapAuthenticationProvider provider) throws NamingException {
    DirContext ctx = mock(DirContext.class);
    when(ctx.getNameInNamespace()).thenReturn("");
    DirContextAdapter dca = new DirContextAdapter();
    SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
    @SuppressWarnings("deprecation") DistinguishedName searchBaseDn = new DistinguishedName(rootDn);
    when(ctx.search(eq(searchBaseDn), any(String.class), any(Object[].class), any(SearchControls.class))).thenReturn(new MockNamingEnumeration(sr)).thenReturn(new MockNamingEnumeration(sr));
    provider.contextFactory = createContextFactoryReturning(ctx);
    Authentication result = provider.authenticate(joe);
    assertThat(result.getAuthorities()).isEmpty();
    dca.addAttributeValue("memberOf", "CN=Admin,CN=Users,DC=mydomain,DC=eu");
    result = provider.authenticate(joe);
    assertThat(result.getAuthorities()).hasSize(1);
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) Authentication(org.springframework.security.core.Authentication) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext)

Aggregations

DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)39 DistinguishedName (org.springframework.ldap.core.DistinguishedName)28 Test (org.junit.Test)19 GrantedAuthority (org.springframework.security.core.GrantedAuthority)11 DirContext (javax.naming.directory.DirContext)7 SearchControls (javax.naming.directory.SearchControls)7 SearchResult (javax.naming.directory.SearchResult)7 Name (javax.naming.Name)4 Authentication (org.springframework.security.core.Authentication)4 HashSet (java.util.HashSet)3 BasicAttribute (javax.naming.directory.BasicAttribute)3 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)3 UserDetails (org.springframework.security.core.userdetails.UserDetails)3 List (java.util.List)2 Attribute (javax.naming.directory.Attribute)2 BasicAttributes (javax.naming.directory.BasicAttributes)2 DirContextOperations (org.springframework.ldap.core.DirContextOperations)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 MockUserSearch (org.springframework.security.ldap.authentication.MockUserSearch)2 ArrayList (java.util.ArrayList)1