提供了两个例子,一个是CRUD功能。另外一个是利用查询LDAP实现用户的登录验证功能。完全借鉴的Java到LDAP教程、JAVA中使用LDAP登录的三种方式
java在LDAP上进行CRUD
现在可以从Java程序访问LDAP。 向您展示如何执行此操作的最佳方法是通过示例程序。 该程序将执行以下任务:
- 创建一个新的LDAP对象
- 查看LDAP对象
- 将新属性添加到LDAP对象
- 修改LDAP对象上的属性
- 删除LDAP对象上的属性
- 删除LDAP对象
注意:此类在其自身之后进行清除,即它将LDAP Server保留在其被发现的状态。 如果要查看正在执行的各种任务,只需运行其中一项任务,然后通过LDAP客户端查看LDAP对象。 不要忘记,您可以在LDAP客户端中修改对象并进行测试。
package test;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.*;
public class LDAPTest {
public void run() {
try {
DirContext context = getContext();
String name = "employeeNumber=00001,ou=system";
createLDAPObject(context, name);
createAttribute(context, name, "displayName", "JOBS");
viewAttribute(context, name, "displayName");
updateAttribute(context, name, "displayName", "STEVE");
viewAttribute(context, name, "displayName");
removeAttribute(context, name, "displayName");
removeLDAPObject(context, name);
} catch (NamingException e) {
e.printStackTrace();
}
}
private void removeLDAPObject(DirContext context, String name) throws NamingException {
context.destroySubcontext(name);
}
private void createLDAPObject(DirContext context, String name) throws NamingException {
Attributes attributes = new BasicAttributes();
Attribute attribute = new BasicAttribute("objectClass");
attribute.add("inetOrgPerson");
attributes.put(attribute);
Attribute sn = new BasicAttribute("sn");
sn.add("Steve");
attributes.put(sn);
Attribute cn = new BasicAttribute("cn");
cn.add("Jobs");
attributes.put(cn);
attributes.put("telephoneNumber", "123456");
context.createSubcontext(name, attributes);
}
private void removeAttribute(DirContext context, String name , String attrName) throws NamingException {
Attribute attribute = new BasicAttribute(attrName);
ModificationItem[] item = new ModificationItem[1];
item[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attribute);
context.modifyAttributes(name, item);
}
private void createAttribute(DirContext context, String name , String attrName, Object attrValue) throws NamingException {
Attribute attribute = new BasicAttribute(attrName, attrValue);
ModificationItem[] item = new ModificationItem[1];
item[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attribute);
context.modifyAttributes(name, item);
}
private void updateAttribute(DirContext context, String name , String attrName, Object attrValue) throws NamingException {
Attribute attribute = new BasicAttribute(attrName, attrValue);
ModificationItem[] item = new ModificationItem[1];
item[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute);
context.modifyAttributes(name, item);
}
private void viewAttribute(DirContext context, String name , String attrName) throws NamingException {
Attributes attrs = context.getAttributes(name);
System.out.println(attrName + ":" + attrs.get(attrName).get());
}
private DirContext getContext() throws NamingException {
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
properties.put(Context.PROVIDER_URL, "ldap://localhost:10389");
return new InitialDirContext(properties);
}
public static void main(String[] args) {
new LDAPTest().run();
}
}
java 查询 LDAP实现用户名密码登录验证
JAVA中使用LDAP登录的三种方式。
搜索中关于java 登录ldap,大部分会采用 cn=xxx,ou=xxx,dc=xxx的方式,此处的cn是用户的Display Name,而不是account,而且如果ou有多层,比如我们的OU就会超过三层。
import java.util.Hashtable;
import java.util.Properties;
import javax.naming.AuthenticationException;
import javax.naming.CommunicationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.*;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
/**
* 获取默认LDAP连接 * Exception 则登录失败,ctx不为空则登录成功
*
* @return void
*/
public static LdapContext getLDAPConnection() throws AuthenticationException, CommunicationException, Exception {
LdapContext ctx = null;
//LDAP 连接地址 ldap://IP:PORT (default port 389)
String LDAP_URL = "ldap://localhost:10389";
//LDAP SSL连接地址 ldaps://IP:PORT (default port 636)
//(这个用起来比较麻烦,目前知道管理员改密码必须使用SSL)
String LDAP_SSL_URL = "";
//用户名
String userAccount = "";
//用户密码
String userPassword = "";
// 方式1
// 基于姓名(cn),此cn为Display Name,部门有同名就麻烦了
//userAccount = "cn=xxx,OU=xxx,DC=xxx,DC=com";
// 方式2
// 基于Account User Logon name:
// userAccount = "xxx@domain.xxx";
// 方式3
// 基于Account User Logon name(pre-windows 2000):
// userAccount = "domain\\xxx"
// 基于登录名(uid (User ID)与 unix 的 uid 完全不同)(请注意objectSID,此处尝试失败)
userAccount = "uid=zhangsan,ou=users,dc=example,dc=com";
userPassword = "123456";
Hashtable<String, String> HashEnv = new Hashtable<String, String>();
HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别(none,simple,strong)
HashEnv.put(Context.SECURITY_PRINCIPAL, userAccount); //AD的用户名
HashEnv.put(Context.SECURITY_CREDENTIALS, userPassword); //AD的密码
HashEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP工厂类
HashEnv.put("com.sun.jndi.ldap.connect.timeout", "3000");//连接超时设置为3秒
HashEnv.put(Context.PROVIDER_URL, LDAP_URL);
ctx = new InitialLdapContext(HashEnv, null);//new InitialDirContext(HashEnv);// 初始化上下文
return ctx;
}
参考文章:
1、https://blog.csdn.net/dnc8371/article/details/106703325
2、https://www.cnblogs.com/huanghongbo/p/12053272.html