ldap急,急,急
我自己配了一个服务器,使用的是tomcat,但用户登录时需要在别的服务器上进行ldap验证,公司给了我apach的配置方法,url和BaseDN,我测试的代码是package com.test.ldap;import java.util.Hashtable;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
public class UserAuthenticate {
private String URL = "ldap://localhost:389/"; (改成自己的了)
private String BASEDN = "ou=catalogue,o=test.com"; (改成自己的了)
private String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
private LdapContext ctx = null;
private Hashtable env = null;
private Control[] connCtls = null;
private void LDAP_connect(){
env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,FACTORY);
env.put(Context.PROVIDER_URL, URL+BASEDN);//LDAP server
env.put(Context.SECURITY_AUTHENTICATION, "易做图");
//此处若不指定用户名和密码,则自动转换为匿名登录
try{
ctx = new InitialLdapContext(env,connCtls);
}catch(javax.naming.AuthenticationException e){
System.out.println("Authentication faild: "+e.toString());
}catch(Exception e){
System.out.println("Something wrong while authenticating: "+e.toString());
}
}
private String getUserDN(String email){
String userDN = "";
LDAP_connect();
try{
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration en = ctx.search("", "mail="+email, constraints); //The UID you are going to query,* means all nodes
if(en == null){
System.out.println("Have no NamingEnumeration.");
}
if(!en.hasMoreElements()){
System.out.println("Have no element.");
}
while (en != null && en.hasMoreElements()){//maybe more than one element
Object obj = en.nextElement();
if(obj instanceof SearchResult){
SearchResult si = (SearchResult) obj;
userDN += si.getName();
userDN += "," + BASEDN;
}
else{
System.out.println(obj);
}
System.out.println();
}
}catch(Exception e){
System.out.println("Exception in search():"+e);
}
return userDN;
}
public boolean authenricate(String ID,String password){
boolean valide = false;
String userDN = getUserDN(ID);
try {
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL,userDN);
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS,password);
ctx.reconnect(connCtls);
System.out.println(userDN + " is authenticated");
valide = true;
}catch (AuthenticationException e) {
System.out.println(userDN + " is not authenticated");
System.out.println(e.toString());
valide = false;
}catch (NamingException e) {
System.out.println(userDN + " is not authenticated");
valide = false;
}
return valide;
}
}
这些代码只改了url和BASEDN,其他的都没变,但我测试时一直连不上那个url,我听说要配一下tomcat的service.xml,不知道怎么配 --------------------编程问答-------------------- 。。。。怎么久了还没人。。
不需要配ldap服务器,只需要在用tomcat的服务器易做图问就行了 --------------------编程问答-------------------- 大侠,跪求呀
补充:Java , Web 开发