java ldap 认证_java ldap用户认证

此博客展示了Java实现LDAP搜索与连接的代码。定义了LdapImpl类,包含搜索、连接和关闭方法,在搜索方法中设置搜索范围、过滤器等进行对象搜索;连接方法建立LDAP连接。还给出了LdapDemo类的main方法调用这些操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package test;

import java.util.Hashtable;

import javax.naming.Context;

import javax.naming.NamingEnumeration;

import javax.naming.NamingException;

import javax.naming.directory.Attribute;

import javax.naming.directory.Attributes;

import javax.naming.directory.DirContext;

import javax.naming.directory.InitialDirContext;

import javax.naming.directory.SearchControls;

import javax.naming.directory.SearchResult;

public class LdapImpl {

private DirContext ds;

public void search() throws NamingException {

System.out.println("Searching...");

SearchControls searchCtls = new SearchControls();

// Specify the search scope

searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

// specify the LDAP search filter

String searchFilter = "(sAMAccountName=adamli)";

// Specify the Base for the search

String searchBase = "dc=XX,dc=XX";

// Specify the attributes to return

String returnedAtts[] = {"mail", "displayName"};

searchCtls.setReturningAttributes(returnedAtts);

// Search for objects using the filter

NamingEnumeration entries = ds.search(searchBase,

searchFilter, searchCtls);

// Loop through the search results

while (entries.hasMoreElements()) {

SearchResult entry = entries.next();

System.out.println(">>>" + entry.getName());

// Print out the groups

Attributes attrs = entry.getAttributes();

if (attrs != null) {

for (NamingEnumeration extends Attribute> names = attrs

.getAll(); names.hasMore();) {

Attribute attr = names.next();

System.out.println("AttributeID: " + attr.getID());

for (NamingEnumeration> e = attr.getAll(); e.hasMore();) {

System.out.println("Attributes:" + e.next());

}

}

}

}

System.out.println("Search complete.");

}

public synchronized void connect() throws NamingException {

System.out.println("connecting...");

if (ds == null) {

Hashtable env = new Hashtable(11);

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

env.put(Context.PROVIDER_URL, "ldap://ip:port");

env.put(Context.SECURITY_AUTHENTICATION, "simple");

env.put(Context.SECURITY_PRINCIPAL, "username");

env.put(Context.SECURITY_CREDENTIALS, "password");

ds = new InitialDirContext(env);

}

System.out.println("connected.");

}

public void close() throws NamingException {

System.out.println("closing...");

ds.close();

System.out.println("closed.");

}

}

package test;

import javax.naming.NamingException;

public class LdapDemo {

public static void main(String[] args) {

LdapImpl ldap = new LdapImpl();

try {

ldap.connect();

ldap.search();

} catch (NamingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值