1.创建连接
/**
* 获取连接
* @param type 操作类型:1.操作用户2.操作用户组
* @return
*/
private static LdapContext getConnection(int type) {
LdapContext ctx = null;
Hashtable 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.put("cn", userId);
attrsbus.put("givenName", "小明");// 名称
attrsbus.put("gidNumber", "121");// ldap用户组编号(数字)
attrsbus.put("homeDirectory", "地址1");// 家庭地址
attrsbus.put("userPassword", "{md5}" + ldapEncoderByMd5("111111"));// 密码
attrsbus.put("sn", userId);
attrsbus.put("uidNumber", "100001");// 用户编号(数字)
String dn = "uid=" + userId;
// 在ou=Duser,dc=sss,dc=com目录下创建uid=xxx用户
ctx.createSubcontext(dn, attrsbus);
return true;
} catch (NamingException e) {
e.printStackTrace();
return false;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return false;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
} finally {
try {
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
} else {
return false;
}
}
/**
* 获取md5加密后的密码
* @param psw 密码,明文
* @return md5加密后的密码
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
private static String ldapEncoderByMd5(String psw) throws NoSuchAlgorithmException, UnsupportedEncodingException {
byte[] byteArray = null;
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.reset();
md5.update(psw.getBytes("utf-8"));
byteArray = md5.digest();
String md5pwd = new sun.misc.BASE64Encoder().encode(byteArray);
return md5pwd;
}
3.新增用户组
/**
* 创建用户组
* @param groupName 英文名
* @param groupId 编号
* @param groupChinseName 中文名
* @return
*/
public static boolean addLdapGroup(String groupName, String groupId, String groupChinseName) {
// 创建连接,此时的目录地址是ou=Group,dc=sss,dc=com,创建的用户组会存在这底下
LdapContext ctx = getConnection(2);
if (ctx != null) {
try {
BasicAttributes attrsbus = new BasicAttributes();
BasicAttribute objClass = new BasicAttribute("objectclass");
objClass.add("top");
objClass.add("posixGroup");
attrsbus.put(objClass);
attrsbus.put("cn", groupName);
attrsbus.put("gidNumber", groupId);// ldap用户组编号(数字)
attrsbus.put("description", groupChinseName);// 用户组中文描述
String dn = "cn=" + groupName;
// 在ou=Group,dc=sss,dc=com目录下创建cn=xxx用户组
ctx.createSubcontext(dn, attrsbus);
return true;
} catch (NamingException e) {
e.printStackTrace();
return false;
} finally {
try {
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
} else {
return false;
}
}
4. 删除用户或用户组
/**
* 删除用户、用户组
* @param dn
* @param type
* @return
*/
public static boolean delete(String userIdOrGroupName,int type) {
LdapContext ctx = getConnection(type);
StringBuffer dn = new StringBuffer();
if(ctx != null) {
if(type == 1) {
dn.append("uid=").append(userIdOrGroupName);
}else if(type == 2) {
dn.append("cn=").append(userIdOrGroupName);
}else {
return false;
}
try {
ctx.destroySubcontext(dn.toString());
return true;
} catch (NamingException e) {
e.printStackTrace();
return false;
}finally {
try {
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
} else {
return false;
}
}
5.修改用户组信息
/**
* 修改用户组信息
* @param groupName 用户组英文名
* @param groupChineseName 用户组中文名
* @return
*/
public static boolean modifyGroup(String groupName,String groupChineseName) {
LdapContext ctx = getConnection(2);
if(ctx != null) {
StringBuffer sb = new StringBuffer();
sb.append("cn=").append(groupName);
ModificationItem [] mods = new ModificationItem[1];
Attribute attr0 = new BasicAttribute("description",groupChineseName);
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr0);
try {
ctx.modifyAttributes(sb.toString(), mods);
return true;
} catch (NamingException e) {
e.printStackTrace();
return false;
}
}else {
return false;
}
}
6.修改用户密码
/**
* 修改用户密码
* @param userId 用户名
* @param password 密码
* @return
*/
public static boolean modifyUserPassword(String userId,String password) {
LdapContext ctx = getConnection(1);
if(ctx != null) {
StringBuffer sb = new StringBuffer();
sb.append("uid=").append(userId);
try {
ModificationItem [] mods = new ModificationItem[1];
Attribute attr0 = new BasicAttribute("userPassword","{md5}" + ldapEncoderByMd5(password));
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr0);
ctx.modifyAttributes(sb.toString(), mods);
return true;
} catch (NamingException e) {
e.printStackTrace();
return false;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return false;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
}
}else {
return false;
}
}
7.用户组新增用户
/**
* 用户组新增用户
* @param groupName 用户组
* @param ids 用户id数组
* @return
*/
public static boolean addUserToGroup(String groupName,String [] ids) {
LdapContext ctx = getConnection(2);
if(ctx != null) {
StringBuffer sb = new StringBuffer();
sb.append("cn=").append(groupName);
try {
ModificationItem [] mods = new ModificationItem[1];
Attribute memberUid = new BasicAttribute("memberUid");
if(ids != null && ids.length > 0) {
for(String id:ids) {
memberUid.add(id);
}
}
mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, memberUid);
ctx.modifyAttributes(sb.toString(), mods);
return true;
} catch (NamingException e) {
e.printStackTrace();
return false;
}
}else {
return false;
}
}
8.用户组删除用户
/**
* 用户组删除用户
* @param groupName 用户组
* @param ids 用户id数组
* @return
*/
public static boolean deleteUserToGroup(String groupName,String [] ids) {
LdapContext ctx = getConnection(2);
if(ctx != null) {
StringBuffer sb = new StringBuffer();
sb.append("cn=").append(groupName);
try {
ModificationItem [] mods = new ModificationItem[1];
Attribute memberUid = new BasicAttribute("memberUid");
if(ids != null && ids.length > 0) {
for(String id:ids) {
memberUid.add(id);
}
}
mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, memberUid);
ctx.modifyAttributes(sb.toString(), mods);
return true;
} catch (NamingException e) {
e.printStackTrace();
return false;
}
}else {
return false;
}
}