Java Code Examples for org.springframework.ldap.core.LdapTemplate#setIgnorePartialResultException()

The following examples show how to use org.springframework.ldap.core.LdapTemplate#setIgnorePartialResultException() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LdapLoginProvider.java    From cuba with Apache License 2.0 7 votes vote down vote up
@PostConstruct
protected void init() {
    if (webLdapConfig.getLdapEnabled()) {
        ldapContextSource = new LdapContextSource();

        checkRequiredConfigProperties(webLdapConfig);

        ldapContextSource.setBase(webLdapConfig.getLdapBase());
        List<String> ldapUrls = webLdapConfig.getLdapUrls();
        ldapContextSource.setUrls(ldapUrls.toArray(new String[ldapUrls.size()]));
        ldapContextSource.setUserDn(webLdapConfig.getLdapUser());
        ldapContextSource.setPassword(webLdapConfig.getLdapPassword());

        ldapContextSource.afterPropertiesSet();

        ldapTemplate = new LdapTemplate(ldapContextSource);
        ldapTemplate.setIgnorePartialResultException(true);
    }
}
 
Example 2
Source File: SingleContextSource.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a SingleContextSource and execute the LdapOperationsCallback using the created instance.
 * This makes sure the same connection will be used for all operations inside the LdapOperationsCallback,
 * which is particularly useful when working with e.g. Paged Results as these typically require the exact
 * same connection to be used for all requests involving the same cookie..
 * The SingleContextSource instance will be properly disposed of once the operation has been completed.
 *
 * @param contextSource The target ContextSource to retrieve a DirContext from
 * @param callback the callback to perform the Ldap operations
 * @param useReadOnly if <code>true</code>, use the {@link org.springframework.ldap.core.ContextSource#getReadOnlyContext()}
 *                    method on the target ContextSource to get the actual DirContext instance, if <code>false</code>,
 *                    use {@link org.springframework.ldap.core.ContextSource#getReadWriteContext()}.
 * @param ignorePartialResultException Used for populating this property on the created LdapTemplate instance.
 * @param ignoreNameNotFoundException Used for populating this property on the created LdapTemplate instance.
 * @return the result returned from the callback.
 * @since 2.0
 */
public static <T> T doWithSingleContext(ContextSource contextSource,
                                        LdapOperationsCallback<T> callback,
                                        boolean useReadOnly,
                                        boolean ignorePartialResultException,
                                        boolean ignoreNameNotFoundException) {
    SingleContextSource singleContextSource;
    if (useReadOnly) {
        singleContextSource = new SingleContextSource(contextSource.getReadOnlyContext());
    } else {
        singleContextSource = new SingleContextSource(contextSource.getReadWriteContext());
    }

    LdapTemplate ldapTemplate = new LdapTemplate(singleContextSource);
    ldapTemplate.setIgnorePartialResultException(ignorePartialResultException);
    ldapTemplate.setIgnoreNameNotFoundException(ignoreNameNotFoundException);

    try {
        return callback.doWithLdapOperations(ldapTemplate);
    } finally {
        singleContextSource.destroy();
    }
}
 
Example 3
Source File: ChoerodonAuthenticationProvider.java    From oauth-server with Apache License 2.0 5 votes vote down vote up
private String accountAsUserDn2Authentication(String loginName, LdapE ldap, LdapContextSource contextSource, AndFilter filter) {
    contextSource.setUserDn(ldap.getAccount());
    contextSource.setPassword(ldap.getPassword());
    contextSource.afterPropertiesSet();
    LdapTemplate template = new LdapTemplate(contextSource);
    if (DirectoryType.MICROSOFT_ACTIVE_DIRECTORY.value().equals(ldap.getDirectoryType())) {
        template.setIgnorePartialResultException(true);
    }
    String userDn = null;
    try {
        List<String> names =
                template.search(
                        query()
                                .searchScope(SearchScope.SUBTREE)
                                .filter(filter),
                        new AbstractContextMapper() {
                            @Override
                            protected Object doMapFromContext(DirContextOperations ctx) {
                                return ctx.getNameInNamespace();
                            }
                        });
        userDn = getUserDn(names, ldap.getLoginNameField(), loginName);
    } catch (Exception e) {
        LOG.error("use ldap account as userDn and password to authentication but search failed, filter {}," +
                " maybe the account or password is illegal, and check for the ldap config, exception {}", filter, e);
    }
    return userDn;
}
 
Example 4
Source File: ChoerodonAuthenticationProvider.java    From oauth-server with Apache License 2.0 4 votes vote down vote up
private boolean ldapAuthentication(Long organizationId, String loginName, String credentials) {
    LdapE ldap = ldapService.queryByOrgId(organizationId);
    if (ldap != null && ldap.getEnabled()) {
        LdapContextSource contextSource = new LdapContextSource();
        String url = ldap.getServerAddress() + ":" + ldap.getPort();
        int connectionTimeout = ldap.getConnectionTimeout();
        contextSource.setUrl(url);
        contextSource.setBase(ldap.getBaseDn());
        setConnectionTimeout(contextSource, connectionTimeout);
        contextSource.afterPropertiesSet();

        LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
        //ad目录不设置会报错
        if (DirectoryType.MICROSOFT_ACTIVE_DIRECTORY.value().equals(ldap.getDirectoryType())) {
            ldapTemplate.setIgnorePartialResultException(true);
        }
        String userDn = null;
        boolean anonymousFetchFailed = false;

        AndFilter filter = getLoginFilter(ldap, loginName);
        try {
            List<String> names =
                    ldapTemplate.search(
                            query()
                                    .searchScope(SearchScope.SUBTREE)
                                    .filter(filter),
                            new AbstractContextMapper() {
                                @Override
                                protected Object doMapFromContext(DirContextOperations ctx) {
                                    return ctx.getNameInNamespace();
                                }
                            });
            userDn = getUserDn(names, ldap.getLoginNameField(), loginName);
        } catch (Exception e) {
            anonymousFetchFailed = true;
            LOG.error("ldap anonymous search failed, filter {}, exception {}", filter, e);
        }
        if (anonymousFetchFailed) {
            userDn = accountAsUserDn2Authentication(loginName, ldap, contextSource, filter);
        }
        if (userDn == null) {
            LOG.error("can not get userDn by filter {}, login failed", filter);
            return false;
        }
        return authentication(credentials, contextSource, userDn);
    } else {
        throw new AuthenticationServiceException(LoginException.LDAP_IS_DISABLE.value());
    }
}