首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
其他好文
> 详细
LDAP
时间:
2015-09-09 22:46:46
阅读:
247
评论:
0
收藏:
0
[点我收藏+]
标签:
package com.smnpc.util;
import java.util.Hashtable;
import java.util.Vector;
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.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.LdapContext;
/**
* Java通过Ldap操作AD的增删该查询
* @author guob
*/
public
class LdapbyUser {
DirContext dc =
null;
String root =
"dc=example,dc=com";
// LDAP的根节点的DC
/**
*
* @param dn类似于"CN=RyanHanson,dc=example,dc=com"
* @param employeeID是Ad的一个员工号属性
*/
public LdapbyUser(String dn,String employeeID) {
init();
// add();//添加节点
// delete("ou=hi,dc=example,dc=com");//删除"ou=hi,dc=example,dc=com"节点
// renameEntry("ou=new,o=neworganization,dc=example,dc=com","ou=neworganizationalUnit,o=neworganization,dc=example,dc=com");//重命名节点"ou=new,o=neworganization,dc=example,dc=com"
// searchInformation("dc=example,dc=com", "", "sAMAccountName=guob");//遍历所有根节点
modifyInformation(dn,employeeID);
//修改
// Ldapbyuserinfo("guob");//遍历指定节点的分节点
close();
}
/**
*
* Ldap连接
*
* @return LdapContext
*/
public
void init() {
Hashtable env =
new Hashtable();
String LDAP_URL =
"ldap://xxxx:389"; // LDAP访问地址
String adminName =
"example\\user";
// 注意用户名的写法:domain\User或
String adminPassword =
"userpassword";
// 密码
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, LDAP_URL);
env.put(Context.SECURITY_AUTHENTICATION,
"simple");
env.put(Context.SECURITY_PRINCIPAL, adminName);
env.put(Context.SECURITY_CREDENTIALS, adminPassword);
try {
dc =
new InitialDirContext(env);
// 初始化上下文
System.out.println(
"认证成功");
// 这里可以改成异常抛出。
}
catch (javax.naming.AuthenticationException e) {
System.out.println(
"认证失败");
}
catch (Exception e) {
System.out.println(
"认证出错:" + e);
}
}
/**
* 添加
*/
public
void add(String newUserName) {
try {
BasicAttributes attrs =
new BasicAttributes();
BasicAttribute objclassSet =
new BasicAttribute(
"objectClass");
objclassSet.add(
"sAMAccountName");
objclassSet.add(
"employeeID");
attrs.put(objclassSet);
attrs.put(
"ou", newUserName);
dc.createSubcontext(
"ou=" + newUserName +
"," + root, attrs);
}
catch (Exception e) {
e.printStackTrace();
System.out.println(
"Exception in add():" + e);
}
}
/**
* 删除
*
* @param dn
*/
public
void delete(String dn) {
try {
dc.destroySubcontext(dn);
}
catch (Exception e) {
e.printStackTrace();
System.out.println(
"Exception in delete():" + e);
}
}
/**
* 重命名节点
*
* @param oldDN
* @param newDN
* @return
*/
public
boolean renameEntry(String oldDN, String newDN) {
try {
dc.rename(oldDN, newDN);
return
true;
}
catch (NamingException ne) {
System.err.println(
"Error: " + ne.getMessage());
return
false;
}
}
/**
* 修改
*
* @return
*/
public
boolean modifyInformation(String dn,String employeeID) {
try {
System.out.println(
"updating...\n");
ModificationItem[] mods =
new ModificationItem[
1];
/* 修改属性 */
// Attribute attr0 = new BasicAttribute("employeeID", "W20110972");
// mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr0);
/* 删除属性 */
// Attribute attr0 = new BasicAttribute("description",
// "陈轶");
// mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
// attr0);
/* 添加属性 */
Attribute attr0 =
new BasicAttribute(
"employeeID",employeeID);
mods[
0] =
new ModificationItem(DirContext.ADD_ATTRIBUTE, attr0);
/* 修改属性 */
dc.modifyAttributes(dn+
",dc=example,dc=com", mods);
return
true;
}
catch (NamingException e) {
e.printStackTrace();
System.err.println(
"Error: " + e.getMessage());
return
false;
}
}
/**
* 关闭Ldap连接
*/
public
void close() {
if (dc !=
null) {
try {
dc.close();
}
catch (NamingException e) {
System.out.println(
"NamingException in close():" + e);
}
}
}
/**
* @param base :根节点(在这里是"dc=example,dc=com")
* @param scope :搜索范围,分为"base"(本节点),"one"(单层),""(遍历)
* @param filter :指定子节点(格式为"(objectclass=*)",*是指全部,你也可以指定某一特定类型的树节点)
*/
public
void searchInformation(String base, String scope, String filter) {
SearchControls sc =
new SearchControls();
if (scope.equals(
"base")) {
sc.setSearchScope(SearchControls.OBJECT_SCOPE);
}
else
if (scope.equals(
"one")) {
sc.setSearchScope(SearchControls.ONELEVEL_SCOPE);
}
else {
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
}
NamingEnumeration ne =
null;
try {
ne = dc.search(base, filter, sc);
// Use the NamingEnumeration object to cycle through
// the result set.
while (ne.hasMore()) {
System.out.println();
SearchResult sr = (SearchResult) ne.next();
String name = sr.getName();
if (base !=
null && !base.equals(
"")) {
System.out.println(
"entry: " + name +
"," + base);
}
else {
System.out.println(
"entry: " + name);
}
Attributes at = sr.getAttributes();
NamingEnumeration ane = at.getAll();
while (ane.hasMore()) {
Attribute attr = (Attribute) ane.next();
String attrType = attr.getID();
NamingEnumeration values = attr.getAll();
Vector vals =
new Vector();
// Another NamingEnumeration object, this time
// to iterate through attribute values.
while (values.hasMore()) {
Object oneVal = values.nextElement();
if (oneVal
instanceof String) {
System.out.println(attrType +
": " + (String) oneVal);
}
else {
System.out.println(attrType +
": " +
new String((
byte[]) oneVal));
}
}
}
}
}
catch (Exception nex) {
System.err.println(
"Error: " + nex.getMessage());
nex.printStackTrace();
}
}
/**
* 查询
*
* @throws NamingException
*/
public
void Ldapbyuserinfo(String userName) {
// Create the search controls
SearchControls searchCtls =
new SearchControls();
// Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// specify the LDAP search filter
String searchFilter =
"sAMAccountName=" + userName;
// Specify the Base for the search 搜索域节点
String searchBase =
"DC=example,DC=COM";
int totalResults =
0;
String returnedAtts[] = {
"url",
"whenChanged",
"employeeID",
"name",
"userPrincipalName",
"physicalDeliveryOfficeName",
"departmentNumber",
"telephoneNumber",
"homePhone",
"mobile",
"department",
"sAMAccountName",
"whenChanged",
"mail" };
// 定制返回属性
searchCtls.setReturningAttributes(returnedAtts);
// 设置返回属性集
// searchCtls.setReturningAttributes(null); // 不定制属性,将返回所有的属性集
try {
NamingEnumeration answer = dc.search(searchBase, searchFilter,
searchCtls);
if (answer ==
null || answer.equals(
null)) {
System.out.println(
"answer is null");
}
else {
System.out.println(
"answer not null");
}
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult) answer.next();
System.out
.println(
"************************************************");
System.out.println(
"getname=" + sr.getName());
Attributes Attrs = sr.getAttributes();
if (Attrs !=
null) {
try {
for (NamingEnumeration ne = Attrs.getAll(); ne
.hasMore();) {
Attribute Attr = (Attribute) ne.next();
System.out.println(
"AttributeID="
+ Attr.getID().toString());
// 读取属性值
for (NamingEnumeration e = Attr.getAll(); e
.hasMore(); totalResults++) {
String user = e.next().toString();
// 接受循环遍历读取的userPrincipalName用户属性
System.out.println(user);
}
// System.out.println(" ---------------");
// // 读取属性值
// Enumeration values = Attr.getAll();
// if (values != null) { // 迭代
// while (values.hasMoreElements()) {
// System.out.println(" 2AttributeValues="
// + values.nextElement());
// }
// }
// System.out.println(" ---------------");
}
}
catch (NamingException e) {
System.err.println(
"Throw Exception : " + e);
}
}
}
System.out.println(
"Number: " + totalResults);
}
catch (Exception e) {
e.printStackTrace();
System.err.println(
"Throw Exception : " + e);
}
}
/**
* 主函数用于测试
* @param args
*/
public
static
void main(String[] args) {
new LdapbyUser(
"CN=RyanHanson",
"bbs.it-home.org");
}
}
LDAP
标签:
原文地址:http://www.cnblogs.com/markleilei/p/4796154.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
分布式事务
2021-07-29
OpenStack云平台命令行登录账户
2021-07-29
getLastRowNum()与getLastCellNum()/getPhysicalNumberOfRows()与getPhysicalNumberOfCells()
2021-07-29
【K8s概念】CSI 卷克隆
2021-07-29
vue3.0使用ant-design-vue进行按需加载原来这么简单
2021-07-29
stack栈
2021-07-29
抽奖动画 - 大转盘抽奖
2021-07-29
PPT写作技巧
2021-07-29
003-核心技术-IO模型-NIO-基于NIO群聊示例
2021-07-29
Bootstrap组件2
2021-07-29
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!