LDAP操作用到的包在附件下载
LDAPConfig.properties
dirverClass=com.novell.sql.LDAPDriver
#dns 10.192.139.200 ldap1.chinaunicom.com
#host=10.192.139.133
host=hqids.hq.unicom.local
baseDN=cn=users,dc=hq,dc=unicom
user=cn=root
password=password
url=JDBC:LDAP: //
#dns 10.192.139.200 ldap1.chinaunicom.com
#host=10.192.139.133
host=hqids.hq.unicom.local
baseDN=cn=users,dc=hq,dc=unicom
user=cn=root
password=password
url=JDBC:LDAP: //
LDAPConfig.java
public
class LDAPConfig {
public static HashMap getConfig()
{
HashMap configMap = new HashMap();
try{
//从配置文件中取得必须的LDAPConfig,并存入hashmap中
// ResourceBundle rb = ResourceBundle.getBundle(ComConstants.LDAP_CONFIG);
MessageResources rb = PropertyMessageResourcesFactory.createResources( "com/config/LDAPConfig.properties");
String dirverClass = rb.getMessage(ComConstants.LDAP_DIRVERCLASS);
//判断direrClass是否为空,如果不为空,则存入configMap中
if(dirverClass != null && dirverClass.trim().length() >0)
{
configMap.put(ComConstants.LDAP_DIRVERCLASS,dirverClass);
}
//判断host是否为空,如果不为空,则存入configMap中
String host = rb.getMessage(ComConstants.LDAP_HOST);
if(host != null && host.trim().length() >0)
{
configMap.put(ComConstants.LDAP_HOST,host);
}
//判断baseDN是否为空,如果不为空,则存入configMap中
String baseDN = rb.getMessage(ComConstants.LDAP_BASEDN);
if(baseDN != null && baseDN.trim().length() >0)
{
configMap.put(ComConstants.LDAP_BASEDN,baseDN);
}
//判断user是否为空,如果不为空,则存入configMap中
String user = rb.getMessage(ComConstants.LDAP_USER);
if(user != null && user.trim().length() >0)
{
configMap.put(ComConstants.LDAP_USER,user);
}
//判断password是否为空,如果不为空,则存入configMap中
String password = rb.getMessage(ComConstants.LDAP_PASSWORD);
if(password != null && password.trim().length() >0)
{
configMap.put(ComConstants.LDAP_PASSWORD,password);
}
//判断url是否为空,如果不为空,则存入configMap中
String url = rb.getMessage(ComConstants.LDAP_URL);
if(url != null && url.trim().length() >0)
{
configMap.put(ComConstants.LDAP_URL,url);
}
}
catch(Exception e)
{
//纪录错误类型,并返回null值
System.out.println(e.getMessage());
configMap = null;
}
//System.out.println(configMap.toString());
return configMap;
}
}
public static HashMap getConfig()
{
HashMap configMap = new HashMap();
try{
//从配置文件中取得必须的LDAPConfig,并存入hashmap中
// ResourceBundle rb = ResourceBundle.getBundle(ComConstants.LDAP_CONFIG);
MessageResources rb = PropertyMessageResourcesFactory.createResources( "com/config/LDAPConfig.properties");
String dirverClass = rb.getMessage(ComConstants.LDAP_DIRVERCLASS);
//判断direrClass是否为空,如果不为空,则存入configMap中
if(dirverClass != null && dirverClass.trim().length() >0)
{
configMap.put(ComConstants.LDAP_DIRVERCLASS,dirverClass);
}
//判断host是否为空,如果不为空,则存入configMap中
String host = rb.getMessage(ComConstants.LDAP_HOST);
if(host != null && host.trim().length() >0)
{
configMap.put(ComConstants.LDAP_HOST,host);
}
//判断baseDN是否为空,如果不为空,则存入configMap中
String baseDN = rb.getMessage(ComConstants.LDAP_BASEDN);
if(baseDN != null && baseDN.trim().length() >0)
{
configMap.put(ComConstants.LDAP_BASEDN,baseDN);
}
//判断user是否为空,如果不为空,则存入configMap中
String user = rb.getMessage(ComConstants.LDAP_USER);
if(user != null && user.trim().length() >0)
{
configMap.put(ComConstants.LDAP_USER,user);
}
//判断password是否为空,如果不为空,则存入configMap中
String password = rb.getMessage(ComConstants.LDAP_PASSWORD);
if(password != null && password.trim().length() >0)
{
configMap.put(ComConstants.LDAP_PASSWORD,password);
}
//判断url是否为空,如果不为空,则存入configMap中
String url = rb.getMessage(ComConstants.LDAP_URL);
if(url != null && url.trim().length() >0)
{
configMap.put(ComConstants.LDAP_URL,url);
}
}
catch(Exception e)
{
//纪录错误类型,并返回null值
System.out.println(e.getMessage());
configMap = null;
}
//System.out.println(configMap.toString());
return configMap;
}
}
LDAPOperate.java
public
class LDAPOperate {
/**
* 获得Mail
* @param configMap
* @param uid
* @return
*/
public static String getMailByUID(HashMap configMap,String uid)
{
LDAPConnection lc = null;
String retString = null;
try {
String hostName = "";
int port = 389;
String superUser = "dc=root";
String passwd ="";
String userBase = "cn=,dc=hq,dc=unicom";
//取得LDAP配置
if(configMap != null)
{
hostName = (String)configMap.get(ComConstants.LDAP_HOST);
userBase = (String)configMap.get(ComConstants.LDAP_BASEDN);
superUser = (String)configMap.get(ComConstants.LDAP_USER);
passwd = (String)configMap.get(ComConstants.LDAP_PASSWORD);
}
lc = new LDAPConnection();
lc.connect(hostName, port);
lc.authenticate(superUser, passwd);
String myFilter = "(uid=" + uid + ")";
String[] myAttrs = { "Mail"};
LDAPSearchResults myResults = null;
myResults = lc.search(userBase, LDAPv2.SCOPE_ONE, myFilter, myAttrs, false);
while (myResults.hasMoreElements()) {
// System.out.println("jjjjjjjjj");
LDAPEntry myEntry = myResults.next();
String nextDN = myEntry.getDN();
//System.out.println( nextDN );
LDAPAttributeSet entryAttrs = myEntry.getAttributeSet();
Enumeration attrsInSet = entryAttrs.getAttributes();
while (attrsInSet.hasMoreElements()) {
LDAPAttribute nextAttr = (LDAPAttribute) attrsInSet.nextElement();
String attrName = nextAttr.getName();
//System.out.println( "\t" + attrName + ":" );
Enumeration valsInAttr = nextAttr.getStringValues();
//System.out.println( "=" + valsInAttr.nextElement().toString() + ":" );
if (attrName.trim().equalsIgnoreCase( "Mail")) {
if (valsInAttr.hasMoreElements()) {
String nextValue = (String) valsInAttr.nextElement();
retString = nextValue.trim();
}
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
finally
{
//关闭连接
if(lc != null)
{
try {
lc.disconnect();
} catch (LDAPException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
}
return retString;
/* String retMail = null;
try{
Connection conn;
Statement statement;
ResultSet rs;
String query = null;
String driverClass = "";
String url = "";
String host = "";
String user = "";
String password = "";
String baseDN = "";
//取得LDAP配置
if(configMap != null)
{
driverClass = (String)configMap.get(ComConstants.LDAP_DIRVERCLASS);
host = (String)configMap.get(ComConstants.LDAP_HOST);
baseDN = (String)configMap.get(ComConstants.LDAP_BASEDN);
user = (String)configMap.get(ComConstants.LDAP_USER);
password = (String)configMap.get(ComConstants.LDAP_PASSWORD);
url = (String)configMap.get(ComConstants.LDAP_URL);
}
Class.forName(driverClass);
if (host == null)
{
url += "localhost;";
}
else
{
url += host + ";";
}
if (baseDN != null)
{
url += "baseDN=" + baseDN + ";";
}
url += "useCleartext=true";
conn = DriverManager.getConnection(url, user, password);
//sql
query = "Select mail from inetOrgperson where uid='"+uid+"'";
statement = conn.createStatement();
rs = statement.executeQuery(query);
if(rs.next()) {
retMail = rs.getString("mail");
}
}catch(Exception e){
System.out.print("error ----> ldap search ...");
e.printStackTrace();
}
return retMail;*/
}
public static String getMail(HashMap configMap,String uid)
{
LDAPConnection lc = null;
String retString = null;
try {
String hostName = "";
int port = 389;
String superUser = "cn=root";
String passwd ="";
String userBase = "cn=,dc=hq,dc=unicom";
//取得LDAP配置
if(configMap != null)
{
hostName = (String)configMap.get(ComConstants.LDAP_HOST);
userBase = (String)configMap.get(ComConstants.LDAP_BASEDN);
superUser = (String)configMap.get(ComConstants.LDAP_USER);
passwd = (String)configMap.get(ComConstants.LDAP_PASSWORD);
}
lc = new LDAPConnection();
lc.connect(hostName, port);
lc.authenticate(superUser, passwd);
String myFilter = "(uid=" + uid + ")";
String[] myAttrs = { "Mail"};
LDAPSearchResults myResults = null;
myResults = lc.search(userBase, LDAPv2.SCOPE_ONE, myFilter, myAttrs, false);
while (myResults.hasMoreElements()) {
// System.out.println("jjjjjjjjj");
LDAPEntry myEntry = myResults.next();
String nextDN = myEntry.getDN();
//System.out.println( nextDN );
LDAPAttributeSet entryAttrs = myEntry.getAttributeSet();
Enumeration attrsInSet = entryAttrs.getAttributes();
while (attrsInSet.hasMoreElements()) {
LDAPAttribute nextAttr = (LDAPAttribute) attrsInSet.nextElement();
String attrName = nextAttr.getName();
//System.out.println( "\t" + attrName + ":" );
Enumeration valsInAttr = nextAttr.getStringValues();
//System.out.println( "=" + valsInAttr.nextElement().toString() + ":" );
if (attrName.trim().equalsIgnoreCase( "Mail")) {
if (valsInAttr.hasMoreElements()) {
String nextValue = (String) valsInAttr.nextElement();
retString = nextValue.trim();
}
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
finally
{
//关闭连接
if(lc != null)
{
try {
lc.disconnect();
} catch (LDAPException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
}
return retString;
}
}
/**
* 获得Mail
* @param configMap
* @param uid
* @return
*/
public static String getMailByUID(HashMap configMap,String uid)
{
LDAPConnection lc = null;
String retString = null;
try {
String hostName = "";
int port = 389;
String superUser = "dc=root";
String passwd ="";
String userBase = "cn=,dc=hq,dc=unicom";
//取得LDAP配置
if(configMap != null)
{
hostName = (String)configMap.get(ComConstants.LDAP_HOST);
userBase = (String)configMap.get(ComConstants.LDAP_BASEDN);
superUser = (String)configMap.get(ComConstants.LDAP_USER);
passwd = (String)configMap.get(ComConstants.LDAP_PASSWORD);
}
lc = new LDAPConnection();
lc.connect(hostName, port);
lc.authenticate(superUser, passwd);
String myFilter = "(uid=" + uid + ")";
String[] myAttrs = { "Mail"};
LDAPSearchResults myResults = null;
myResults = lc.search(userBase, LDAPv2.SCOPE_ONE, myFilter, myAttrs, false);
while (myResults.hasMoreElements()) {
// System.out.println("jjjjjjjjj");
LDAPEntry myEntry = myResults.next();
String nextDN = myEntry.getDN();
//System.out.println( nextDN );
LDAPAttributeSet entryAttrs = myEntry.getAttributeSet();
Enumeration attrsInSet = entryAttrs.getAttributes();
while (attrsInSet.hasMoreElements()) {
LDAPAttribute nextAttr = (LDAPAttribute) attrsInSet.nextElement();
String attrName = nextAttr.getName();
//System.out.println( "\t" + attrName + ":" );
Enumeration valsInAttr = nextAttr.getStringValues();
//System.out.println( "=" + valsInAttr.nextElement().toString() + ":" );
if (attrName.trim().equalsIgnoreCase( "Mail")) {
if (valsInAttr.hasMoreElements()) {
String nextValue = (String) valsInAttr.nextElement();
retString = nextValue.trim();
}
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
finally
{
//关闭连接
if(lc != null)
{
try {
lc.disconnect();
} catch (LDAPException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
}
return retString;
/* String retMail = null;
try{
Connection conn;
Statement statement;
ResultSet rs;
String query = null;
String driverClass = "";
String url = "";
String host = "";
String user = "";
String password = "";
String baseDN = "";
//取得LDAP配置
if(configMap != null)
{
driverClass = (String)configMap.get(ComConstants.LDAP_DIRVERCLASS);
host = (String)configMap.get(ComConstants.LDAP_HOST);
baseDN = (String)configMap.get(ComConstants.LDAP_BASEDN);
user = (String)configMap.get(ComConstants.LDAP_USER);
password = (String)configMap.get(ComConstants.LDAP_PASSWORD);
url = (String)configMap.get(ComConstants.LDAP_URL);
}
Class.forName(driverClass);
if (host == null)
{
url += "localhost;";
}
else
{
url += host + ";";
}
if (baseDN != null)
{
url += "baseDN=" + baseDN + ";";
}
url += "useCleartext=true";
conn = DriverManager.getConnection(url, user, password);
//sql
query = "Select mail from inetOrgperson where uid='"+uid+"'";
statement = conn.createStatement();
rs = statement.executeQuery(query);
if(rs.next()) {
retMail = rs.getString("mail");
}
}catch(Exception e){
System.out.print("error ----> ldap search ...");
e.printStackTrace();
}
return retMail;*/
}
public static String getMail(HashMap configMap,String uid)
{
LDAPConnection lc = null;
String retString = null;
try {
String hostName = "";
int port = 389;
String superUser = "cn=root";
String passwd ="";
String userBase = "cn=,dc=hq,dc=unicom";
//取得LDAP配置
if(configMap != null)
{
hostName = (String)configMap.get(ComConstants.LDAP_HOST);
userBase = (String)configMap.get(ComConstants.LDAP_BASEDN);
superUser = (String)configMap.get(ComConstants.LDAP_USER);
passwd = (String)configMap.get(ComConstants.LDAP_PASSWORD);
}
lc = new LDAPConnection();
lc.connect(hostName, port);
lc.authenticate(superUser, passwd);
String myFilter = "(uid=" + uid + ")";
String[] myAttrs = { "Mail"};
LDAPSearchResults myResults = null;
myResults = lc.search(userBase, LDAPv2.SCOPE_ONE, myFilter, myAttrs, false);
while (myResults.hasMoreElements()) {
// System.out.println("jjjjjjjjj");
LDAPEntry myEntry = myResults.next();
String nextDN = myEntry.getDN();
//System.out.println( nextDN );
LDAPAttributeSet entryAttrs = myEntry.getAttributeSet();
Enumeration attrsInSet = entryAttrs.getAttributes();
while (attrsInSet.hasMoreElements()) {
LDAPAttribute nextAttr = (LDAPAttribute) attrsInSet.nextElement();
String attrName = nextAttr.getName();
//System.out.println( "\t" + attrName + ":" );
Enumeration valsInAttr = nextAttr.getStringValues();
//System.out.println( "=" + valsInAttr.nextElement().toString() + ":" );
if (attrName.trim().equalsIgnoreCase( "Mail")) {
if (valsInAttr.hasMoreElements()) {
String nextValue = (String) valsInAttr.nextElement();
retString = nextValue.trim();
}
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
finally
{
//关闭连接
if(lc != null)
{
try {
lc.disconnect();
} catch (LDAPException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
}
return retString;
}
}
LDAPTest.java
public
class LDAPTest {
/**
* @param args
*/
public static void main(String[] args) {
// String test = LDAPOperate.getMail(LDAPConfig.getConfig(),"000015011");
String test= null;
long time3 = System.currentTimeMillis();
test = LDAPOperate.getMail(LDAPConfig.getConfig(), "000015011");
long time4 = System.currentTimeMillis();
System.out.println(time4-time3);
String[] temps = StringOperate.split(test, "@");
System.out.println( "test:"+test);
String test1 = temps[0];
System.out.println( "test1:"+test1);
long time1 = System.currentTimeMillis();
test = LDAPOperate.getMailByUID(LDAPConfig.getConfig(), "000015011");
long time2 = System.currentTimeMillis();
System.out.println(time2-time1);
System.out.println( "test:"+test);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// String test = LDAPOperate.getMail(LDAPConfig.getConfig(),"000015011");
String test= null;
long time3 = System.currentTimeMillis();
test = LDAPOperate.getMail(LDAPConfig.getConfig(), "000015011");
long time4 = System.currentTimeMillis();
System.out.println(time4-time3);
String[] temps = StringOperate.split(test, "@");
System.out.println( "test:"+test);
String test1 = temps[0];
System.out.println( "test1:"+test1);
long time1 = System.currentTimeMillis();
test = LDAPOperate.getMailByUID(LDAPConfig.getConfig(), "000015011");
long time2 = System.currentTimeMillis();
System.out.println(time2-time1);
System.out.println( "test:"+test);
}
}