如何从WAS上动态获取LDAP服务器主机名称和端口号

偶做的一个项目是使用LDAP(Lightweight Directory Access Protocol)服务器进行用户认证,而对于新用户还需要从LDAP服务器上获取当前用户的所有基本信息(姓,名,电子邮箱等),关于如何从LDAP上获取数据,本文就不讲了,因为网上很多文章讲这个,本文主要想讲一下如何从WAS(WebSphere Application Server)上动态获取LDAP服务器主机名称和端口号。

进入正文前,我首先想提出两个问题:

1. 为什么需要动态获取?

因为在测试服务器上用的是测试LDAP服务器,而生产服务器上是生产LDAP服务器。两个服务的主机名不一样,有时甚至端口也不一样。

所以在连接LDAP时,需要用不同的LDAP服务器主机名称和端口号。

在我提供这个方案之前,我们这个项目一直都是采用将LDAP服务器名称和端口号存储在一个属性文件中,但我们需要在测试与生产两个环境中提供不同的属性文件。因此使我们最终提供的部署文件不是很独立。每次往生产服务器上发布时都需要特别小心地更改这个配置文件。有时候很容易搞错。

2. 为什么可以从WAS上获取?

要使用LDAP服务器来进行用户认证,必须要在WAS administration console 上配置所使用的LDAP服务器主机名和端口号。这样我们就可以直接从WAS上获取到LDAP服务器主机名称和端口号。这样就不需要配置文件了,当然更不需要人为地做任何更改,完全实现了部署文件的独立性。即测试服务器与生产服务器使用相同的包。

以下是我在实现这个解决方案时所用的代码:


package org.brad.woo.ladp;

import java.io.File;
import java.util.*;
import javax.naming.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class LDAPHelper {
private final static String LDAP_HOST_PREFIX="ldap://";
private final static String LDAP_SSL_PORT="636";
private static Hashtable env = null;
static{
env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
String ldapHost = null;
String ldapPort = null;
try {
System.out.println("LDAPHelper - begin to parse ldap info");
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
StringBuffer tempPath=new StringBuffer();
tempPath.append(new File(".").getAbsolutePath());
tempPath.append("/config/cells/");
File tempDirectory=new File(tempPath.toString());
if(tempDirectory.exists()&&tempDirectory.list()[0]!=null){
tempPath.append(tempDirectory.list()[0]);
tempPath.append("/security.xml");
File file = new File(tempPath.toString());
if(file.exists()){
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
NodeList nodeLst = doc.getElementsByTagName("hosts");
Node fstNode = nodeLst.item(0);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
ldapHost = fstElmnt.getAttribute("host");
System.out.println("LDAPHelper - ldapHost : " + ldapHost);
ldapPort = fstElmnt.getAttribute("port");
System.out.println("LDAPHelper - ldapPort : " + ldapPort);
}
}
}
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
System.out.println("LDAPHelper - end to parse ldap info");
} catch (Exception e) {
e.printStackTrace();
}
env.put(Context.PROVIDER_URL, LDAP_HOST_PREFIX+ldapHost+":"+ldapPort);
if(ldapPort.equals(LDAP_SSL_PORT)){
env.put("java.naming.ldap.derefAliases", "never");
env.put("java.naming.ldap.version", "3");
env.put(Context.REFERRAL, "follow");
env.put("java.naming.ldap.referral;.bind", "true");
env.put(Context.SECURITY_PROTOCOL, "ssl");
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值