`
bravewu
  • 浏览: 48163 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

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

    博客分类:
  • Java
阅读更多
偶做的一个项目是使用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");
		}
	}
}


1
0
分享到:
评论
1 楼 wangqh_2008 2013-03-10  
有点没有看明白呀,你在WAS控制台已经配置LDAP主机名和端口了,怎么还需要代码?

相关推荐

Global site tag (gtag.js) - Google Analytics