Examples of DirContextOperations


Examples of org.springframework.ldap.core.DirContextOperations

    unionizer.setPopulators( populators );

    unionizer.afterPropertiesSet();

    // get the user record
    DirContextOperations ctx = new SpringSecurityLdapTemplate( getContextSource() ).retrieveEntry( "uid=suzy,ou=users", //$NON-NLS-1$
        null );

    GrantedAuthority[] auths = unionizer.getGrantedAuthorities( ctx, "suzy" ); //$NON-NLS-1$

    assertTrue( null != auths && auths.length > 0 );
View Full Code Here

Examples of org.springframework.ldap.core.DirContextOperations

        }

        Assert.notNull(password, "Null password was supplied in authentication token");

        try {
            DirContextOperations userData = getAuthenticator().authenticate(authentication);

            Collection<GrantedAuthority> extraAuthorities = loadUserAuthorities(userData, username, password);

            UserDetails user = userDetailsContextMapper.mapUserFromContext(userData, username, extraAuthorities);
View Full Code Here

Examples of org.springframework.ldap.core.DirContextOperations

    public DirContextOperations authenticate(final Authentication authentication) {
        Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication,
                "Can only process UsernamePasswordAuthenticationToken objects");
        // locate the user and check the password

        DirContextOperations user = null;
        String username = authentication.getName();
        String password = (String)authentication.getCredentials();

        SpringSecurityLdapTemplate ldapTemplate = new SpringSecurityLdapTemplate(getContextSource());

        for (String userDn : getUserDns(username)) {
            try {
                user = ldapTemplate.retrieveEntry(userDn, getUserAttributes());
            } catch (NameNotFoundException ignore) {
            }
            if (user != null) {
                break;
            }
        }

        if (user == null && getUserSearch() != null) {
            user = getUserSearch().searchForUser(username);
        }

        if (user == null) {
            throw new UsernameNotFoundException("User not found: " + username, username);
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Performing LDAP compare of password attribute '" + passwordAttributeName + "' for user '" +
                    user.getDn() +"'");
        }

        String encodedPassword = passwordEncoder.encodePassword(password, null);
        byte[] passwordBytes = LdapUtils.getUtf8Bytes(encodedPassword);

        if (!ldapTemplate.compare(user.getDn().toString(), passwordAttributeName, passwordBytes)) {
            throw new BadCredentialsException(messages.getMessage("PasswordComparisonAuthenticator.badCredentials",
                    "Bad credentials"));
        }

        return user;
View Full Code Here

Examples of org.springframework.ldap.core.DirContextOperations

    }

    //~ Methods ========================================================================================================

    public DirContextOperations authenticate(Authentication authentication) {
        DirContextOperations user = null;
        Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication,
                "Can only process UsernamePasswordAuthenticationToken objects");

        String username = authentication.getName();
        String password = (String)authentication.getCredentials();

        if (!StringUtils.hasLength(password)) {
            logger.debug("Rejecting empty password for user " + username);
            throw new BadCredentialsException(messages.getMessage("LdapAuthenticationProvider.emptyPassword",
                    "Empty Password"));
        }

        // If DN patterns are configured, try authenticating with them directly
        for (String dn : getUserDns(username)) {
            user = bindWithDn(dn, username, password);

            if (user != null) {
                break;
            }
        }

        // Otherwise use the configured search object to find the user and authenticate with the returned DN.
        if (user == null && getUserSearch() != null) {
            DirContextOperations userFromSearch = getUserSearch().searchForUser(username);
            user = bindWithDn(userFromSearch.getDn().toString(), username, password);
        }

        if (user == null) {
            throw new BadCredentialsException(
                    messages.getMessage("BindAuthenticator.badCredentials", "Bad credentials"));
View Full Code Here

Examples of org.springframework.ldap.core.DirContextOperations

        this.userSearch = userSearch;
        this.authoritiesPopulator = authoritiesPopulator;
    }

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        DirContextOperations userData = userSearch.searchForUser(username);

        return userDetailsMapper.mapUserFromContext(userData, username,
                authoritiesPopulator.getGrantedAuthorities(userData, username));
    }
