ldap操作用户、用户组---南瓜55555先生

本文档详细介绍了如何使用LDAP进行用户和用户组的操作,包括建立连接、新增与删除用户及用户组、修改用户信息和密码,以及用户组成员的增删管理。
摘要由CSDN通过智能技术生成

 1.创建连接

   /**
	 * 获取连接
	 * @param type 操作类型:1.操作用户2.操作用户组
	 * @return
	 */
	private static LdapContext getConnection(int type) {
		LdapContext ctx = null;
		Hashtable<String, String> env = new Hashtable<>();
		StringBuffer ldapUrl = new StringBuffer();
		if (type == 1) {// 对用户操作时
			ldapUrl.append("ldap://127.0.0.1:389/").append("ou=Duser,dc=sss,dc=com");
		} else if (type == 2) {// 对用户组操作时
			ldapUrl.append("ldap://127.0.0.1:389/").append("ou=Group,dc=sss,dc=com");
		}
		env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LadpCtxFactory");
		env.put(Context.SECURITY_AUTHENTICATION, "simple");
		env.put(Context.SECURITY_PRINCIPAL, "uid=ldap,ou=aaa,dc=sss,dc=com");// 用户名
		env.put(Context.SECURITY_CREDENTIALS, "psw12345t");// 密码
		env.put(Context.PROVIDER_URL, ldapUrl.toString());// 目录地址
		try {
			ctx = new InitialLdapContext(env, null);// 创建连接
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
		return ctx;
	}

 2.新增用户


	/**
	 * 新增用户---注意密码部分--{加密类型}加密后的密码
	 * @param userId
	 * @return
	 */
	public static boolean addUser(String userId) {
		// 创建连接,此时的目录地址是ou=Duser,dc=sss,dc=com,创建的用户会存在这底下
		LdapContext ctx = getConnection(1);

		if (ctx != null) {
			try {
				BasicAttributes attrsbus = new BasicAttributes();
				BasicAttribute objClass = new BasicAttribute("objectclass");
				objClass.add("inetOrgPerson");
				objClass.add("posixAccount");
				objClass.add("top");
				objClass.add("shadowAccount");
				attrsbus.put(objClass);
				attrsbus.pu
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值