如题。。 怎么读出域和用户名呢?

解决方案 »

  1.   

    我的最近项目也用到LDAP实现域登陆 ,在网上找了几十个版本的代码整合,,总算勉强达到需求,你可以参考下:我用Spring 实现的  不过原理都是一样:
      首先在网上下载 spring-ldap.jar 包, 当前最新的版本是1.3,
           新建WEB工程,导入 常用的spring包, 和 spring-ldap.jar包,
            下面是部分代码 
          applicationContext.xml中如下配置:
       <?xml version="1.0" encoding="UTF-8"?>
    <beans
     xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="ContextSource" class="org.springframework.ldap.core.support.LdapContextSource">
      <property name="url" value="ldap://192.168.1.102:389" />
      <property name="base" value="dc=EKT,dc=com" />
      <property name="referral" value="follow"></property> 
      <property name="authenticationSource" ref="AuthenticationSource" />
     </bean>
     <bean id="AuthenticationSource"
      class="org.springframework.ldap.authentication.DefaultValuesAuthenticationSourceDecorator">
      <property name="target" ref="SpringSecurityAuthenticationSource" />
      <property name="defaultUser" value="" />
      <property name="defaultPassword" value="" />
     </bean>
     <bean id="SpringSecurityAuthenticationSource"
      class="com.eagle.struts.util.LdapAuthenticationSource">
      </bean>
     <bean id="LdapTemplate" class="org.springframework.ldap.core.LdapTemplate">
      <constructor-arg ref="ContextSource" />
     </bean>
    <bean name="/login" class="com.eagle.struts.action.LoginAction" >
    <property name="lt" ref="LdapTemplate"></property>
    <property name="dc" value="@EKT.com"></property>
    <property name="ssas" ref="SpringSecurityAuthenticationSource"></property>
    </bean>
    </beans> 
    在web.xml加载 Spring 配置文件
     <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
     <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:applicationContext.xml</param-value>
     </context-param>
    创建   LdapAuthenticationSource  类
    package com.eagle.struts.util;
    import org.springframework.ldap.core.AuthenticationSource;
    public class LdapAuthenticationSource implements AuthenticationSource
    {
     private static ThreadLocal login = new ThreadLocal();
      private static ThreadLocal password = new ThreadLocal();
      public static void setLogin(String login) {
       LdapAuthenticationSource.login.set(login);
      }
      public static void setPassword(String password) {
       LdapAuthenticationSource.password.set(password);
      }
      public String getPrincipal() {
       String login = (String) LdapAuthenticationSource.login.get();
       if (login == null || "".equals(login)||login.toLowerCase().startsWith("admin")) { //这里是我自己加的,不知道为什么
     //在WEB层,只要用 admin 和 administrators账号,不管密码是什么都可以登录
        return null;
       }
       return login;
      }
      public String getCredentials() {
       String password = (String) LdapAuthenticationSource.password.get();
       if (password == null || "".equals(password)) {
        return null;
       }
       return password;
      }
    }
     
    下面是Action 中的部分代码:package com.eagle.struts.action;
    import javax.naming.directory.DirContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.actions.DispatchAction;
    import org.springframework.ldap.core.LdapTemplate;
    import org.springframework.ldap.support.LdapUtils;
    import com.eagle.struts.form.LoginForm;
    import com.eagle.struts.util.LdapAuthenticationSource;
    public class LoginAction extends DispatchAction
    {
     private LdapTemplate  lt=null;
     private String  dc=null;
     private LdapAuthenticationSource ssas=null;
     public void setDc(String dc)
     {
      this.dc = dc;
     }
     public void setSsas(LdapAuthenticationSource ssas)
     {
      this.ssas = ssas;
     }
     public void setLt(LdapTemplate lt)
     {
      this.lt = lt;
     }
     public ActionForward check(ActionMapping mapping, ActionForm form,
       HttpServletRequest request, HttpServletResponse response)
     {
      LoginForm loginForm = (LoginForm) form;
      ssas.setLogin(loginForm.getUsername());
      ssas.setPassword(loginForm.getPassword());
       
      if(authenticate(ssas))
        {
      System.out.println("登录成功!");
        }
      else
      {
       System.out.println("登录失败!");
      }
      return null;
      
     }
     /**
      * 验证用户名和密码的方法
      */
     public  boolean authenticate(LdapAuthenticationSource ssas) {    
                   
               DirContext ctx = null;     
                 try {     
                   ctx = lt.getContextSource().getContext(ssas.getPrincipal()+dc,     
                     ssas.getCredentials());     
                   return true;     
               } catch (Exception e) {                  
            
                   return false;     
               } finally {        
                   LdapUtils.closeContext(ctx);     
               }     
        }
    }
     
     
    先写在这里,以后再补充, 千万别尝试用这个去连我们公司的LDAP服务器哦, 我可付不起责任,呵呵, 有什么意见请指教!谢谢!
      

  2.   

    谢谢! 没有简单点的方法吗?像.net 的HttpContext.Current.User.Identity.Name.ToString(); 直接就出来了