首页 > 其他分享 >LDAP 研究

LDAP 研究

时间:2023-09-14 12:03:33浏览次数:38  
标签:username String 研究 LDAP import new public append


用LDAP牵涉到的几个概念,Context,entry/object,filter.Attribute
Context:上下文,我的理解是相当与文件系统的中的目录(JNDI的Naming Service是可以用操作系统的文件系统的).
entry/object:一个节点,相当与文件系统中的目录或文件.
filter:查询/过滤条件是一个字符串表达式如:(&(objectClass=top)(cn=*))查询出objectClass属性为top,cn属性为所有情况的entry.
Attribute:entry/object的属性可以理解成JAVA对象的属性,不同的是这个属性可以多次赋值.

import java.util.HashMap;
import java.util.Properties;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.naming.Context;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.AttributeInUseException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.NoSuchAttributeException;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class LDAPManager {

    /** The OU (organizational unit) to add users to */
    private static final String USERS_OU =
        "ou=People,dc=ibm,dc=com";

    /** The OU (organizational unit) to add groups to */
    private static final String GROUPS_OU =
        "ou=Group,dc=ibm,dc=com";

    /** The OU (organizational unit) to add permissions to */
    private static final String PERMISSIONS_OU =
        "ou=Permission,dc=ibm,dc=com";

    /** The default LDAP port */
    private static final int DEFAULT_PORT = 389;

    /** The LDAPManager instance object */
    private static Map instances = new HashMap();

    /** The connection, through a <code>DirContext</code>, to LDAP */
    private DirContext context;

    /** The hostname connected to */
    private String hostname;

    /** The port connected to */
    private int port;

    protected LDAPManager(String hostname, int port,
                          String username, String password)
        throws NamingException {

        context = getInitialContext(hostname, port, username, password);

        // Only save data if we got connected
        this.hostname = hostname;
        this.port = port;
    }

    public static LDAPManager getInstance(String hostname,
                                          int port,
                                          String username,
                                          String password)
        throws NamingException {

        // Construct the key for the supplied information
        String key = new StringBuffer()
            .append(hostname)
            .append(":")
            .append(port)
            .append("|")
            .append((username == null ? "" : username))
            .append("|")
            .append((password == null ? "" : password))
            .toString();

        if (!instances.containsKey(key)) {
            synchronized (LDAPManager.class) {
                if (!instances.containsKey(key)) {
                    LDAPManager instance =
                        new LDAPManager(hostname, port,
                                        username, password);
                    instances.put(key, instance);
                    return instance;
                }
            }
        }

        return (LDAPManager)instances.get(key);
    }

    public static LDAPManager getInstance(String hostname, int port)
        throws NamingException {

        return getInstance(hostname, port, null, null);
    }

    public static LDAPManager getInstance(String hostname)
        throws NamingException {

        return getInstance(hostname, DEFAULT_PORT, null, null);
    }

    public void addUser(String username, String firstName,
                        String lastName, String password)
        throws NamingException {

        // Create a container set of attributes
        Attributes container = new BasicAttributes();

        // Create the objectclass to add
        Attribute objClasses = new BasicAttribute("objectClass");
        objClasses.add("top");
        objClasses.add("person");
        objClasses.add("organizationalPerson");
        objClasses.add("inetOrgPerson");

        // Assign the username, first name, and last name
        String cnValue = new StringBuffer(firstName)
            .append(" ")
            .append(lastName)
            .toString();
        Attribute cn = new BasicAttribute("cn", cnValue);
        Attribute givenName = new BasicAttribute("givenName", firstName);
        Attribute sn = new BasicAttribute("sn", lastName);
        Attribute uid = new BasicAttribute("uid", username);
//        
//        Attribute att = new BasicAttribute("memberof");
//        if(username.startsWith("dev")){
//        	String memberOf = new StringBuilder("cn=dev,").append(GROUPS_OU).toString();
//        	att.add(memberOf);
//        }else if(username.startsWith("qa")){
//        	String memberOf = new StringBuilder("cn=qa,").append(GROUPS_OU).toString();
//        	att.add(memberOf);
//        }else{
//        	String memberOf = new StringBuilder("cn=dev,").append(GROUPS_OU).toString();
//        	att.add(memberOf);
//        	
//        	memberOf = new StringBuilder("cn=qa,").append(GROUPS_OU).toString();
//        	att.add(memberOf);
//        }
//        container.put(att);

        // Add password
        Attribute userPassword =
            new BasicAttribute("userpassword", password);

        // Add these to the container
        container.put(objClasses);
        container.put(cn);
        container.put(sn);
        container.put(givenName);
        container.put(uid);
        container.put(userPassword);

        // Create the entry
        context.createSubcontext(getUserDN(username), container);
    }

    public void deleteUser(String username) throws NamingException {
        try {
            context.destroySubcontext(getUserDN(username));
        } catch (NameNotFoundException e) {
            // If the user is not found, ignore the error
        }
    }

    public boolean isValidUser(String username, String password)
        throws UserNotFoundException {
        try {
            DirContext context =
                getInitialContext(hostname, port, getUserDN(username),
                                  password);
            return true;
        } catch (javax.naming.NameNotFoundException e) {
            throw new UserNotFoundException(username);
        } catch (NamingException e) {
            // Any other error indicates couldn't log user in
            return false;
        }
    }

    public void addGroup(String name, String description, String[] memberOfGroup)
        throws NamingException, Throwable {

    	 if(memberOfGroup != null && memberOfGroup.length > 0){
    		// Create a container set of attributes
		        Attributes container = new BasicAttributes();

		        // Create the objectclass to add
		        Attribute objClasses = new BasicAttribute("objectClass");
		        objClasses.add("top");
		        objClasses.add("groupOfUniqueNames");
//		        objClasses.add("groupOfNames");

		        // Assign the name and description to the group
		        Attribute cn = new BasicAttribute("cn", name);
		        Attribute desc = new BasicAttribute("description", description);
//		        StringBuilder sb = new StringBuilder();
//		        int count = 0;
//		        for(String member : memberOfGroup){
//		        	if(count > 0){
//		        		sb.append(",");
//		        	}
//		        	sb.append("uid=").append(member);
//		        	count ++;
//		        }
//		        String memberAttribute = sb.append(",").append(USERS_OU).toString();
//	        	Attribute groupmember = new BasicAttribute("member", memberAttribute);
//	        	container.put(groupmember);

		        Attribute memberAttributes = new BasicAttribute("uniquemember");
		        for(String member : memberOfGroup){
		        	String memberAttribute = new StringBuilder("uid=").append(member).append(",").append(USERS_OU).toString();
//		        	Attribute groupmember = new BasicAttribute("member", memberAttribute);
		        	memberAttributes.add(memberAttribute);
		        }

		        container.put(memberAttributes);
		        // Add these to the container
		        container.put(objClasses);
		        container.put(cn);
		        container.put(desc);

		        // Create the entry
		        context.createSubcontext(getGroupDN(name), container);
    	 }else{
    		 throw new Throwable("Error occurs, not assign members into this group.");
    	 }
    }

    public void deleteGroup(String name) throws NamingException {
        try {
            context.destroySubcontext(getGroupDN(name));
        } catch (NameNotFoundException e) {
            // If the group is not found, ignore the error
        }
    }

    public void addPermission(String name, String description)
        throws NamingException {

        // Create a container set of attributes
        Attributes container = new BasicAttributes();

        // Create the objectclass to add
        Attribute objClasses = new BasicAttribute("objectClass");
        objClasses.add("top");
        objClasses.add("ibmPermission");

        // Assign the name and description to the group
        Attribute cn = new BasicAttribute("cn", name);
        Attribute desc = new BasicAttribute("description", description);

        // Add these to the container
        container.put(objClasses);
        container.put(cn);
        container.put(desc);

        // Create the entry
        context.createSubcontext(getPermissionDN(name), container);
    }

    public void deletePermission(String name) throws NamingException {
        try {
            context.destroySubcontext(getPermissionDN(name));
        } catch (NameNotFoundException e) {
            // If the permission is not found, ignore the error
        }
    }

    public void assignUser(String username, String groupName)
        throws NamingException {

        try {
            ModificationItem[] mods = new ModificationItem[1];

            Attribute mod =
                new BasicAttribute("uniqueMember",
                                   getUserDN(username));
            mods[0] =
                new ModificationItem(DirContext.ADD_ATTRIBUTE, mod);
            context.modifyAttributes(getGroupDN(groupName), mods);
        } catch (AttributeInUseException e) {
            // If user is already added, ignore exception
        }
    }

    public void removeUser(String username, String groupName)
        throws NamingException {

        try {
            ModificationItem[] mods = new ModificationItem[1];

            Attribute mod =
                new BasicAttribute("uniqueMember",
                                   getUserDN(username));
            mods[0] =
                new ModificationItem(DirContext.REMOVE_ATTRIBUTE, mod);
            context.modifyAttributes(getGroupDN(groupName), mods);
        } catch (NoSuchAttributeException e) {
            // If user is not assigned, ignore the error
        }
    }

    public boolean userInGroup(String username, String groupName)
        throws NamingException {

        // Set up attributes to search for
        String[] searchAttributes = new String[1];
        searchAttributes[0] = "uniqueMember";

        Attributes attributes =
            context.getAttributes(getGroupDN(groupName),
                                  searchAttributes);
        if (attributes != null) {
            Attribute memberAtts = attributes.get("uniqueMember");
            if (memberAtts != null) {
                for (NamingEnumeration vals = memberAtts.getAll();
                     vals.hasMoreElements();
                     ) {
                    if (username.equalsIgnoreCase(
                        getUserUID((String)vals.nextElement()))) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    public List getMembers(String groupName) throws NamingException {
        List members = new LinkedList();

        // Set up attributes to search for
        String[] searchAttributes = new String[1];
        searchAttributes[0] = "uniqueMember";

        Attributes attributes =
            context.getAttributes(getGroupDN(groupName),
                                  searchAttributes);
        if (attributes != null) {
            Attribute memberAtts = attributes.get("uniqueMember");
            if (memberAtts != null) {
                for (NamingEnumeration vals = memberAtts.getAll();
                     vals.hasMoreElements();
                     members.add(
                         getUserUID((String)vals.nextElement()))) ;
            }
        }

        return members;
    }

    public List getGroups(String username) throws NamingException {
        List groups = new LinkedList();

        // Set up criteria to search on
        String filter = new StringBuffer()
            .append("(&")
            .append("(objectClass=groupOfIBMNames)")
            .append("(uniqueMember=")
            .append(getUserDN(username))
            .append(")")
            .append(")")
            .toString();

        // Set up search constraints
        SearchControls cons = new SearchControls();
        cons.setSearchScope(SearchControls.ONELEVEL_SCOPE);

//        cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
//        cons.setCountLimit(10000);

        NamingEnumeration results =
            context.search(GROUPS_OU, filter, cons);

        while (results.hasMore()) {
            SearchResult result = (SearchResult)results.next();
            groups.add(getGroupCN(result.getName()));
        }

        return groups;
    }

    public void assignPermission(String groupName, String permissionName)
        throws NamingException {

        try {
            ModificationItem[] mods = new ModificationItem[1];

            Attribute mod =
                new BasicAttribute("uniquePermission",
                                   getPermissionDN(permissionName));
            mods[0] =
                new ModificationItem(DirContext.ADD_ATTRIBUTE, mod);
            context.modifyAttributes(getGroupDN(groupName), mods);
        } catch (AttributeInUseException e) {
            // Ignore the attribute if it is already assigned
        }
    }

    public void revokePermission(String groupName, String permissionName)
        throws NamingException {

        try {
            ModificationItem[] mods = new ModificationItem[1];

            Attribute mod =
                new BasicAttribute("uniquePermission",
                                   getPermissionDN(permissionName));
            mods[0] =
                new ModificationItem(DirContext.REMOVE_ATTRIBUTE, mod);
            context.modifyAttributes(getGroupDN(groupName), mods);
        } catch (NoSuchAttributeException e) {
            // Ignore errors if the attribute doesn't exist
        }
    }

    public boolean hasPermission(String groupName, String permissionName)
        throws NamingException {

        // Set up attributes to search for
        String[] searchAttributes = new String[1];
        searchAttributes[0] = "uniquePermission";

        Attributes attributes =
            context.getAttributes(getGroupDN(groupName),
                                  searchAttributes);
        if (attributes != null) {
            Attribute permAtts = attributes.get("uniquePermission");
            if (permAtts != null) {
                for (NamingEnumeration vals = permAtts.getAll();
                     vals.hasMoreElements();
                     ) {
                    if (permissionName.equalsIgnoreCase(
                        getPermissionCN((String)vals.nextElement()))) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    public List getPermissions(String groupName) throws NamingException {
        List permissions = new LinkedList();

        // Set up attributes to search for
        String[] searchAttributes = new String[1];
        searchAttributes[0] = "uniquePermission";

        Attributes attributes =
            context.getAttributes(getGroupDN(groupName),
                                  searchAttributes);
        if (attributes != null) {
            Attribute permAtts = attributes.get("uniquePermission");
            if (permAtts != null) {
                for (NamingEnumeration vals = permAtts.getAll();
                     vals.hasMoreElements();
                     permissions.add(
                         getPermissionCN((String)vals.nextElement()))) ;
            }
        }

        return permissions;
    }

    private String getUserDN(String username) {
        return new StringBuffer()
                .append("uid=")
                .append(username)
                .append(",")
                .append(USERS_OU)
                .toString();
    }

    private String getUserUID(String userDN) {
        int start = userDN.indexOf("=");
        int end = userDN.indexOf(",");

        if (end == -1) {
            end = userDN.length();
        }

        return userDN.substring(start+1, end);
    }

    private String getGroupDN(String name) {
        return new StringBuffer()
                .append("cn=")
                .append(name)
                .append(",")
                .append(GROUPS_OU)
                .toString();
    }

    private String getGroupCN(String groupDN) {
        int start = groupDN.indexOf("=");
        int end = groupDN.indexOf(",");

        if (end == -1) {
            end = groupDN.length();
        }

        return groupDN.substring(start+1, end);
    }

    private String getPermissionDN(String name) {
        return new StringBuffer()
                .append("cn=")
                .append(name)
                .append(",")
                .append(PERMISSIONS_OU)
                .toString();
    }

    private String getPermissionCN(String permissionDN) {
        int start = permissionDN.indexOf("=");
        int end = permissionDN.indexOf(",");

        if (end == -1) {
            end = permissionDN.length();
        }

        return permissionDN.substring(start+1, end);
    }

    private DirContext getInitialContext(String hostname, int port,
                                         String username, String password)
        throws NamingException {

        String providerURL =
            new StringBuffer("ldap://")
                .append(hostname)
                .append(":")
                .append(port)
                .toString();

        Properties props = new Properties();
        props.put(Context.INITIAL_CONTEXT_FACTORY,
                  "com.sun.jndi.ldap.LdapCtxFactory");
        props.put(Context.PROVIDER_URL, providerURL);

        if ((username != null) && (!username.equals(""))) {
            props.put(Context.SECURITY_AUTHENTICATION, "simple");
            props.put(Context.SECURITY_PRINCIPAL, username);
            props.put(Context.SECURITY_CREDENTIALS,
                ((password == null) ? "" : password));
        }

        return new InitialDirContext(props);
    }
}

public class UserNotFoundException extends RuntimeException{
	private static final long serialVersionUID = 1L;

	public UserNotFoundException(String message) {
		super(message);
	}

	public UserNotFoundException(Throwable cause) {
		super(cause);
	}

	public UserNotFoundException(String message, Throwable cause) {
		super(message, cause);
	}
}



测试方法:


import org.junit.BeforeClass;
import org.junit.Test;


public class LDAPManagerTest {
	private static LDAPManager ldapManager;

	@BeforeClass
	public static void initEnv() throws Exception{
		ldapManager = LDAPManager.getInstance("10.2.9.110", 389, "cn=manager,dc=ibm,dc=com", "secret");
	}

	@Test
	public void addGroup() throws Throwable{
		try{
			String[] str1 = {"test1", "test2", "test3", "test4", "test5"};
			String[] str2 = {"test1", "test7", "test9"};
			ldapManager.addGroup("dev", "All people in dev team", str1);
			ldapManager.addGroup("qa", "All people in dev team", str2);
			ldapManager.assignUser("test5", "qa");
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Test
	public void addUser() throws Exception{
		for(int i=1; i<234000; i++){
			ldapManager.addUser("test"+i, "first"+i, "last"+i, "123");
		}
	}

}



search()方法的第一个参数是一个上下文执行时在这个上下文下进行查找,第二个参数是一个Filter字串意思是符合objectClass=top并且cn属性为任意值的entry,第三个属性是一个SearchControls对象在这个对象中设置一些参数用于控制查找,如controls.setSearchScope(SearchControls.SUBTREE_SCOPE); 这个方法中有三个值,SUBTREE_SCOPE为查找给定上下文(第一个参数)下以及所有下级上下文下的所有entry,而 ONELEVEL_SCOPE只查找给定上下文下的entry,OBJECT_SCOPE只查找一个entry。 controls.setCountLimit(100); 是设置查找返回的最大结果,如果查询的结果超过了这个值那么就会抛出一个异常。还有一个设置超时时间的方法setTimeout()。



[img]http://dl.iteye.com/upload/attachment/524853/dae31924-4ace-3371-a0a1-c80407c2494a.jpg



import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.SizeLimitExceededException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

/**
 * Demonstrates how to perform a search and limit the number of results
 * returned.
 * 
 * usage: java SearchCountLimit
 */
class SearchCountLimit {
  static int expected = 1;

  public static void printSearchEnumeration(NamingEnumeration srhEnum) {
    int count = 0;
    try {
      while (srhEnum.hasMore()) {
        SearchResult sr = (SearchResult) srhEnum.next();
        System.out.println(">>>" + sr.getName());
        ++count;
      }

      System.out.println("number of answers: " + count);
    } catch (SizeLimitExceededException e) {
      if (count == expected)
        System.out.println("number of answers: " + count);
      else
        e.printStackTrace();
    } catch (NamingException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {
      // Create initial context
      DirContext ctx = new InitialDirContext(env);

      // Set search controls to limit count to 'expected'
      SearchControls ctls = new SearchControls();
      ctls.setCountLimit(expected);

      // Search for objects with those matching attributes
      NamingEnumeration answer = ctx.search("ou=People", "(sn=M*)", ctls);

      // Print the answer
      printSearchEnumeration(answer);

      // Close the context when we're done
      ctx.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}

标签:username,String,研究,LDAP,import,new,public,append
From: https://blog.51cto.com/u_16261339/7468622

相关文章

  • 易基因: MeRIP-seq等揭示组蛋白乙酰化和m6A修饰在眼部黑色素瘤发生中的互作调控|肿瘤研
    大家好,这里是专注表观组学十余年,领跑多组学科研服务的易基因。组蛋白去乙酰化抑制剂(HDACis)在多种恶性肿瘤中显示出令人鼓舞的结果。N6-甲基腺嘌呤(m6A)是最普遍的mRNA修饰,在肿瘤发生调控中起重要作用。然而,对组蛋白乙酰化和m6ARNA修饰之间的互作研究仍然未知。2023年7月1......
  • 360安全研究二面
    360安全研究二面现在能力方向和想要发展的方向?我看你有挖SRC的漏洞,你可以说一个比较重要比较特别的漏洞吗?防火墙的相关内容?并发漏洞又挖过吗?知道如何进行防护吗?域渗透学的多吗?APP渗透是抓包吗还是说逆向方面?我这边没什么问题了,你还有什么想问的吗?......
  • 深入研究消息队列05 各消息队列集群架构对比
    23RabbitMQ的集群架构集群构建数据可靠性身份认证资源鉴权可观测性......
  • 【专题】2023年中国工业互联网平台行业研究报告PDF合集分享(附原数据表)
    原文链接:https://tecdat.cn/?p=33647原文出处:拓端数据部落公众号这份报告合集是基于中国工业产业升级和智能制造的大背景而展开的。报告合集分析了工业互联网平台市场的发展阶段、平台玩家的产品和服务的底层逻辑以及变化趋势,并探讨了补贴减少、数据归属权之争、标准化与盈利模......
  • R语言: GARCH模型股票交易量的研究道琼斯股票市场指数|附代码数据
    原文链接:http://tecdat.cn/?p=6632原文出处:拓端数据部落公众号 最近我们被客户要求撰写关于GARCH的研究报告,包括一些图形和统计输出。我将建立道琼斯工业平均指数(DJIA)日交易量对数比的ARMA-GARCH模型。 ``获取数据load(file='DowEnvironment.RData')日交易量 每日交易量......
  • 百度中文点选验证码研究
    之前百度都是只有旋转验证码,最近总是出现中文点选验证码。而且中文数量比较多,大图中有固定的7个中文字符需要识别。我首先想到使用通用的中文识别,当时我尝试了很多出名的中文识别但是效果都非常差,基本上全错,完全是不可用的状态。没有办法,只有自己来训练识别模型了。由于中文种类非......
  • 360安全研究一面
    360安全研究一面中间一半的时候才开始录的,整理了一下后半段的面试问题。自我介绍sqlmap针对时间盲注该用什么参数?MSF使用上的流程?你经常用MSF的用来做什么?内网里不出网,如何搭建隧道?如果说内网里拿到层层主机,如何配置路由突破网段?SSH端口转发了解过吗?命令知道吗?连接soc......
  • python3 pip3 安装python-ldap失败
    pip3安装时提示ERROR:Couldnotbuildwheelsforpython-ldap,uWSGI,M2Crypto,whichisrequiredtoinstallpyproject.toml-basedprojectsERROR:CouldnotbuildwheelsforuWSGI,whichisrequiredtoinstallpyproject.toml-basedprojects需要apt-getinstall......
  • 基于深度学习的图像识别技术研究
    基于深度学习的图像识别技术是计算机视觉领域的一个重要研究方向,它已经在多个领域取得了巨大的成功。下面是关于这一技术研究的一些重要方面:1.卷积神经网络(CNNs): CNNs是深度学习中用于图像识别的基本工具。它们模拟了人类视觉系统的工作原理,通过一系列卷积和池化层来提取图......