首页

关于jasig-cas包定义LdapUtils工具类实现JAVA关于LDAP协议目录存的指定用户过滤替换处理元源码代码

标签:jasig-cas,LdapUtils工具类,LDAP协议,轻量目录访问协议     发布时间:2018-10-15   

一、前言

基于jasig-case包中的org.jasig.cas.util.LdapUtils目录协议工具类,用适当的方法替换过滤器中的占位符的实用方法用户名的值,详情参见源码说明部分。

二、源码说明

package org.jasig.cas.util;@b@@b@import java.util.HashMap;@b@import java.util.Map;@b@@b@import javax.naming.NamingException;@b@import javax.naming.directory.DirContext;@b@@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@@b@/**@b@ * Utilities related to LDAP functions.@b@ * @b@ * @author Scott Battaglia@b@ * @version $Revision: 42053 $ $Date: 2007-06-10 09:17:55 -0400 (Sun, 10 Jun 2007) $@b@ * @since 3.0@b@ */@b@public final class LdapUtils {@b@@b@    private static final Log logger = LogFactory.getLog(LdapUtils.class);@b@@b@    private LdapUtils() {@b@        // private constructor so that no one can instantiate.@b@    }@b@@b@    /**@b@     * Utility method to replace the placeholders in the filter with the proper@b@     * values from the userName.@b@     * @b@     * @param filter@b@     * @param userName@b@     * @return the filtered string populated with the username@b@     */@b@    public static String getFilterWithValues(final String filter,@b@        final String userName) {@b@        final Map<String, String> properties = new HashMap<String, String>();@b@        final String[] userDomain;@b@        String newFilter = filter;@b@@b@        properties.put("%u", userName.replace("\\", "\\\\"));@b@@b@        userDomain = userName.split("@");@b@@b@        properties.put("%U", userDomain[0]);@b@@b@        if (userDomain.length > 1) {@b@            properties.put("%d", userDomain[1]);@b@@b@            final String[] dcArray = userDomain[1].split("\\.");@b@@b@            for (int i = 0; i < dcArray.length; i++) {@b@                properties.put("%" + (i + 1), dcArray[dcArray.length@b@                    - 1 - i]);@b@            }@b@        }@b@@b@        for (final String key : properties.keySet()) {@b@            final String value = properties.get(key);@b@            newFilter = newFilter.replaceAll(key, value);@b@        }@b@@b@        return newFilter;@b@    }@b@@b@    /**@b@     * Close the given context and ignore any thrown exception. This is useful@b@     * for typical finally blocks in manual Ldap statements.@b@     * @b@     * @param context the Ldap context to close@b@     */@b@    public static void closeContext(final DirContext context) {@b@        if (context != null) {@b@            try {@b@                context.close();@b@            } catch (NamingException ex) {@b@                logger.warn("Could not close context", ex);@b@            }@b@        }@b@    }@b@}