高分请教如何加ldap的条目啊。急,在线等。
我已经成功添加了一下条目,
dn: dc=edu,dc=cn
objectclass: dcObject
objectclass: organization
o: ynau
dc: edu
dn: cn=Manager,dc=edu,dc=cn
objectclass: organizationalRole
cn: Manager
我现在想添加
dn: cn=commonUser,dc=edu,dc=cn
objectclass: organizationalRole
cn: commonUser
可是输入密码后总是提示错误哦。
问题点数:100、回复次数:6Top
1 楼mysticality(影子传说)回复于 2006-06-02 20:40:52 得分 0
符合LDAP 的schema的要求么?Top
2 楼mywindyboy(华剑香)回复于 2006-06-02 20:58:30 得分 0
就是用的openldap自带的core.schema啊。
dn: cn=Manager,dc=edu,dc=cn
objectclass: organizationalRole
cn: Manager
这个都可以,我的意思是再建一个organizationalRole条目而已啊。Top
3 楼mywindyboy(华剑香)回复于 2006-06-02 21:11:46 得分 0
错误提示是: invalid credentials
不知道是什么类型的错误?Top
4 楼figoren(figoren)回复于 2006-12-01 14:55:40 得分 100
import javax.naming.*;
import javax.naming.directory.*;
import java.util.*;
import com.ibm.jndi.*;
public class RegisterBean{
private String sID = "";
private String sPass = "";
private int iMsg = 0;
private String sServer = "";
private DirContext ctx;
private BasicAttribute objClasses;
private BasicAttributes attrs;
private Properties env = new Properties();
/**
函数名:setServer
说明:付初值于服务器变量
输入:server--LDAP服务器名和端口
输出:
*/
public void setServer(String server){
sServer = server;
}
/**
函数名:setID
说明:付初值于用户ID
输入:sUserID--用户ID
输出:
*/
public void setID(String sUserID){
sID = sUserID;
}
/**
函数名:setPass
说明:付初值于用户口令
输入:pass--用户口令
输出:
*/
public void setPass(String pass){
sPass = pass;
}
/**
函数名:initEnv
说明:初始化系统变量
输入:
输出:iMsg--0-成功;1-失败
*/
public int initEnv(){
env.put("java.naming.factory.initial", "com.ibm.jndi.LDAPCtxFactory");
env.put("java.naming.provider.url", sServer);
env.put("java.naming.ldap.version", "3");
env.put(Context.SECURITY_AUTHENTICATION, "CRAM-MD5");
env.put(Context.SECURITY_PRINCIPAL, "cn=admin");
env.put(Context.SECURITY_CREDENTIALS, "admin");
objClasses = new BasicAttribute("objectclass");
objClasses.add("inetOrgPerson");// 初始化要创建的用户类
attrs = new BasicAttributes();
try{
ctx = new InitialDirContext(env);
iMsg = 0;
}catch(Exception e){
iMsg = 1;
Debug.printErr(e.getMessage());
}
return iMsg;
}
/**
函数名:doRegister
说明:注册新用户
输入:
输出:iMsg--0-成功;1-失败
*/
public int doRegister(){
attrs.put(objClasses);
attrs.put("sn", "test");
attrs.put("userPassword", sPass);
attrs.put("entryOwner","access-id:cn="+sID+",o=company,c=cn");//设定条目所有者为当前所创建用户
try{
ctx.createSubcontext("cn=" + sID + ",o=company,c=cn",attrs);
ctx.modifyAttributes("cn="+sID+",o=company,c=cn", DirContext.ADD_ATTRIBUTE,new BasicAttributes("entryOwner","access-id:cn=admin"));//设定多个所有者
iMsg = 0;
}catch(Exception e){
iMsg = 1;
}finally
{
try
{
ctx.close();
}catch(Exception e){
Debug.printErr(this.getClass().getName()+": "+e.getMessage());}
}
return iMsg;
}
public static void main(String[] args){
RegisterBean registerBean = new RegisterBean();
registerBean.setServer(args[0]);
registerBean.setID(args[1]);
registerBean.setPass(args[2]);
int iCode = registerBean.initEnv();
if(iCode == 1){
Debug.println("注册失败");
}else{
Debug.println( "注册成功");
}
iCode = registerBean.doRegister();
if(iCode == 1){
Debug.println("注册失败");
}else{
Debug.println( "注册成功");
}
}
}
public class DeleteBean{
private String sID = "";
private int iMsg = 0;
private String sServer = "";
private DirContext ctx;
private Properties env = new Properties();
public void setServer(String server){
sServer = server;
}
public void setID(String sUserID){
sID = sUserID;
}
public void initEnv(){
env.put("java.naming.factory.initial", "com.ibm.jndi.LDAPCtxFactory");
env.put("java.naming.provider.url", sServer);
env.put("java.naming.ldap.version", "3");
env.put(Context.SECURITY_AUTHENTICATION, "CRAM-MD5");
env.put(Context.SECURITY_PRINCIPAL, "cn=db2admin");
env.put(Context.SECURITY_CREDENTIALS, "db2admin");
}
/**
函数名:doRegister
说明:注册新用户
输入:
输出:iMsg--0-成功;1-失败
*/
public int doDelete(){
try{
ctx = new InitialDirContext(env);
ctx.destroySubcontext("cn="+sID+",o=company,c=cn");
iMsg = 0;
}catch(Exception e){
iMsg = 1;
// Debug.printErr(e.getMessage());
}finally
{
try
{
ctx.close();
}catch(Exception e){
Debug.printErr(this.getClass().getName()+": "+e.getMessage());
}
}
return iMsg;
}
}
Top
5 楼figoren(figoren)回复于 2006-12-01 14:56:36 得分 0
public class LoginBean{
private String sID = "";
private String sPass = "";
private int iMsg = 0;
private String sServer = "";
private String sSuffix = "";
// private DirContext ctx;
private Properties env = new Properties();
public LoginBean(){
}
/**
函数名:LoginBean(String server,String suffix,String sUserID,String pass)
说明:初始化变量
输入:server--LDAP服务器名和端口;String suffix--域后缀;sUserID--用户ID;pass--用户口令
输出:
*/
public LoginBean(String server,String suffix,String sUserID,String pass){
setServer(server);
setSuffix(suffix);
setID(sUserID);
setPass(pass);
}
public void setServer(String server){
sServer = server;
}
public void setSuffix(String suffix){
sSuffix = suffix;
}
public void setID(String sUserID){
sID = sUserID;
}
public void setPass(String pass){
sPass = pass;
}
public void initEnv(){
env.put("java.naming.factory.initial", "com.ibm.jndi.LDAPCtxFactory");
env.put("java.naming.provider.url", sServer);
env.put("java.naming.ldap.version", "3");
// env.put(Context.SECURITY_AUTHENTICATION, "CRAM-MD5");
env.put("java.naming.ldap.derefAliases","never");
// env.put(Context.SECURITY_PRINCIPAL, "cn=admin");
// env.put(Context.SECURITY_CREDENTIALS, "admin");
}
/**
函数名:checkLogin
说明:检查是否为合法用户
输入:
输出:LDAPAuthenticationException
*/
public void checkLogin() throws LDAPAuthenticationException{
if(sServer.equals("")) throw new LDAPAuthenticationException("LDAP服务器名称未付初值!");
if(sID.equals("")) throw new LDAPAuthenticationException("用户ID未付初值!");
if(sPass.equals(""))throw new LDAPAuthenticationException("用户口令未付初值!");
if(sSuffix.equals(""))throw new LDAPAuthenticationException("LDAP域后缀未付初值!");
String sFilter = "(uid="+sID+")";
String sDN = "";
try{
initEnv();
DirContext ctx = new InitialDirContext(env);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = ctx.search(sSuffix, sFilter, constraints);
if(results.hasMore()){
SearchResult sr = (SearchResult)results.next();
sDN = sr.getName()+","+sSuffix;
}else{
throw new LDAPAuthenticationException("用户名不存在!");
}
results.close();
ctx.close();
env.put(Context.SECURITY_PRINCIPAL, sDN);
env.put(Context.SECURITY_CREDENTIALS, sPass);
ctx = new InitialDirContext(env);
ctx.close();
iMsg = 0;
}catch(Exception e){
throw new LDAPAuthenticationException("用户登录失败!");
}
public void doChangePass() throws LDAPChangePassException{
if(sServer=="") throw new LDAPChangePassException("LDAP服务器名称未付初值!");
if(sID=="") throw new LDAPChangePassException("用户ID未付初值!");
if(sOldPass=="")throw new LDAPChangePassException("旧用户口令未付初值!");
if(sNewPass=="")throw new LDAPChangePassException("新用户口令未付初值!");
if(sSuffix=="")throw new LDAPChangePassException("LDAP域后缀未付初值!");
String sFilter = "(uid="+sID+")";
String sDN = "";
try{
initEnv();
DirContext ctx = new InitialDirContext(env);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = ctx.search(sSuffix, sFilter, constraints);
if(results.hasMore()){
SearchResult sr = (SearchResult)results.next();
sDN = sr.getName()+","+sSuffix;
}else{
throw new LDAPChangePassException("用户名不存在!");
}
results.close();
ctx.close();
env.put(Context.SECURITY_PRINCIPAL, sDN);
env.put(Context.SECURITY_CREDENTIALS, sOldPass);
ctx = new InitialDirContext(env);
Debug.println(sDN);
ctx.modifyAttributes(sDN, DirContext.REPLACE_ATTRIBUTE,new BasicAttributes("userPassword",sNewPass));
}catch(Exception e){
Debug.printErr("ChangePassBean : doChangePass error :: " + e.getMessage());
e.printStackTrace();
throw new LDAPChangePassException("密码更改失败!");
}
finally
{
try
{
ctx.close();
}catch(Exception e){
Debug.printErr(this.getClass().getName()+": "+e.getMessage());
}
}
}
}
}
public class SearchBean{
private String sFilter = "";
private String sFields[];
private String sServer = "";
private String sSuffix = "";
// private DirContext ctx;
private Properties env = new Properties();
public SearchBean(){
}
/**
函数名:LoginBean(String server,String suffix,String sUserID,String pass)
说明:初始化变量
输入:server--LDAP服务器名和端口;String suffix--域后缀;filter--查询条件;fields--返回子段
输出:
*/
public SearchBean(String server,String suffix,String filter,String[] fields){
setServer(server);
setSuffix(suffix);
setFilter(filter);
setFields(fields);
}
public void setServer(String server){
sServer = server;
}
public void setSuffix(String suffix){
sSuffix = suffix;
}
public void setFilter(String filter){
sFilter = filter;
}
public void setFields(String fields[]){
sFields = fields;
}
public void initEnv(){
env.put("java.naming.factory.initial", "com.ibm.jndi.LDAPCtxFactory");
env.put("java.naming.provider.url", sServer);
env.put("java.naming.ldap.version", "3");
env.put("java.naming.ldap.derefAliases","never");
}Top
6 楼figoren(figoren)回复于 2006-12-01 14:56:51 得分 0
/**
函数名:search
说明:根据查询条件返回结果集
输入:
输出:结果集
*/
public NamingEnumeration search() throws LDAPSearchException{
if(sServer=="") throw new LDAPSearchException("LDAP服务器名称未付初值!");
if(sSuffix=="")throw new LDAPSearchException("LDAP域后缀未付初值!");
DirContext ctx = null;
NamingEnumeration results =null;
try{
initEnv();
ctx = new InitialDirContext(env);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
constraints.setReturningAttributes(sFields);
//Debug.println("sSuffix:"+sSuffix);
//Debug.println("sFilter:"+sFilter);
results = ctx.search(sSuffix, sFilter, constraints);
return results;
}catch(Exception e){
Debug.printErr(this.getClass().getName()+": "+e.getMessage());
throw new LDAPSearchException("查询失败!");
}finally
{
try
{
//results.close();
ctx.close();
}catch(Exception e){
Debug.printErr(this.getClass().getName()+": "+e.getMessage());}
}
}
public static void main(String[] args){
// String inputFields[];
SearchBean searchBean = new SearchBean();
searchBean.setServer(args[0]);
searchBean.setSuffix(args[1]);
searchBean.setFilter(args[2]);
// inputFields[0] = args[3];
// searchBean.setFields(inputFields);
NamingEnumeration searchResults;
try{
searchResults = searchBean.search();
while (searchResults.hasMore()) {
SearchResult si =(SearchResult)searchResults.next();
Debug.println(si.getName());
Attributes attrs = si.getAttributes();
if (attrs == null) {
Debug.println(" No attributes");
continue;
}
NamingEnumeration ae = attrs.getAll();
while (ae.hasMoreElements()) {
Attribute attr =(Attribute)ae.next();
String id = attr.getID();
Enumeration vals = attr.getAll();
while (vals.hasMoreElements())
Debug.println(" "+id + ": " + vals.nextElement());
}
ae.close();
}
searchResults.close();
}catch(Exception e){
Debug.printErr(e.getMessage());
}
}
}
public class SearchDeptNameByID{
private String sFilter = "";
private String sFields[]={"company_SupDept","ou"};
private String sServer = "";
private String sSuffix = "";
private String sDeptID = "";
// private DirContext ctx;
private Properties env = new Properties();
/**
函数名:SearchDeptNameByID()
说明:构造函数
输入:
输出:
*/
public SearchDeptNameByID(){
}
/**
函数名:SearchDeptNameByID(String server,String suffix,String deptID)
说明:初始化变量
输入:server--LDAP服务器名和端口;String suffix--域后缀;filter--查询条件;fields--返回子段
输出:
*/
public SearchDeptNameByID(String server,String suffix,String deptID){
setServer(server);
setSuffix(suffix);
setDeptID(deptID);
// setFields(fields);
}
/**
函数名:setServer
说明:付初值于服务器变量
输入:server--LDAP服务器名和端口
输出:
*/
public void setServer(String server){
sServer = server;
}
/**
函数名:setSuffix
说明:付初值于LDAP域后缀
输入:suffix--LDAP服务器域后缀
输出:
*/
public void setSuffix(String suffix){
sSuffix = suffix;
}
/**
函数名:setDeptID
说明:付初值于部门编号
输入:deptID--部门编号
输出:
*/
public void setDeptID(String deptID){
sDeptID = deptID;
sFilter = "(&(objectClass=organizationalUnit)(departmentNumber="+sDeptID+"))";
}
/**
函数名:initEnv
说明:初始化系统变量
输入:
输出:
*/
public void initEnv(){
env.put("java.naming.factory.initial", "com.ibm.jndi.LDAPCtxFactory");
env.put("java.naming.provider.url", sServer);
env.put("java.naming.ldap.version", "3");
env.put("java.naming.ldap.derefAliases","never");
}
/**
函数名:search
说明:根据查询条件返回结果集
输入:
输出:结果集
*/
public Vector search() throws LDAPSearchException{
Vector deptInfo = new Vector();
String sTemp="";
if(sServer=="") throw new LDAPSearchException("LDAP服务器名称未付初值!");
if(sSuffix=="")throw new LDAPSearchException("LDAP域后缀未付初值!");
DirContext ctx = null;
NamingEnumeration results =null;
try{
initEnv();
ctx = new InitialDirContext(env);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
constraints.setReturningAttributes(sFields);
results = ctx.search(sSuffix, sFilter, constraints);
while (results.hasMore()) {
SearchResult si =(SearchResult)results.next();
//Debug.println(si.getName());
Attributes attrs = si.getAttributes();
if (attrs == null) {
Debug.println(" No attributes");
continue;
}
NamingEnumeration ae = attrs.getAll();
while (ae.hasMoreElements()) {
Attribute attr =(Attribute)ae.next();
String id = attr.getID();
Enumeration vals = attr.getAll();
while (vals.hasMoreElements())
// Debug.println(" "+id + ": " + vals.nextElement());
sTemp = (String)vals.nextElement();
deptInfo.add(sTemp);
}
}
}catch(Exception e){
throw new LDAPSearchException("查询失败!"+e.getMessage());
}finally
{
try
{
results.close();
ctx.close();
}catch(Exception e){
Debug.printErr(this.getClass().getName()+": "+e.getMessage());}
}
return deptInfo;
}
Top