View Full Code Here

Examples of org.springframework.ldap.core.DirContextOperations

                MAIL_ATTRIBUTE_NAME, DISPLAY_NAME_ATTRIBUTE_NAME, "columns_3");
    }

    @Test
    public void testMapUserFromContext_new() throws Exception {
        DirContextOperations ctx = createMock(DirContextOperations.class);

        final String username = "johnldap";
        User user = new UserImpl("123", username);

        expect(userService.getUserByUsername(username)).andReturn(null).once();
        expect(ctx.attributeExists(MAIL_ATTRIBUTE_NAME)).andReturn(true);
        expect(ctx.getStringAttribute(MAIL_ATTRIBUTE_NAME)).andReturn("johnldap@example.com").times(2);
        expect(ctx.attributeExists(DISPLAY_NAME_ATTRIBUTE_NAME)).andReturn(true);
        expect(ctx.getStringAttribute(DISPLAY_NAME_ATTRIBUTE_NAME)).andReturn("John Ldap");
        expect(userService.getUserByUsername(username)).andReturn(user).once();
        expectLastCall();

        replay(userService, ctx);
View Full Code Here

Examples of org.springframework.ldap.core.DirContextOperations

        assertEquals(user, userDetails);
    }

    @Test
    public void testMapUserFromContext_new_no_displayname() throws Exception {
        DirContextOperations ctx = createMock(DirContextOperations.class);

        final String username = "johnldap";
        User user = new UserImpl("123", username);

        expect(userService.getUserByUsername(username)).andReturn(null).once();
        expect(ctx.attributeExists(MAIL_ATTRIBUTE_NAME)).andReturn(true);
        expect(ctx.getStringAttribute(MAIL_ATTRIBUTE_NAME)).andReturn("johnldap@example.com").times(2);
        expect(ctx.attributeExists(DISPLAY_NAME_ATTRIBUTE_NAME)).andReturn(false);
        expect(userService.getUserByUsername(username)).andReturn(user).once();
        expectLastCall();

        replay(userService, ctx);
View Full Code Here

Examples of org.springframework.ldap.core.DirContextOperations

        assertEquals(user, userDetails);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testMapUserFromContext_new_empty_username() throws Exception {
        DirContextOperations ctx = createMock(DirContextOperations.class);

        final String username = "";

        contextMapper.mapUserFromContext(ctx, username, Collections.<GrantedAuthority>emptyList());
View Full Code Here

Examples of org.springframework.ldap.core.DirContextOperations

        assertFalse("Exception thrown", true);
    }

    @Test(expected = RuntimeException.class)
    public void testMapUserFromContext_missing_mail() throws Exception {
        DirContextOperations ctx = createMock(DirContextOperations.class);

        final String username = "johnldap";

        expect(userService.getUserByUsername(username)).andReturn(null).once();
        expect(ctx.attributeExists(MAIL_ATTRIBUTE_NAME)).andReturn(false);

        replay(userService, ctx);

        contextMapper.mapUserFromContext(ctx, username, Collections.<GrantedAuthority>emptyList());
View Full Code Here

Examples of org.springframework.ldap.core.DirContextOperations

        assertFalse("Exception thrown", true);
    }

    @Test(expected = RuntimeException.class)
    public void testMapUserFromContext_empty_mail() throws Exception {
        DirContextOperations ctx = createMock(DirContextOperations.class);

        final String username = "johnldap";

        expect(userService.getUserByUsername(username)).andReturn(null).once();
        expect(ctx.attributeExists(MAIL_ATTRIBUTE_NAME)).andReturn(true);
        expect(ctx.getStringAttribute(MAIL_ATTRIBUTE_NAME)).andReturn("").times(1);

        replay(userService, ctx);

        contextMapper.mapUserFromContext(ctx, username, Collections.<GrantedAuthority>emptyList());
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.