public class LDAPHelper {
/**
* LDAP可以理解为一个多级目录,这里,表示要连接到那个具体的目录
*/
private final String baseDn = "ou=People,dc=changyeyi,dc=com";
private LdapContext ctx = null;
private final Control[] connCtls = null;
private void connect() {
Hashtable env = new Hashtable<>();
String factory = "com.sun.jndi.ldap.LdapCtxFactory";
env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
String url = "ldap://192.168.31.178:389/";
env.put(Context.PROVIDER_URL, url + baseDn);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
String root = "cn=admin,dc=changyeyi,dc=com";
//管理员dn
env.put(Context.SECURITY_PRINCIPAL, root);
// 管理员密码
env.put(Context.SECURITY_CREDENTIALS, "123456");
try {
ctx = new InitialLdapContext(env, connCtls);
System.out.println( "连接成功" );
} catch (javax.naming.AuthenticationException e) {
System.out.println("连接失败:");
e.printStackTrace();
} catch (Exception e) {
System.out.println("连接出错:");
e.printStackTrace();
}
}
private void closeContext(){
if (ctx != null) {
try {
ctx.close();
}
catch (NamingException e) {
e.printStackTrace();
}
}
}
/**
* dn就是目标名字+当前目录名字
*/
private String getUserDN(String uid) {
String userDN = "";
connect();
try {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
//第一个参数是从哪个目录下开始查,因为连接的url里已经指定了ou=People,dc=gosun,dc=com,所以这里填""即可
//第二个参数是查询条件,uid、email等,都是ldap中存放的参数
NamingEnumeration en = ctx.search("", "uid=" + uid, constraints);
if (en == null || !en.hasMoreElements()) {
System.out.println("未找到该用户");
}
while (en != null && en.hasMoreElements()) {
SearchResult obj = en.nextElement();
if (obj != null) {
SearchResult si = obj;
userDN += si.getName();
userDN += "," + baseDn;
} else {
System.out.println((Object) null);
}
}
} catch (Exception e) {
System.out.println("查找用户时产生异常。");
e.printStackTrace();
}
return userDN;
}
/**
* 通过uid获取dn,然后连接验证
*/
public boolean authentication(String uid, String password) {
boolean valide = false;
String userDN = getUserDN(uid);
try {
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, userDN);
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
ctx.reconnect(connCtls);
System.out.println(userDN + " 验证通过");
valide = true;
} catch (AuthenticationException e) {
System.out.println(userDN + " 验证失败");
System.out.println(e.toString());
} catch (NamingException e) {
System.out.println(userDN + " 验证失败");
}
closeContext();
return valide;
}
public boolean addUser(String usr, String pwd,String uid,String description) {
try {
connect();
BasicAttributes attrsbu = new BasicAttributes();
BasicAttribute objclassSet = new BasicAttribute("objectclass");
objclassSet.add("inetOrgPerson");
attrsbu.put(objclassSet);
attrsbu.put("sn", usr);
attrsbu.put("cn", usr);
attrsbu.put("uid", uid);
attrsbu.put("userPassword", pwd);
attrsbu.put("description", description);
//这里指定目录名字,可以用uid=/cn=,如uid=changyeyi,或者cn=yaoji
ctx.createSubcontext("uid="+uid+"", attrsbu);
return true;
} catch (NamingException ex) {
ex.printStackTrace();
}
closeContext();
return false;
}
public static void main(String[] args) {
LDAPHelper ldap = new LDAPHelper();
/**
* 用户认证
*/
/*if(ldap.authentication("changyeyi", "changyeyi")){
System.out.println( "该用户认证成功" );
}*/
/**
* 用户添加
*/
ldap.addUser("emma","emma","emma_uid","测试");
}
}