您的位置:首页 > 编程语言 > Java开发

用JAVA通过JNDI操作Domino中LDAP

2009-05-31 08:31 337 查看
由于现在好多企业都在使用Domino系统。这里我就使用JAVA语言直接调用Domino中用户信息,进行常见的认证,增加,

修改和删除操作。

一、获取Domino连接

/**
* 从连接池中获取一个连接.
*
* @return LdapContext
* @throws NamingException
*/
public LdapContext getConnectionFromFool() throws NamingException {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.0.189:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL,
"CN=administrator,O=fall");
env.put(Context.SECURITY_CREDENTIALS, "123456");
env.put("com.sun.jndi.ldap.connect.pool", "true");
env.put("java.naming.referral", "follow");
return new InitialLdapContext(env, null);
}


二、认证用户信息

/**
* 校验用户登录.
*
* @param userDn
*            String
* @param password
*            String
* @return boolean
*/
public boolean authenticate(String userDn, String password) {
LdapContext ctx = null;
try {
Control[] connCtls = new Control[] {};
ctx = getConnectionFromFool();
ctx.getRequestControls();
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, userDn);
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
ctx.reconnect(connCtls);
return true;
} catch (AuthenticationException e) {
return false;
} catch (NamingException e) {
return false;
} finally {
if (ctx != null) {
try {
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL,
"CN=administrator,O=fall");
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS,
"123456");
ctx.reconnect(ctx.getConnectControls());
ctx.close();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ctx = null;
}
}
}


三、添加用户信息

/**
* 添加用户.
*
* @param userDN
*            String用户DN
* @param userName
*            String 用户登录名
* @param userPwd
*            String 用户密码
* @return boolean 添加是否成功.
*
*/
public boolean addUser(String userDN, String userName, String userPwd) {
LdapContext ctx = null;
try {
ctx = getConnectionFromFool();
// Create attributes to be associated with the new user
Attributes attrs = new BasicAttributes(true);
attrs.put("objectClass", "person");
attrs.put("userPassword", userPwd);
attrs.put("cn", userName);
attrs.put("sn", userName);
return true;
} catch (NamingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ctx = null;
}
}
return false;
}


或者:

/**
* 添加用户.
*
* @param userDN
*            String用户DN
* @param attrs
*            Attributes 用户属性
* @return boolean 添加是否成功.
*
*/
public boolean addUser(String userDN, Attributes attrs) {
LdapContext ctx = null;
try {
ctx = getConnectionFromFool();
// 补充必须的用户属性.
if (attrs.get("objectClass") == null
||attrs.get("objectClass").get() == null) {
attrs.put("objectClass", "person");
}
if (attrs.get("sn") == null
||attrs.get("sn").get() == null) {
attrs.put("sn", userName);
}
ctx.createSubcontext(userDN, attrs);
return true;
} catch (NamingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ctx = null;
}
}
return false;
}


四、修改用户信息

/**
* 修改用户信息.
*
* @param attrs
*            Attributes 需要修改的用户属性.
* @param userDN
*            String 用户DN
* @return
*/
public boolean modify(Attributes attrs, String userDN) {
LdapContext ctx = null;
try {
ctx = getConnectionFromFool();
attrs.remove("cn");
ctx.modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, attrs);
return true;
} catch (NamingException e) {
System.err.println("Problem changing password: " + e);
} catch (Exception e) {
System.err.println("Problem: " + e);
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ctx = null;
}
}
return false;
}


五、删除用户信息

/**
* 删除用户.
*
* @param userDN
*            String 用户DN
* @return
*/
public boolean del(String userDN) {
LdapContext ctx = null;
try {
ctx = getConnectionFromFool();
ctx.destroySubcontext(userDN);
return true;
} catch (NamingException e) {
System.err.println("Problem changing password: " + e);
} catch (Exception e) {
System.err.println("Problem: " + e);
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ctx = null;
}
}
return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: